如果我有一个listview扩展列表活动,并创建一个检查列表(simple_item_checked)。使用列表首选项保存选中的值或共享首选项以保存值是否更好?
我对设置列表视图活动的共享首选项也有一点疑问。我使用getInt和getBoolean将列表项行位置和该行的布尔值(已检查或未检查)传递给共享首选项。这样对吗?或者我是否应该包含或传递除此之外的任何其他参数以确保检查正确的行?有人请帮助我。
我对列表首选项有疑问,因为据我所见,列表首选项仅用于设置屏幕。像我们的Android设置屏幕。我们是否也可以使用相同的列表项来检查列表中项目的已检查状态。
public class Secondact extends ListActivity {
ListView list;
SharedPreferences pref;
int pos,itemposition,positionvalue;
boolean boolval,checkvalue;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.secondact);
Toast.makeText(getApplicationContext(), "ONCREATE", 50).show();
list=getListView();
String[] family_array=this.getResources().getStringArray(R.array.family);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,family_array);
list.setAdapter(adapter);
list.setChoiceMode(1);
pref=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
pos=pref.getInt("itempos", pos);
boolval=pref.getBoolean("boolvalue", true);
list.setItemChecked(pos, boolval);
Toast.makeText(getApplicationContext(), "DATA LOADED ITEM NO:"+pos, 50).show();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
pos=position;
list.setItemChecked(pos, true);
pref=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
editor=pref.edit();
editor.putInt("itempos", pos);
editor.putBoolean("boolvalue", list.isItemChecked(pos));
editor.commit();
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
pref=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
editor=pref.edit();
editor.putInt("itempos", pos);
editor.putBoolean("boolvalue", list.isItemChecked(pos));
editor.commit();
// Toast.makeText(getApplicationContext(), "PAUSE", 50).show();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
pref=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
pos=pref.getInt("itempos", pos);
boolval=pref.getBoolean("boolvalue", true);
list.setItemChecked(pos, boolval);
//Toast.makeText(getApplicationContext(), "RESUME", 50).show();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
pref=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
editor=pref.edit();
editor.putInt("itempos", pos);
editor.putBoolean("boolvalue", list.isItemChecked(pos));
editor.commit();
Toast.makeText(getApplicationContext(), "DESTROY", 50).show();
Toast.makeText(getApplicationContext(), "DATA SAVED ITEM NO:"+pos, 50).show();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
pref=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
pos=pref.getInt("itempos", pos);
boolval=pref.getBoolean("boolvalue", true);
list.setItemChecked(pos, boolval);
//Toast.makeText(getApplicationContext(), "RESTART", 50).show();
}
@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
savedInstanceState.putInt("itempositionno", positionvalue);
savedInstanceState.putBoolean("checkvalue", true);
Toast.makeText(getApplicationContext(), "ONSAVEINSTANCE", 50).show();
list.setItemChecked(pos, checkvalue);
super.onSaveInstanceState(savedInstanceState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
pos=savedInstanceState.getInt("itempositionno", pos);
checkvalue=savedInstanceState.getBoolean("checkvalue", true);
list.setItemChecked(pos, checkvalue);
Toast.makeText(getApplicationContext(), "ONRESTOREINSTANCE", 50).show();
super.onRestoreInstanceState(savedInstanceState);
}
}
答案 0 :(得分:0)
您无法在此处使用ListPreference。 您可以将首选项列表保存为文件,也可以使用共享首选项。 最好使用sharedPreference,因为它非常快速和简单。
private ArrayList<Boolean> checkList;
SharedPreferences pref=getSharedPreferences("shared_check_prefs",Context.MODE_PRIVATE);
SharedPreferences.Editor edit=pref.edit();
for(int i=0;i<number;i++){
edit.putBoolean("CECK_PREFS"+i, value);
edit.commit();
}
获取row = i复选框首选项的代码将是
SharedPreferences pref = getSharedPreferences(&#34; shared_check_prefs&#34;,Context.MODE_PRIVATE); pref.getBoolean(&#34; CECK_PREFS&#34; + 1,假);