1

我有一个自定义列表视图,其中的每一行都由文本视图和复选框组成。(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)

任何帮助将不胜感激

4

3 回答 3

1

正如我在评论中所说:您尝试从您的复选框中获取标签,但您从未设置标签。所以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;
}
于 2012-04-06T02:05:04.990 回答
0

在下一行

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

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

于 2012-04-06T01:49:33.317 回答
0

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

于 2012-04-05T22:59:25.493 回答