Android:自定义可扩展列表和CheckBox问题。为什么复选框状态随机?

时间:2012-02-20 13:04:37

标签: android checkbox expandablelistview baseadapter

问题是:

http://img204.imageshack.us/img204/6071/immaginety.png

为什么会这样? 如果我在“媒体”附近检查并展开节点,则选中标记会转到第二个孩子。 我不明白为什么

EDIT1 :解决了checkBox状态。另一个问题发生!!

但是,也许我刚才明白问题不是checkBox状态!!!问题是切换行的位置!如果我把一个数组用于检查刚加载视图时,checkBox状态运行良好!问题是观点位置!这就是:

row0: parentA + checkBoxMARKED

row1: parentB + checkBoxNOTmarked

点击A,这应该发生:

row0:      parentA + checkBoxMARKED
 subRow0:  --childA
row1:      parentB + checkBoxNOTmarked

但相反,这种情况发生(父转换)

row1:    parentB + checkBoxNOTmarked
 subRow0:  --childA
row0:    parentA + checkBoxMarked 

现在,如果我评论这一行

if(usedPositionParent.contains(convertView.getId())){

          return convertView;
      }
      else{
          usedPositionParent.add(convertView.getId());
}

情况变为:

row0: parentA + checkBoxMARKED

row1: parentB + checkBoxNOTmarked

点击A,然后发生:

row1:     parentA + checkBoxNOTmarked
 subRow1: --childA
row0:     parentB + checkBoxMARKED

但实际上,parentA是第0行!所以它不会出现checkBox问题。这是父母职位问题!!你在想什么?

这是新代码:

