如何在android中存储多个复选框值?
我有一个包含文本视图和复选框的列表。列表中有3项。现在,当用户单击列表项或复选框时,我正在使用如何在android中存储多个复选框值?,android,checkbox,Android,Checkbox,我有一个包含文本视图和复选框的列表。列表中有3项。现在,当用户单击列表项或复选框时,我正在使用setAllCall(),setNotContacts(),setAllContacts()方法将其值存储在变量中,以了解哪个列表项被激活或停用。这一切都在安卓系统中 现在,我在这方面面临两个问题: 1) 当我单击后退按钮或关闭应用程序并再次返回listview页面时,所有复选框值都消失了。我的意思是所有复选框值都变成默认值 2) 甚至我使用下面的代码存储激活或停用设置的变量也被销毁。因此,在其他一些活
setAllCall()
,setNotContacts()
,setAllContacts()
方法将其值存储在变量中,以了解哪个列表项被激活或停用。这一切都在安卓系统中
现在,我在这方面面临两个问题:
1) 当我单击后退按钮或关闭应用程序并再次返回listview页面时,所有复选框值都消失了。我的意思是所有复选框值都变成默认值
2) 甚至我使用下面的代码存储激活或停用设置的变量也被销毁。因此,在其他一些活动中,当我尝试检索这些变量值时,它们总是默认值
public View getView(int position, View convertView, ViewGroup parent) {
callBlockOptions callBlockOptions = (callBlockOptions) this.getItem( position );
CheckBox checkBox ;
TextView textView ;
if ( convertView == null ) {
convertView = inflater.inflate(R.layout.call_setting_list_item, null);
textView = (TextView) convertView.findViewById( R.id.rowTextView );
checkBox = (CheckBox) convertView.findViewById( R.id.CheckBox01 );
convertView.setTag( new CallViewHolder(textView,checkBox) );
checkBox.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
callBlockOptions callBlockOptions = (callBlockOptions) cb.getTag();
callBlockOptions.setChecked( cb.isChecked() );
String yo;
if(callBlockOptions.getPosition()=="0")
{
callBlockOptions.setAllCalls();
}
else if(callBlockOptions.getPosition()=="1")
{
callBlockOptions.setNotContacts();
}
else if(callBlockOptions.getPosition()=="2")
{
callBlockOptions.setAllContacts();
}
}
});
}
else {
CallViewHolder viewHolder = (CallViewHolder) convertView.getTag();
checkBox = viewHolder.getCheckBox() ;
textView = viewHolder.getTextView() ;
}
checkBox.setTag( callBlockOptions );
checkBox.setChecked( callBlockOptions.isChecked() );
textView.setText( callBlockOptions.getName() );
return convertView;
}
}
如果您需要其他代码,请告诉我
我在这里唯一想做的就是保留所有复选框值,并保留变量值,即使应用程序已关闭 您可以使用共享首选项来存储值。使用这样的代码-
public boolean getFromSP(String key){
SharedPreferences preferences = ctx.getSharedPreferences("Contacts", android.content.Context.MODE_PRIVATE);
return preferences.getBoolean(key, false);
}
private void saveInSp(String key,boolean value){
SharedPreferences preferences = ctx.getSharedPreferences("Contacts", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
holder.checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(((CheckBox)v).isChecked())
{
saveInSp("check"+position,true);
Log.i("pos", ""+position);
}
else
{
saveInSp("check"+position,false);
}
在getview方法中,按如下方式存储它-
public boolean getFromSP(String key){
SharedPreferences preferences = ctx.getSharedPreferences("Contacts", android.content.Context.MODE_PRIVATE);
return preferences.getBoolean(key, false);
}
private void saveInSp(String key,boolean value){
SharedPreferences preferences = ctx.getSharedPreferences("Contacts", android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
holder.checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(((CheckBox)v).isChecked())
{
saveInSp("check"+position,true);
Log.i("pos", ""+position);
}
else
{
saveInSp("check"+position,false);
}
要在加载listview时检索复选框的选中状态,请在getview方法中使用-
holder.checkBox.setChecked(getFromSP("check"+position));
i的价值是什么。它如何知道单击了哪个复选框以及为什么返回preferences.getBoolean(key,false);始终返回falsei=position(getview methiod的属性)。If(((CheckBox)v).isChecked()将告诉您是否选中了复选框,并且相应的位置值(i值)为您提供了复选框checkedKey的位置……但是为什么要返回preferences.getBoolean(key,false);总是返回false您需要从适配器的getview方法调用getFromSp方法,如-holder.checkBox.setChecked(getFromSp(“check”+position));获取错误时不能引用内部类中的非最终变量以获取位置