在ListView中存储CheckBox状态

时间:2014-03-23 09:31:59

标签: android android-listview

我在每个列表项中都有一个ListView CheckBox,我用SharedPreferences存储了检查状态,但是每次滚动选中一个项目时,每个项目都未选中,或者选中,取决于我调用checkbox.setChecked()时的默认值; ListViewAdapter.java

public class ListViewAdapter extends BaseAdapter {

Context context;
AppPicker.Package[] packagesForAdapter;
SharedPreferences.Editor editor;
private Intent blackListIntent;

public ListViewAdapter(Context context, AppPicker.Package packages[]) {
    this.context = context;
    this.packagesForAdapter = packages;
}

@Override
public int getCount() {
    return packagesForAdapter.length;
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public long getItemId(int i) {
    return 0;
}

static class ViewHolder {
    TextView text;
    ImageView icon;
    CheckBox checkBox;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    SharedPreferences sharedPreferences = context.getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
    ViewHolder viewHolder;
    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(convertView == null) {
        convertView = layoutInflater.inflate(R.layout.list_item, null);
        viewHolder = new ViewHolder();
        viewHolder.text = (TextView) convertView.findViewById(R.id.icon_text);
        viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon_image);
        viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.icon_check);

        convertView.setTag(viewHolder);
    }else{
        viewHolder = (ViewHolder) convertView.getTag();
    }

    editor = sharedPreferences.edit();

    viewHolder.icon.setImageDrawable(packagesForAdapter[position].icon);
    viewHolder.text.setText(packagesForAdapter[position].label);
    //viewHolder.checkBox.setChecked(sharedPreferences.getBoolean("CheckValue" + position, false));
    viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        int pos = position;
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            if(isChecked) {
                Toast.makeText(context, "Item checked: " + pos + " App: " + packagesForAdapter[pos].name, Toast.LENGTH_SHORT).show();
                blackListIntent = new Intent("com.jason.floating.notification.appPickerFragment.ListViewAdapter");
                blackListIntent.putExtra("packageName", packagesForAdapter[pos].name);
            }else{
                Toast.makeText(context, "Item unchecked: " + pos + " App: " + packagesForAdapter[pos].name, Toast.LENGTH_SHORT).show();
                blackListIntent.removeExtra("packageName");
            }
            context.sendBroadcast(blackListIntent);
            editor.putBoolean("CheckValue" + pos, isChecked);
            editor.commit();
        }
    });

    return convertView;
}

}

任何想法请帮助,因为我一直在寻找互联网但没有运气/ _ \

编辑: 修改后的代码 公共类ListViewAdapter扩展了BaseAdapter {

Context context;
AppPicker.Package[] packagesForAdapter;
SharedPreferences.Editor editor;
private Intent blackListIntent;
boolean isChecked;

public ListViewAdapter(Context context, AppPicker.Package packages[]) {
    this.context = context;
    this.packagesForAdapter = packages;
}

@Override
public int getCount() {
    return packagesForAdapter.length;
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public long getItemId(int i) {
    return 0;
}

static class ViewHolder {
    TextView text;
    ImageView icon;
    CheckBox checkBox;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    ViewHolder viewHolder;
    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(convertView == null) {
        convertView = layoutInflater.inflate(R.layout.list_item, null);
        viewHolder = new ViewHolder();
        viewHolder.text = (TextView) convertView.findViewById(R.id.icon_text);
        viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon_image);
        viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.icon_check);

        convertView.setTag(viewHolder);
    }else{
        viewHolder = (ViewHolder) convertView.getTag();
    }

    editor = sharedPreferences.edit();

    viewHolder.icon.setImageDrawable(packagesForAdapter[position].icon);
    viewHolder.text.setText(packagesForAdapter[position].label);
    viewHolder.checkBox.setChecked(packagesForAdapter[position].isChecked);
    viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        int pos = position;
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if(b) {
                Toast.makeText(context, "Item checked: " + pos + " App: " + packagesForAdapter[pos].name, Toast.LENGTH_SHORT).show();
                blackListIntent = new Intent("com.jason.floating.notification.appPickerFragment.ListViewAdapter");
                blackListIntent.putExtra("packageName", packagesForAdapter[pos].name);
                packagesForAdapter[pos].isChecked = true;
                editor.commit();
            }else{
                Toast.makeText(context, "Item unchecked: " + pos + " App: " + packagesForAdapter[pos].name, Toast.LENGTH_SHORT).show();
                blackListIntent.removeExtra("packageName");
                packagesForAdapter[pos].isChecked = false;
                editor.commit();
            }
            context.sendBroadcast(blackListIntent);
            //editor.putBoolean("CheckValue" + pos, isChecked);
            //editor.commit();
        }
    });

    return convertView;
}

}

