2012-04-05 103 views
1

我有一个自定义列表视图中的每行包含textview和复选框。 (1)当我点击列表中的项目时,它应该转到其他活动 和(2)当复选框被选中时,textview中的值应该被添加到数组中//当它未被选中时,值应该从阵列。 第一个要求对我成功运行,但第二个要求不起作用。 这是我的代码:自定义列表视图(复选框问题)... setOnCheckedChangeListener

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) 

任何帮助,将在您的DataAdapter类可以理解

+0

能看到你打电话setTag你叫'holder.checkbox之前。 getTag()'。那么必须为空 – 207 2012-04-05 23:01:44

+0

你能否再解释一下? – user2012 2012-04-05 23:38:18

回答

1

正如我在评论中所述:您尝试从复选框中获取标签,但是您从未设置标签。所以element必须为空。因此你得到一个NullPointer。问题是......你为什么试图获得标签?没有必要使用这种方法(如果我正确理解你的意图)。你想访问你的element变量的相应位置。这个元素在你的列表中,这样你可以试试以下(代码不tested..just从您复制并做了一些改动):

@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; 
} 
0

检查线92。它看起来像适配器(或其他var?)为空。你是否实例化了所有对象?如果这没有帮助,请发布包含XML的完整代码。

+0

添加setOnCheckedChangeListener后出现错误。 – user2012 2012-04-05 23:08:55

+0

92指向此行:element.setSelected(buttonView.isChecked()); – user2012 2012-04-05 23:10:59

+0

'element'或'buttonView'都是null。在调试器中逐步完成并确保它们非空。 – kcoppock 2012-04-05 23:16:56

0

在下面一行

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

你想从复选框一个标签,这在getView不崩位置设置过()。结果它返回null。

相关问题