2013-02-27 38 views
0

我有两个活动,在列表中有复选框,我必须将每个复选框的值传递给另一个活动并使其检查,其中我无法使用意图传递因为两个活动都不在一起。如何将复选框的值从一个活动存储到另一个活动

在这种情况下,我将如何将值存储在阵列中,而在另一个活动

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

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

public void onSetcheckboxchanged(int k) { 

     if (checkBox[k].isChecked()) {    
      Constants.INDEX_TEMP_NEW[k] = 0; 

     } else { 
      Constants.INDEX_TEMP_NEW[k] = 1; 
     }  
} 
+0

将使用共享偏好存储复选框值.. – 2013-02-27 11:48:08

+0

@Tamilarasi Sivaraj我必须存储的复选框如果列表与该列表具有相同的详细信息,并且此复选框在此处被选中,则在另一个活动中使用该值也是必须的。 – saran 2013-02-27 12:06:22

+0

你可以发布你的代码到目前为止你尝试过吗? – 2013-02-27 12:09:42

回答

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

你可以让一个单独的类来存储的价值阵列。 例如。 '存储'类具有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(); 

在此得到利用吸附剂中的数据访问该类的对象。

+0

如果列表中有与该列表相同的详细信息,并且此复选框在此处被选中,则必须将其复选框值存储为其他活动。 – saran 2013-02-27 12:07:47

+0

在Strorage类中使用id(int)和value(boolean)创建二维数组列表。你在问什么? – Vineet 2013-02-27 12:16:36

+0

checkBox.isChecked(); – Vineet 2013-02-27 12:19:01

1

创建一个java类,它将扩展到Application类,并通过调用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; 
} 
} 

现在注册下应用标签Android中的Manifest.xml应用程序类

<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> 
+0

谢谢,但如何存储复选框的值,我是新的Android的 – saran 2013-02-27 12:14:46

+0

如果您使用多选择ListView然后获取选定的项目,并将该列表设置为该类别... – 2013-02-27 12:38:35

相关问题