0

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;
        }       
}
4

3 回答 3

1

创建一个将扩展到 Application 类的 java 类,并通过调用App.getInstance()获取该类的对象,然后您可以从任何活动中设置或获取您选择的列表项。

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;
}
}

现在在application 标签下的 Android Manifest.xml 中注册 Application 类

<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>
于 2013-02-27T12:06:41.767 回答
1

我通常对这种事情使用静态类/方法,利用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;
    }
}
于 2013-02-27T11:53:16.750 回答
1

您可以创建一个单例类来存储数组的值。例如。'Storage' 类具有数组的 getter、setter。您可以在类中设置数据:

public class Storage { 
private static Strorage strorageData = new Strorage ();
public static synchronized Strorage getInstance() {
        if (strorageData == null) {
            strorageData = new Strorage ();
        }
        return strorageData ;
    }
    // getter & setter

}

在您的活动中,您可以使用访问此类的对象

     private Storage storageData;
 storageData = Storage .getInstance();

在此使用 getter 获取数据。

于 2013-02-27T11:59:08.977 回答