0

問題はこれです:

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

なぜそれが起こるのですか?「メディア」の近くをチェックしてノードを展開すると、チェックマークが2番目の子に移動します。理由がわかりません

EDIT1:チェックボックスの状態を解決しました。別の問題が発生します!!

しかし、問題がチェックボックスの状態ではないことを理解したばかりかもしれません!!! 問題は、切り替わる行の位置です。ビューがロードされたときにチェックするために配列を配置すると、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です!!! したがって、チェックボックスの問題は発生しません。親の位置の問題です!! あなたはどう思いますか?

これは新しいコードです:

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はグループ内で一意です)。
  • できれば、hasStableIds()をオーバーライドし、安定したIDを約束できる場合はtrueに設定してください。

ExpandableListViewでのリサイクルは少し奇妙であり、ユーザー側で追加の作業が必要になる可能性があることに注意してください。ドキュメントから:

可能であれば、 convertViewは古いビューを再利用します。使用する前に、このビューがnullでなく、適切なタイプであることを確認する必要があります。このビューを変換して正しいデータを表示できない場合、このメソッドは新しいビューを作成できます。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 に答える