自定义列表视图(复选框问题)... setOnCheckedChangeListener

时间:2012-04-05 22:50:42

标签: android listview checkbox

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

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)

任何帮助将不胜感激

3 个答案:

答案 0 :(得分:1)

正如我在评论中所述:您尝试从复选框中获取标记,但您从未设置标记。所以element必须为null。因此你得到一个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;
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

在以下一行

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

您正试图从复选框中获取一个标记,该标记不是在getView()中的任何位置设置的。结果它返回null。