EDIT2: 修改后的代码 公共类ListViewAdapter扩展了BaseAdapter {

Context context;
AppPicker.Package[] packagesForAdapter;
SharedPreferences.Editor editor;
private Intent blackListIntent;
boolean isChecked;

public ListViewAdapter(Context context, AppPicker.Package packages[]) {
    this.context = context;
    this.packagesForAdapter = packages;
}

@Override
public int getCount() {
    return packagesForAdapter.length;
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public long getItemId(int i) {
    return 0;
}

static class ViewHolder {
    TextView text;
    ImageView icon;
    CheckBox checkBox;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    ViewHolder viewHolder;
    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(convertView == null) {
        convertView = layoutInflater.inflate(R.layout.list_item, null);
        viewHolder = new ViewHolder();
        viewHolder.text = (TextView) convertView.findViewById(R.id.icon_text);
        viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon_image);
        viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.icon_check);

        convertView.setTag(viewHolder);
    }else{
        viewHolder = (ViewHolder) convertView.getTag();
    }

    editor = sharedPreferences.edit();

    viewHolder.icon.setImageDrawable(packagesForAdapter[position].icon);
    viewHolder.text.setText(packagesForAdapter[position].label);
    viewHolder.checkBox.setChecked(packagesForAdapter[position].isChecked);
    viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            /**
            if(isCheck) {
                Toast.makeText(context, "Item checked: " + pos + " App: " + packagesForAdapter[pos].name, Toast.LENGTH_SHORT).show();
                blackListIntent = new Intent("com.jason.floating.notification.appPickerFragment.ListViewAdapter");
                blackListIntent.putExtra("packageName", packagesForAdapter[pos].name);
                packagesForAdapter[pos].isChecked = true;
            }else{
                Toast.makeText(context, "Item unchecked: " + pos + " App: " + packagesForAdapter[pos].name, Toast.LENGTH_SHORT).show();
                blackListIntent.removeExtra("packageName");
                packagesForAdapter[pos].isChecked = false;
            }
            context.sendBroadcast(blackListIntent);
            editor.putBoolean("CheckValue" + pos, isChecked);
            editor.commit();
            packagesForAdapter[pos].isChecked = isCheck;
             **/
            packagesForAdapter[position].isChecked = isChecked;
        }
    });

    return convertView;
}

}

4 个答案:

答案 0 :(得分:1)

您应该在类AppPicker.Package中定义一个名为isChecked的变量。

因此,在您的setOnCheckedChangeListener中,您将在AppPicker.Package [pos]中存储已检查状态.isChecked

然后,在你的getView方法中:
你会调用:viewHolder.checkBox.setChecked(AppPicker.Package [pos] .isChecked)//这样的东西。

答案 1 :(得分:1)

有很多方法可以做到这一点。

更新: 您需要一种方法来查找packagesForAdapter中每个项目的复选框。你创建它的id,它可以是任何独特的。

例如,在创建数据时使用计数器添加到packagesForAdapter并使用它。

String anIdForThisCheckbox = packagesForAdapter[position].id; 

在你的getView()

...
final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
boolean isChecked = pref.getBoolean(anIdForThisCheckbox,  false);
viewHolder.checkBox.setChecked(isChecked );
 ...

为您的复选框创建一个onClickListener:

viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Editor editor = pref.edit();

        if (viewHolder.checkBox.isChecked()) {//Selected
            editor.putBoolean(anIdForThisCheckbox, true);//save state 

        } else {
            editor.putBoolean(anIdForThisCheckbox, false);
        }
    }
    editor.apply/commit
});
...

答案 2 :(得分:1)

您将isChecked添加为AppPicker.Package的类成员。

然后:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    SharedPreferences sharedPreferences = context.getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
    ViewHolder viewHolder;
    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(convertView == null) {
        convertView = layoutInflater.inflate(R.layout.list_item, null);
        viewHolder = new ViewHolder();
        viewHolder.text = (TextView) convertView.findViewById(R.id.icon_text);
        viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon_image);
        viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.icon_check);

        convertView.setTag(viewHolder);
    }else{
        viewHolder = (ViewHolder) convertView.getTag();
    }

    editor = sharedPreferences.edit();

    viewHolder.icon.setImageDrawable(packagesForAdapter[position].icon);
    viewHolder.text.setText(packagesForAdapter[position].label);
    viewHolder.checkBox.setChecked(packagesForAdapter[position].isChecked);// add by tauitdnmd
    viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

            packagesForAdapter[position].isChecked = isChecked; // add by tauitdnmd

        }
    });

    return convertView;
}

答案 3 :(得分:1)

感谢所有人的支持。但我终于找到了解决这个恼人问题的解决方案。使用模型存储复选框的状态,每次listview更新时获取值,并设置复选框的选中状态。我花了一些时间。