【问题标题】:Custom List view (checkbox issue) ...setOnCheckedChangeListener自定义列表视图(复选框问题) ...setOnCheckedChangeListener
【发布时间】:2023-03-13 06:58:02
【问题描述】:

我有一个自定义列表视图,其中的每一行都由文本视图和复选框组成。 (1)当我点击列表中的项目时,它应该转到其他活动 和(2)当复选框被选中时,文本视图中的值应该被添加到数组中//当它被取消选中时,应该从数组中删除值。 第一个要求与我一起成功运行,但第二个要求不起作用。 这是我的代码:

public class DataAdapter extends ArrayAdapter<ItemInList> {


public ArrayList<ItemInList> list;

public Activity context;
public LayoutInflater inflater;
public static ArrayList<String> array=new ArrayList<String>();
ItemInList element=new ItemInList();

public DataAdapter(Activity context,int x,ArrayList<ItemInList> list) {
    super(context, 0, list);
    this.context = context;
    this.list = list;
    this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}


static class ViewHolder {
    protected TextView name,Description;
    protected CheckBox checkbox;
}

public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

@Override
public ItemInList getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

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

   final ViewHolder holder;



    if (convertView == null) {

         holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.row1, null);


        holder.name = (TextView) convertView.findViewById(R.id.food_title);
        holder.name.setTextColor(Color.BLACK);

        holder.Description = (TextView) convertView.findViewById(R.id.food_description);
        holder.Description.setTextColor(Color.GRAY);

        holder.checkbox = (CheckBox) convertView.findViewById(R.id.add_food_item);


        holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                        element = (ItemInList) holder.checkbox.getTag();
                        element.setSelected(buttonView.isChecked());
                            if(element.isSelected())
                            {
                                array.add(element.getName());
                            }
                            else
                            {
                                array.remove(position);
                            }
                                            }
                });
         convertView.setTag(holder);


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

            ItemInList bean = (ItemInList) list.get(position);

            holder.name.setText( bean.getName());
            holder.Description.setText( bean.getDescription()+"");
            holder.checkbox.setChecked( bean.isSelected());

              return convertView;
    }



    holder.name.setText(list.get(position).getName());
    holder.Description.setText(list.get(position).getDescription()+"");
    holder.checkbox.setChecked(list.get(position).isSelected());

      return convertView;
}

错误:

 null pointer exception..
                                at  DataAdapter$1.onCheckedChanged(DataAdapter.java:92)
    E/AndroidRuntime(543):  at android.widget.CompoundButton.setChecked(CompoundButton.java:125)
    E/AndroidRuntime(543):  at sehaty.com.DataAdapter$1.onCheckedChanged(DataAdapter.java:92)
   E/AndroidRuntime(543):   at android.widget.CompoundButton.setChecked(CompoundButton.java:125)
                                at android.widget.CompoundButton.performClick(CompoundButton.java:99)

任何帮助将不胜感激

【问题讨论】:

  • 可以在调用holder.checkbox.getTag() 之前查看调用 setTag 的位置。所以那一定是空的
  • 请您再解释一下好吗?

标签: android listview checkbox


【解决方案1】:

正如我在 cmets 中所述:您尝试从复选框中获取标签,但您从未设置标签。所以element 必须为空。这样你就得到了一个 NullPointer。问题是..你为什么要尝试获取标签?无需使用该方法(如果我正确理解您的意图)。您想访问相应职位的 element 变量。该元素在您的列表中,因此您可以尝试以下操作(代码未经测试..只是从您那里复制并进行了一些更改):

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

final ViewHolder holder;

   if (convertView == null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.row1, null);
        holder.name = (TextView) convertView.findViewById(R.id.food_title);
        holder.name.setTextColor(Color.BLACK);
        holder.checkbox = (CheckBox) convertView
                .findViewById(R.id.add_food_item);
        convertView.setTag(holder);

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

    final ItemInList element = list.get(position);
    holder.name.setText(element.getName());
    holder.checkbox.setChecked(element.isSelected());
    holder.checkbox
            .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    element.setSelected(buttonView.isChecked());

                    if (element.isSelected()) {
                        array.add(element.getName());
                    } else {
                        if (position < array.size())
                            array.remove(position);
                    }
                }
            });

    return convertView;
}

【讨论】:

    【解决方案2】:

    检查 DataAdapter 类中的第 92 行。看起来适配器(或其他 var?)为空。您是否实例化了所有对象?如果这没有帮助,请发布您的完整代码,包括 XML。

    【讨论】:

    • 92 指向这一行 :element.setSelected(buttonView.isChecked());
    • elementbuttonView 均为空。在调试器中单步执行并确保它们不为空。
    • element 必须为空,因为您调用了holder.checkbox.getTag(),但您从未通过setTag(...) 设置它......正如上面评论中已经说过的......
    【解决方案3】:

    在下面一行

    element = (ItemInList) holder.checkbox.getTag();
    

    您正在尝试从复选框中获取标签,该标签未在 getView() 中的任何位置设置。结果它返回 null。

    【讨论】:

      猜你喜欢
      • 2012-12-15
      • 2011-07-23
      • 2011-10-28
      • 1970-01-01
      • 2016-10-23
      • 2016-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多