public class ListaEspandibileAdapter extends BaseExpandableListAdapter  {

private ArrayList<Object> _objInt;
Context mContext;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

//HashMap<Integer,Boolean> checkboxMap = new HashMap<Integer,Boolean>();
CheckListener checkListner;

ArrayList<Integer> usedPositionParent = new ArrayList<Integer>();

CheckBox checkBox;

public ListaEspandibileAdapter(Context context, ArrayList<Object> objList){
    mContext = context;
    _objInt = objList;
    popolaCheckMap();
}   

public void popolaCheckMap(){

    for(int i=0; i< _objInt.size(); i++)
        checkboxMap.put(i, false);
}

@Override
public Object getChild(int arg0, int arg1) {

    return null;
}

@Override
public long getChildId(int groupPos, int childPos) {
    return childPos;
}

@Override
public View getChildView(int groupPos, int childPos, boolean arg2, View convertView,
        ViewGroup parent) { 

     if (convertView == null) {
            Log.i("childView", "my parent is "+ groupPos);
            LayoutInflater inflater =  (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.opportunita_cella_child, null);

            EditText descrizioneEditText = (EditText) convertView.findViewById(R.id.opportunita_child_descrizione);
            Intervento i = (Intervento) _objInt.get(groupPos);
            descrizioneEditText.setText(i.descrizione);
     }   

     return convertView;
}

@Override
public int getChildrenCount(int arg0) {
    // i have always one child!
    return 1;
}

@Override
public Object getGroup(int arg0) {

    return null;
}

@Override
public int getGroupCount() {

    return _objInt.size();
}

@Override
public long getGroupId(int groupPos) {

    return groupPos;
}   

@Override
public View getGroupView(int groupPos, boolean isExpanded, View convertView, ViewGroup parent) {


     if(convertView == null) {
            Log.i("parentView", "I'am "+ groupPos);
            LayoutInflater inflater =  (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.opportunita_cella, null);       
            convertView.setId(groupPos);

            checkBox = (CheckBox) convertView.findViewById(R.id.opportunita_checkbox);          
            checkBox.setFocusable(false); 
            CheckListener checkL = new CheckListener();
            checkL.setPosition(groupPos);
            checkBox.setOnCheckedChangeListener(checkL);

            Intervento intervento = (Intervento) _objInt.get(groupPos);
            ImageView imageView = (ImageView) convertView.findViewById(R.id.opportunita_image); 
            TextView tipoText = (TextView) convertView.findViewById(R.id.opportunita_tipo); 
            TextView oggettoText = (TextView) convertView.findViewById(R.id.opportunita_oggetto); 
            TextView urgenzaText = (TextView) convertView.findViewById(R.id.opportunita_urgenza); 
            TextView dataText = (TextView) convertView.findViewById(R.id.opportunita_data); 
            TextView oraText = (TextView) convertView.findViewById(R.id.opportunita_ora);    

            if(intervento.getTipo().equals("Guasto"))
                 imageView.setImageResource(R.drawable.ic_intervento);
            else if(intervento.getTipo().equals("Programmato"))
                 imageView.setImageResource(R.drawable.image_programmato);

            tipoText.setText(intervento.tipo);
            oggettoText.setText(intervento.oggetto);

            urgenzaText.setText(intervento.urgenza);
            if(intervento.urgenza.equals("Immediata"))
                 urgenzaText.setTextColor(Color.RED);
            else if(intervento.urgenza.equals("Alta"))
                 urgenzaText.setTextColor(Color.YELLOW);
            else if(intervento.urgenza.equals("Media"))
                 urgenzaText.setTextColor(Color.GREEN);
            else if(intervento.urgenza.equals("Bassa"))
                 urgenzaText.setTextColor(Color.WHITE);

            Date d = null;
            try {
                 d = sdf.parse(intervento.data);
            } catch (ParseException e) {

                 e.printStackTrace();
             }

            String yyyy = String.valueOf(d.getYear()+1900);

            String MM = String.valueOf(d.getMonth()+1);
            String gg = String.valueOf(d.getDate());
            String hh = String.valueOf(d.getHours());
            String mm = String.valueOf(d.getMinutes());

            if(Integer.parseInt(MM) <= 9)
                 MM = "0"+MM;
            if(Integer.parseInt(gg) <= 9)
                 gg = "0"+gg;
            if(Integer.parseInt(hh) <= 9)
                 hh = "0"+hh;
            if(Integer.parseInt(mm) <= 9)
                 mm = "0"+mm;

            dataText.setText(gg+"-"+MM+"-"+yyyy);
            oraText.setText(hh+":"+mm);
           }

      if(usedPositionParent.contains(convertView.getId())){
          //checkBox.setChecked(checkboxMap.get(groupPos));
          return convertView;
      }
      else{
          usedPositionParent.add(convertView.getId());
      }              

         //if(isExpanded)
            // checkBox.setChecked(checkboxMap.get(groupPos));

    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int arg0, int arg1) {
    return false;
}

public class CheckListener implements OnCheckedChangeListener{

    int pos;

    public void setPosition(int p){
        pos = p;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
        Log.i("checkListenerChanged", String.valueOf(pos)+":"+String.valueOf(isChecked));
        //checkboxMap.put(pos, isChecked);
    }
}

}

2 个答案:

答案 0 :(得分:0)

可能是因为查看回收。你在getGroupView中使用转换视图吗?

确定一些事情

  • 选中时保存复选框的状态,并在getGroupView中设置值。
  • 您实现getGroupId()和getChildId()以使每个组具有唯一且稳定的ID(子ID在组内是唯一的)。
  • 最好覆盖hasStableIds(),如果可以保证稳定ID,则设置为true。

请注意,ExpandableListView中的回收有点奇怪,可能需要您的一些额外工作。来自文档:

  如果可能的话,

convertView 将旧视图重用。在使用之前,您应该检查此视图是否为非null且具有适当的类型。如果   无法转换此视图以显示正确的数据,   此方法可以创建新视图。它不能保证   convertView以前是由getGroupView创建的(int,   boolean,View,ViewGroup)。

答案 1 :(得分:0)

使用它:

     ArrayList<Boolean> item_checked = new ArrayList<Boolean>();
     in starting item_checked.add(false)


     holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

 @Override
 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  // TODO Auto-generated method stub
   if (isChecked)
                  {
                      itemChecked.set(position, true);
                  }
                  else
                  {
                      itemChecked.set(position, false);
                  }
 }
});