0

问题是这样的:

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

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

EDIT1:解决了复选框状态。又出现问题!!

但是,也许我刚刚明白问题不在于复选框状态!!!问题是切换的行的位置!如果我放置一个数组来检查何时刚刚加载视图,则复选框状态运行良好!问题是视图位置!这就是发生的事情:

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 行!所以它不会出现复选框问题。是家长位置问题!!你在想什么?

这是新代码:

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);
    }
}

}

4

2 回答 2

0

可能是由于视图回收。您是否在 getGroupView 中使用转换视图?

确保一些事情

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

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

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

于 2012-02-20T13:17:51.387 回答
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);
                  }
 }
}); 
于 2012-02-21T09:11:00.457 回答