当活动发生变化时,checkBox的状态会自动更改(使用共享首选项存储)

时间:2015-04-10 13:33:19

标签: android android-listview sharedpreferences android-checkbox

我有两项活动MainActivityFilterActivity,在FilterActivity我使用自定义适配器创建了一个包含row(Textview,Checkbox)的列表视图。

在自定义适配器中,我使用共享首选项来存储复选框的状态,请参阅下面的代码。

public class filterCustomAdapter extends ArrayAdapter<Application>
{
    private final List<Application> list;
    Context context;
    Editor editor;
    ViewHolder viewHolder;
    private LayoutInflater inflater;
    Set<String> set =new HashSet<String>();

    public filterCustomAdapter(Context context, List<Application> list) 
    {
        super(context, R.layout.fil_list_items, list);
        this.context = context;
        this.list = list;
        inflater=LayoutInflater.from(context);
    }

    public int getCount() 
    {
        return list.size();

    }

    public Application getItem(int position)
    {
        return list.get(position);
    }

    public long getItemId(int position) 
    {
        return position;
    }


    static class ViewHolder
    {
        protected TextView text;
        protected CheckBox checkbox;
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
        View view = convertView;
        final Application item=list.get(position);

          view = inflater.inflate(R.layout.fil_list_items, null);

          viewHolder = new ViewHolder();
          viewHolder.text = (TextView) view.findViewById(R.id.fil_txt);
          viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);

          view.setTag(viewHolder);

        /*else
        {
            viewHolder=(ViewHolder)convertView.getTag();
        }*/
        viewHolder.text.setText(list.get(position).getApp_name());

        SharedPreferences settings = context.getSharedPreferences("data",Context.MODE_PRIVATE);
        boolean Checked = settings.getBoolean(item.getApp_name(),true);
        viewHolder.checkbox.setChecked(Checked);

        viewHolder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() 
        {
            @SuppressLint("NewApi")
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
            {
                if(viewHolder.checkbox.isChecked()==true)
                {
                    SharedPreferences settings = context.getSharedPreferences("data", Context.MODE_PRIVATE);
                    settings.edit().putBoolean(item.getApp_name(),true).commit();
                    //set.add(list.get(position).getApp_name());
                    //settings.edit().putStringSet("selected_app_name",set);

                }
                else
                {
                    SharedPreferences settings = context.getSharedPreferences("data", Context.MODE_PRIVATE);
                    settings.edit().putBoolean(item.getApp_name(),false).commit();
                    //set.remove(list.get(position).getApp_name());
                    //settings.edit().putStringSet("selected_app_name",set);
                }

            }
        });
        return view;
      } 
}

我注释掉了一些部分,这些部分将所选项目的名称存储在集合中,该部分再次存储在共享首选项中(也不存在)。

我的问题是,每当我切换活动时,都会随机选中并取消选中复选框。我正在处理此事4个小时没有运气。请帮助。提前谢谢。

更新:这是我的filterActivity代码:

public class FilterActivity extends Activity
{
	ListView listview;
	@Override
    public void onCreate(Bundle savedInstanceState) 
	{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.filter);
        
        listview = (ListView) findViewById(R.id.filter_item_list); 
        ArrayAdapter<Application> adapter = new filterCustomAdapter(this,getItems());
        listview.setAdapter(adapter);
       
        
        Intent i = getIntent();
        String text=i.getStringExtra("activity");
       
	}
	private List<Application> getItems() 
	 {
		    List<Application> list = new ArrayList<Application>();
		    list.add(get("Message"));
		    list.add(get("Mail"));
		    list.add(get("Reminder"));
		    list.add(get("Event"));
		    list.add(get("Contact"));
		    list.add(get("Application"));
		    list.add(get("Music"));
		    list.add(get("Video"));
		    list.add(get("Search Web"));
		    return list;
	}
	private Application get(String s)
	{
		return new Application(s,true,1);
	}
}

只要点击一个按钮,我就会从MainActivity调用此活动。

1 个答案:

答案 0 :(得分:0)

请查看此代码并修改您的代码。

 public class MyAdapter extends ArrayAdapter<Model> {

private final List<Model> list;
private final Activity context;
boolean checkAll_flag = false;
boolean checkItem_flag = false;

public MyAdapter(Activity context, List<Model> list) {
    super(context, R.layout.row, list);
    this.context = context;
    this.list = list;
}

static class ViewHolder {
    protected TextView text;
    protected CheckBox checkbox;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        convertView = inflator.inflate(R.layout.row, null);
        viewHolder = new ViewHolder();
        viewHolder.text = (TextView) convertView.findViewById(R.id.label);
        viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
        viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
            }
        });
        convertView.setTag(viewHolder);
        convertView.setTag(R.id.label, viewHolder.text);
        convertView.setTag(R.id.check, viewHolder.checkbox);
        } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    viewHolder.checkbox.setTag(position); // This line is important.

    viewHolder.text.setText(list.get(position).getName());
    viewHolder.checkbox.setChecked(list.get(position).isSelected());

    return convertView;
}

}