2012-04-05 102 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。

相關問題