列表视图中的一些子项为空

时间:2015-06-20 18:06:12

标签: android android-listview android-adapter

我在listview中遇到了适配器问题。如果我尝试在可见屏幕下获取元素(使用metod list.getChildAt(index)),则应用程序返回空对象引用。 适配器代码:

public class CustomAdapter extends ArrayAdapter<GiocatoreRosa> implements CompoundButton.OnCheckedChangeListener {
     private LayoutInflater inflater;
     private int resource;
     public SparseBooleanArray mCheckStates;

 public CustomAdapter(Context context, int resourceId, ArrayList<GiocatoreRosa> objects) {
         super(context, resourceId, objects);
         resource = resourceId;
         inflater = LayoutInflater.from(context);
         mCheckStates = new SparseBooleanArray(objects.size());
 }

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

         // Recuperiamo l'oggetti che dobbiamo inserire a questa posizione
     GiocatoreRosa p = getItem(position);
     System.out.println(position);

     ViewHolder holder;

         if (v == null) {
             v = inflater.inflate(resource, parent, false);
                 holder = new ViewHolder();
                 holder.nome = (TextView) v.findViewById(R.id.giocatore);
                 holder.chk = (CheckBox) v.findViewById(R.id.checkBox);
                 if(p.getRuolo().equals("P"))
                     v.setBackgroundResource(R.drawable.shape_gray);
                 if(p.getRuolo().equals("D"))
                     v.setBackgroundResource(R.drawable.shape_blue);
                 if(p.getRuolo().equals("C"))
                     v.setBackgroundResource(R.drawable.shape_green);
                 if(p.getRuolo().equals("A"))
                     v.setBackgroundResource(R.drawable.shape_red);
                 v.setTag(holder);
         } else {
                 holder = (ViewHolder) v.getTag();

         }

         holder.nome.setText(p.getNome());
         holder.chk.setTag(position);
         holder.chk.setChecked(mCheckStates.get(position, false));
         holder.chk.setOnCheckedChangeListener(this);

         return v;
 }

 public boolean isChecked(int position) {
     return mCheckStates.get(position, false);
 }

 public void setChecked(int position, boolean isChecked) {
     mCheckStates.put(position, isChecked);

 }

 public void toggle(int position) {
     setChecked(position, !isChecked(position));

 }

 @Override
 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

      mCheckStates.put((Integer) buttonView.getTag(), isChecked);    

 }

 private static class ViewHolder {
         TextView nome;
         CheckBox chk;
 }

}

我调用list.getChildAt(index)的代码是:

        for(int i=0;i<list.getCount();i++){
                View child= (View) list.getChildAt(i);
                ArrayList<View> touchablesElement=child.getTouchables();
                    if (touchablesElement.get(0) instanceof CheckBox) {
                                CheckBox c=(CheckBox) touchablesElement.get(0);
                                if(c.isChecked())  {
                                    addPlayerToDb(adapter.getItem(i));
                                    c.setChecked(false);
                                }
                      }
                }

0 个答案:

没有答案