Save Restore Array List Android Example
int[] mArrayOfInt;
String[] mArrayOfString;
List<String> mList;
public void saveState(Bundle map)
{
map.putIntArray("mArrayOfInt", mArrayOfInt);
map.putStringArray("mArrayOfString", mArrayOfString);
map.putStringArrayList("mList", mList);
}
public void restoreState(Bundle map)
{
mArrayOfInt= map.getIntArray("mArrayOfInt");
mArrayOfString = map.getStringArray("mArrayOfString");
mList = map.getStringArrayList("mList");
}
396LW NO topic_id
AD
Další témata ....(Topics)
Screen Android example source code for developers.
Get orientation of screen.
Get orientation of screen.
public int getScreenOrientation() {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
return 1; // Portrait Mode
}else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
return 2; // Landscape mode
}
return 0;
}
HashMap<String,Locale> _mapOfLocale = new HashMap<String,Locale>();
_mapOfLocale.put("French",Locale. FRENCH );
_mapOfLocale.put("German",Locale. GERMAN );
_mapOfLocale.put("Italian",Locale. ITALIAN );
for (Entry<String, Locale> entry : _mapOfLocale.entrySet()) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
Android Emulator warning:[Accessibility] Missing contentDescription attribute on image
Workaround example code:
Workaround example code:
android:contentDescription="@string/desc"
<ImageView
android:id="@+id/imageViewOil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/desc"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="84dp"
android:src="@drawable/tank_silver" />
Goto in for cycle Java example source code.
MainClass.java
MainClass.java
public class MainClass {
public static void main(String[] arg) {
String[] arrayOfString = {"nothing", "Hello", "people"
, "bye-bye", "hello", "world!", "end" };
OuterLoop: for (int i = 0;i<6; i++) {
for (int j = 0; j < arrayOfString.length; j++) {
if (arrayOfString[j].equals("world!")) {
continue OuterLoop; // as goto from Csharp, or C/C++
}
System.out.println(arrayOfString[j]);
System.out.println(i);
if (i == 1) {
System.out.println("break");
break OuterLoop;
}
}
}
}
}
/*
nothing
0
Hello
0
people
0
bye-bye
0
hello
0
nothing
1
break
*/
getContext() getApplicationContext() method Java Android example source
Context myContext_1 = ThisClassName.this; // to open a Dialog
Context myContext_2 = getContext();
Context myContext_3 = this.getContext();
Context myContext_4 = this;
Context myContext_5 = this.getApplicationContext ();
OnClickListener getImageBtnOnClick = new OnClickListener() {
public void onClick(View view) {
Context context = view.getContext();
}
};
// Toast
Toast.makeText(getApplicationContext(), "Context == getApplicationContext "
, Toast.LENGTH_SHORT).show();
// store Context in public class
public class MyActivity extends Activity {
public static Context myCnt = null;
...
protected void onCreate(Bundle icicle) {
...
myCnt = this;
MyStorage.setContext(myCnt);
// or
// MyStorage.setContext(this);
// cntxFromStorage == this
Context cntxFromStorage = MyStorage.getContext();
...
};
};
public class MyStorage
{
private static Context cntStorageContext = null;
public static Context getContext() {
return cntStorageContext;
}
public static void setContext(Context context) {
MyStorage.cntStorageContext = context;
}
};
class DataBaseHelper extends SQLiteOpenHelper {
// get MyActivity context
Context cnt = MyStorage.getContext();
}
Editace: 2013-12-09 11:04:36
Počet článků v kategorii: 396
Url:save-restore-array-list-android-example