Android 使用SharedReferences在listview中存储复选框的状态
下次打开应用程序时,如何使用共享首选项存储复选框的状态?我正在使用一个自定义适配器,所以我猜它必须放在里面,但我不太确定 我的适配器:Android 使用SharedReferences在listview中存储复选框的状态,android,sharedpreferences,Android,Sharedpreferences,下次打开应用程序时,如何使用共享首选项存储复选框的状态?我正在使用一个自定义适配器,所以我猜它必须放在里面,但我不太确定 我的适配器: public class MobileArrayAdapter extends ArrayAdapter<String> { private final Context context; private final String[] values; private ArrayList<Boolean> itemChecked = new
public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
public MobileArrayAdapter(Context context, String[] values) {
super(context, R.layout.list_adapter, values);
this.context = context;
this.values = values;
for (int i = 0; i < this.getCount(); i++) {
itemChecked.add(i, false);
}
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.list_adapter,
parent, false);
}
// in your code you search for the CheckBox with the id checkBox1 2 times so I assumed that you are referring to the same view.
CheckBox cBox = (CheckBox) rowView.findViewById(R.id.checkBox1);
cBox.setTextColor(0xFFFFFFFF);
cBox.setText(values[position]);
cBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
itemChecked.set(position, true);
// do some operations here
} else {
itemChecked.set(position, false);
// do some operations here
}
}
});
cBox.setChecked(itemChecked.get(position));
return rowView;
}
}
要保存选择,请创建一个方法saveSelections并在
onPause()
和onDestroy()
中调用它,或者创建一个按钮为您执行相同的操作。。。
编辑:
由于您使用的是一个列表视图
,它是多回音的,我想您可以在onCreate中完成这项工作
listView = (ListView) findViewById(R.id.list);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
android.R.id.text1, names));
最后在onCreate中调用此
LoadSelections();
您可以创建一个0和1的字符串,并使用共享首选项存储它。您正在使用的复选框数(或视图数)将是字符串的长度。您可以相应地保存它 例如:
字符串s[]=“0000000000”;
对于(i=0;i在OnCLickListener
中)对于按钮添加以下内容:
//...
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.list);
dialog.setTitle("The List");
prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefsEditor = prefs.edit();
String currentlyStored = prefs.getString("checked_list", null);
int[] savedStatus = null;
if (currentlyStored != null) {
String[] tmp = currentlyStored.split(",");
savedStatus = new int[tmp.length];
for (int i = 0; i < tmp.length; i++) {
savedStatus[i] = Integer.parseInt(tmp[i]);
}
}
adapter = new MobileArrayAdapter(this, soundnames, savedStatus);
ListView mListView = (ListView) dialog.findViewById(R.id.myList);
mListView.setAdapter(mAdapter);
//...
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String toStore = "";
ArrayList<Boolean> status = adapter.getCheckedStatus();
for (int i = 0; i < status.size(); i++) {
if (status.get(i)) {
toStore += i + ",";
}
}
prefsEditor.putString("checked_list", toStore.equals("") ? null
: toStore.substring(0, toStore.length() - 1));
prefsEditor.commit();
dialog.dismiss();
}
});
类中的字段具有列表视图
(适配器
字段将保存您在列表中设置的适配器对象)
修改自定义适配器的构造函数,如下所示:
public MobileArrayAdapter(Context context, String[] values,
int[] oldStatus) {
super(context, R.layout.adapters_simpleplay_row, values);
this.context = context;
this.values = values;
// make every CheckBox unchecked and then loop through oldStatus(if
// not null)
for (int i = 0; i < this.getCount(); i++) {
itemChecked.add(i, false);
}
if (oldStatus != null) {
for (int j = 0; j < oldStatus.length; j++) {
itemChecked.set(oldStatus[j], true);
}
}
}
对话框按钮的最后一个侦听器添加以下内容:
//...
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.list);
dialog.setTitle("The List");
prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefsEditor = prefs.edit();
String currentlyStored = prefs.getString("checked_list", null);
int[] savedStatus = null;
if (currentlyStored != null) {
String[] tmp = currentlyStored.split(",");
savedStatus = new int[tmp.length];
for (int i = 0; i < tmp.length; i++) {
savedStatus[i] = Integer.parseInt(tmp[i]);
}
}
adapter = new MobileArrayAdapter(this, soundnames, savedStatus);
ListView mListView = (ListView) dialog.findViewById(R.id.myList);
mListView.setAdapter(mAdapter);
//...
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String toStore = "";
ArrayList<Boolean> status = adapter.getCheckedStatus();
for (int i = 0; i < status.size(); i++) {
if (status.get(i)) {
toStore += i + ",";
}
}
prefsEditor.putString("checked_list", toStore.equals("") ? null
: toStore.substring(0, toStore.length() - 1));
prefsEditor.commit();
dialog.dismiss();
}
});
dialogButton.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串toStore=“”;
ArrayList status=adapter.getCheckedStatus();
对于(int i=0;i
我将把第一段代码放在哪里?我是否不需要在我的适配器的if语句中放置任何东西?我给了你一个关于你能做什么的抽象概念,你必须在设置适配器的活动中做我说的每一件事,遵循这个方法,它将解决你的问题。我将它放在我的主要活动im中猜猜?我只是想实现它,如果我把我的主要活动代码放在一起,你能帮我一把吗?很简单,只要试着把各个部分放在一起,我可以确认你有你想要做的一切…我最好在所有步骤上都需要帮助,因为我以前从未使用过共享首选项。如果你需要我放更多的代码,我可以,但我可以需要精确植入:)需要编写大量代码。有时需要。在此之前,您可以尝试此方法-生成一个静态字符串s[]=“00000000”。在getView()方法中添加视图时更新它。并将字符串保存在共享首选项中。启动应用程序时,使用此选项标记复选框。如果您得到它或有疑问,请进行注释。@carbon刺客soundnames是来自自定义适配器构造函数的字符串数组String[]values
。我已经编辑了我的答案。我得到了这个错误。类型首选项管理器中的方法GetDefaultSharedReferences(Context)不适用于参数(new View.OnClickListener(){})@Carbon刺客修改该行如下:GetDefaultSharedReferences(KevinandericaboxActivity.this)
我的应用程序现在在kevin.erica.box.KevinandericaboxActivity$5$1.onClick(KevinandericaboxActivity.java:190)@carbon刺客中,KevinandericaboxActivity
类中哪一行是190,导致nullpointerexecption崩溃?
private SharedPreferences prefs;
private SharedPreferences.Editor prefsEditor;
private MobileArrayAdapter adapter;
public MobileArrayAdapter(Context context, String[] values,
int[] oldStatus) {
super(context, R.layout.adapters_simpleplay_row, values);
this.context = context;
this.values = values;
// make every CheckBox unchecked and then loop through oldStatus(if
// not null)
for (int i = 0; i < this.getCount(); i++) {
itemChecked.add(i, false);
}
if (oldStatus != null) {
for (int j = 0; j < oldStatus.length; j++) {
itemChecked.set(oldStatus[j], true);
}
}
}
public ArrayList<Boolean> getCheckedStatus() {
return itemChecked;
}
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String toStore = "";
ArrayList<Boolean> status = adapter.getCheckedStatus();
for (int i = 0; i < status.size(); i++) {
if (status.get(i)) {
toStore += i + ",";
}
}
prefsEditor.putString("checked_list", toStore.equals("") ? null
: toStore.substring(0, toStore.length() - 1));
prefsEditor.commit();
dialog.dismiss();
}
});