ListView控件Viewholder复选框状态控件、复选框、状态、ListView

2023-09-12 10:09:34 作者:拿根辣条砸死你

我有一些问题,我的ListView自定义适配器(其新实施的viewHolder)。我有每个项目的复选框(没有什么新意)一个ListView。问题是,如果有超过9个项目在我的名单,当我检查第一个复选框,第十会自动检查(同为第二次与第11号),就像如果有一个侦听两个项目(和我beleive它以某种方式的情况下)。

I've some problems with my ListView custom adapter (and its newly implemented viewHolder). I have a ListView with a checkbox for each item (nothing new here). The problem is, if there is more than 9 items in my list, when I check the first checkbox, the tenth will be automatically checked (same for the second with the eleventh) just like if there were one listener for both item (and I beleive it's the case in some way).

我读到与ListView中的位置问题,查看回收及ViewHolder的方式在这里解决:How我可以让我的ArrayAdapter按照ViewHolder模式?

I read about the position issue with listView, view recycling and the ViewHolder way to solve it here: How can I make my ArrayAdapter follow the ViewHolder pattern?

不过,我可能做一些错误的,因为它不工作...

But I probably made something wrong because it's not working...

public class PresenceListAdapter extends SimpleAdapter {
private LayoutInflater  inflater;
private List<Integer> ids;
private List<String> statuts;

public PresenceListAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to, List<Integer> ids, List<String> statuts)
{
    super (context, data, resource, from, to);
    inflater = LayoutInflater.from (context);
    this.ids = ids;
    this.statuts= statuts;

}

@Override
public Object getItem (int position)
{
    return super.getItem (position);
}

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

    ViewHolder holder;

    if (convertView == null)
    {
        convertView = inflater.inflate (R.layout.list_text_checkbox, null);

        holder = new ViewHolder();

        holder.btn = (Button) convertView.findViewById(R.id.btnRetard);
        holder.check = (CheckBox) convertView.findViewById(R.id.checkPresent);

        if (statuts.get(position).equals("P")) {
            Drawable img = inflater.getContext().getResources().getDrawable(android.R.drawable.presence_online);
            holder.btn.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );
            holder.btn.setEnabled(true);
            holder.check.setChecked(true);
        }
        else if(statuts.get(position).equals("R"))
        {
            Drawable img = inflater.getContext().getResources().getDrawable(android.R.drawable.presence_away);
            holder.btn.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );
            holder.btn.setEnabled(true);
            holder.check.setChecked(true);
        }
        else 
        {
            Drawable img = inflater.getContext().getResources().getDrawable(android.R.drawable.presence_invisible);
            holder.btn.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );
            holder.check.setChecked(false);
        }

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

    int id = ids.get(position);

    if(id != 0)
    {
        holder.check.setTag(id);
        holder.btn.setTag(id);
    }

    return super.getView (position, convertView, parent);
}

static class ViewHolder {
    Button btn;
    CheckBox check;
}

和我的听众:         公共无效换present(视图v){

And my listener: public void changerPresent(View v) {

    CheckBox checkPresent = (CheckBox) v;
    int idPersonne = (Integer) checkPresent.getTag();
    View parent = (View)v.getParent();
    Button btn = (Button) parent.findViewById(R.id.btnRetard);

    if(checkPresent.isChecked()) {
        gestion.updatePresence(idPersonne, idSeance, "P");

        btn.setEnabled(true);
        setBtnRetardPresent(btn);           
    }
    else 
    {
        gestion.updatePresence(idPersonne, idSeance, "A");
        btn.setEnabled(false);
        setBtnRetardAbsent(btn);

    }

}

我会AP preciate在这一点上的任何帮助,我在这方面的工作了几个小时了。

I would appreciate any help at this point, I'm working on this for hours now.

非常感谢你。

推荐答案

这是一个很微妙的。我很惊讶的Andr​​oid还没有简化,这是如何实现的,到这一天 - 在列表视图复选框都应该是基本

This is a tricky one. I'm surprised Android has not yet simplified how this is implemented, up to this day - checkboxes on ListViews are supposed to be basic.

总之,这里是我如何使它的工作:

Anyway, here's how I made it work:

首先,您需要为您的选中状态一个单独的数组。它必须是大小相同的适配器 getCount将()

First, you need a separate array for your checked state. It has to be the same size as your adapter's getCount().

然后在你的getView,您的复选框的setOnCheckedChangedListener必须preCEED您checkbox.setChecked语句。

Then on your getView, your checkbox's setOnCheckedChangedListener MUST PRECEED your checkbox.setChecked statements.

例如:

(是器isChecked独立阵列我提到你的支票状态)

(isChecked is the separate array i mentioned for your check state)

holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  @Override
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    isChecked[position] = isChecked;
  }
});

holder.checkBox.setChecked(isChecked[position]);