如何存储从一个活动复选框值到另一个活动 [英] how to store check box value from one activity to another activity

查看:224
本文介绍了如何存储从一个活动复选框值到另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表checkboxs 2活动,我不得不复选框的每个值传递到另一个活动,并使其选中,在这里我不能使用意图传递,因为这两个活动不是下一在一起。

在这种情况下,我怎么会值存储在数组中,并在另一个活动以检索

感谢所有,我现在已经做到了,我张贴。

  INT I = 0;
        复选框=新的复选框[getItemPR.size()];
        Constants.INDEX_TEMP_NEW =新INT [getItemPR.size(); // stoing数组[]
        而(ⅰ&下; getItemPR.size()){            项dtItem = getItemPR.get(ⅰ);            TableLayout表11;
                表11 =(TableLayout)LayoutInflater.from(本).inflate(
                        R.layout.itemoverview2,NULL);
                复选框[i] =(复选框)table11.findViewById(R.id.checkBox1);                复选框[I] .setId(ⅰ);
                复选框[I]
                        .setOnCheckedChangeListener(新OnCheckedChangeListener(){                            @覆盖
                            公共无效onCheckedChanged(
                                    CompoundButton buttonView,
                                    布尔器isChecked){                            onSetcheckboxchanged(buttonView.getId());
                            }                        });
                如果(Constants.INDEX_TEMP_NEW [I] == 0){
                    复选框[I] .setChecked(真);                }其他{
                    复选框[I] .setChecked(假);
                }

 公共无效onSetcheckboxchanged(INT K){        如果(复选框[k]的.isChecked()){
            Constants.INDEX_TEMP_NEW [K] = 0;        }其他{
            Constants.INDEX_TEMP_NEW [K] = 1;
        }
}


解决方案

制作一个Java类,这将延伸到应用程序类,并通过调用获取类的对象的 App.getInstance()并然后就可以设置或任何活动让你选中的列表项。

 公共类应用扩展android.app.Application {
私有静态应用程序;
私人的ArrayList<整数GT; checkedItemList;
私人活动活动;@覆盖
公共无效的onCreate(){
    super.onCreate();
    应用=这;
}公共静态应用的getInstance(){
    返回程序;
}公共无效setCheckedItemList(ArrayList的<整数GT; checkedItemList){
    this.checkedItemList = checkedItemList;
}公众的ArrayList<整数GT; getCheckedItemList(){
    返回this.checkedItemList;
}
}

现在注册下的Andr​​oid的Manifest.xml应用程序类的应用程序标记

 <应用
    机器人:名字=应用程序
    机器人:图标=@绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ AppTheme>
    <活动
        机器人:名字=。YourActivity
        机器人:标签=@字符串/ APP_NAME>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.MAIN/>
            <类机器人:名字=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
< /用途>

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

解决方案

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>

这篇关于如何存储从一个活动复选框值到另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