Reputation: 95
I've a list which contains a textview and a checkbox. There are 3 items in the list. Now when user clicks a list item or checkbox i'm storing its value in an variable using setAllCall()
, setNotContacts()
, setAllContacts()
method, to know that which list item was activated or deactivated. Its all in android.
Now, i'm facing two problems in these:
1) When I click back button or close the application and come back to the listview page again all the checkbox values are gone. I mean all the checkbox values become defaults.
2) Even the variables in which i've stored the activation or deactivation of the settings using below code is destroyed. So, in some other activity when i try to retrieve those variable values are always default.
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;
}
}
Let me know if u need some other code!
Only thing i want to do here is to retain all the checkbox values and to retain the variable values even if the app is closed!
Upvotes: 1
Views: 2946
Reputation: 3362
You can use shared preferences to store the values. Use code like this -
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();
}
And in your getview method store it like this -
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);
}
And to retrieve the checked state of checkbox when loading the listview, use this in your getview method -
holder.checkBox.setChecked(getFromSP("check"+position));
Upvotes: 2