问题
i have two activity with checkboxs in a list and i have to pass each value of checkbox to another activity and make it checked, where i can't use intent to pass that because both activity is not next together.
In this case how i will store the values in an array and to retrive in another activity
thanks to all, i have done it now and i'm posting it.
int i = 0;
checkBox = new CheckBox[getItemPR.size()];
Constants.INDEX_TEMP_NEW=new int[getItemPR.size()];// stoing in array[]
while (i < getItemPR.size()) {
ITEM dtItem = getItemPR.get(i);
TableLayout table11;
table11 = (TableLayout) LayoutInflater.from(this).inflate(
R.layout.itemoverview2, null);
checkBox[i] = (CheckBox) table11.findViewById(R.id.checkBox1);
checkBox[i].setId(i);
checkBox[i]
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(
CompoundButton buttonView,
boolean isChecked) {
onSetcheckboxchanged(buttonView.getId());
}
});
if (Constants.INDEX_TEMP_NEW[i] == 0) {
checkBox[i].setChecked(true);
} else {
checkBox[i].setChecked(false);
}
and
public void onSetcheckboxchanged(int k) {
if (checkBox[k].isChecked()) {
Constants.INDEX_TEMP_NEW[k] = 0;
} else {
Constants.INDEX_TEMP_NEW[k] = 1;
}
}
回答1:
I usually use a static class/methods for this kind of thing, utilising SharedPreferences
public static class Hal {
private static final String PREFS = "com.xxx.xxx.xxx";
public static String getPreferenceString(Context ctx, String key) {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(Hal.PREFS, Activity.MODE_PRIVATE);
return sharedPreferences.getString(key, "");
}
public static void setPreferenceString(Context ctx, String key, String val) {
SharedPreferences sharedPreferences = ctx.getSharedPreferences(Hal.PREFS, Activity.MODE_PRIVATE);
Editor preferencesEditor = sharedPreferences.edit();
preferencesEditor.putString(key, val);
preferencesEditor.commit();
return;
}
}
回答2:
You can make a singleton class to store the value of the array. eg. 'Storage' class with has the getter, setter of an array. You can set the data here in the class:
public class Storage {
private static Strorage strorageData = new Strorage ();
public static synchronized Strorage getInstance() {
if (strorageData == null) {
strorageData = new Strorage ();
}
return strorageData ;
}
// getter & setter
}
In your activity you can access the object of this class using
private Storage storageData;
storageData = Storage .getInstance();
In this get the data by using getter.
回答3:
Create one java class which will extends to Application class, and get the object of that class by calling App.getInstance() and then you can set or get your selected list item from any activity.
public class App extends android.app.Application {
private static App app;
private ArrayList<Integer> checkedItemList;
private Activity activity;
@Override
public void onCreate() {
super.onCreate();
app = this;
}
public static App getInstance(){
return app;
}
public void setCheckedItemList(ArrayList<Integer> checkedItemList){
this.checkedItemList = checkedItemList;
}
public ArrayList<Integer> getCheckedItemList(){
return this.checkedItemList;
}
}
Now register the Application class in Android Manifest.xml under application tag
<application
android:name=".App"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".YourActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
来源:https://stackoverflow.com/questions/15111266/how-to-store-check-box-value-from-one-activity-to-another-activity