2012-12-06 139 views
1

我有用於列表視圖定製arrayadapter,其內部包括複選框5次,在這裏,我要實現的選擇列表視圖中所有的複選框,並取消所有複選框,我試着用getchildat()方法在oncreate()onresume()onpostcreate也不過,對我沒有好運。如果你能告訴我解決方案,我很感激。選擇列表視圖中的所有複選框中的Android

@Override 
     public void onPostCreate(Bundle savedInstanceState){ 
      super.onPostCreate(savedInstanceState); 

     Button selectall=(Button) findViewById(R.id.allselect); 

     selectall.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      int count = listview.getCount(); 

      System.out.println("the count is "+count); 
      for (int i = 0; i < listview.getLastVisiblePosition() - listview.getFirstVisiblePosition(); i++) { 

       RelativeLayout itemLayout = (RelativeLayout)listview.getChildAt(i); 
       CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.chkbx); 
       cb.setChecked(true); 

      } 

     } 
    }); 
    } 

一個ArrayAdapter類

@SuppressWarnings("unchecked") 
    public MultipleLeadSyncAdapter(Context context, String[] values,String[] values1,List<Model> qrcode ,String[] values3,String[] values4,String[] values5) { 
     super(context, R.layout.multipleselectlist, values1); 
     this.context = context; 
     this.values = values; 
     this.values1 = values1; 
     this.list=qrcode; 
     this.values3=values3; 
     this.values4=values4; 
     this.eventid=values5; 
    } 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 


    ViewHolder holder =null; 
    if (convertView == null) { 


      convertView = inflater.inflate(R.layout.multipleselectlist, parent, false); 

      holder = new ViewHolder(); 
      holder.textView1 = (TextView) convertView.findViewById(R.id.multileadfirst_name); 

      holder.textView3 = (TextView) convertView 
        .findViewById(R.id.multileadcompany_name); 

      holder.rate=(RatingBar) convertView.findViewById(R.id.multileadlistrating); 

      holder.chkbox = (CheckBox) convertView.findViewById(R.id.chkbx); 

      holder.chkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        int getPosition = (Integer) buttonView.getTag(); 
        list.get(getPosition).setSelected(buttonView.isChecked()); 
       } 
      }); 

      convertView.setTag(holder); 
      convertView.setTag(R.id.chkbx,holder.chkbox); 
      convertView.setTag(R.id.multileadfirst_name,holder.textView1); 
      convertView.setTag(R.id.multileadcompany_name,holder.textView3); 
      convertView.setTag(R.id.multileadlistrating,holder.rate); 

    }else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 


    holder = (ViewHolder) convertView.getTag(); 


    holder.textView1.setText(values[position]); 

    if(values3[position].equals(" ")||values3[position].equals("null")||values3[position].equals("")){ 

    }else{ 

     String[] temp=values3[position].split(" "); 

    } 

    holder.textView3.setText(values1[position]); 

    holder.rate.setRating(Float.valueOf(values4[position]).floatValue()); 

    String s = values[position]; 
    String ss = values1[position]; 

    System.out.println(s + ss); 


    holder.chkbox.setTag(position); 

    //holder.textView3.setText(list.get(position).getName()); 
    holder.chkbox.setChecked(list.get(position).isSelected()); 
    String igurl=values[position]; 
    System.out.println("the imagurl is "+igurl); 

    return convertView; 



} 

class ViewHolder { 

    String qrcode,Boothid; 
    TextView textView1, textView2, textView3,textView4,textView5; 
    RatingBar rate; 
    ImageView imageView; 
    CheckBox chkbox; 

    public String getQrcode() { 
     return qrcode; 
    } 

    public void setQrcode(String qrcode) { 
     this.qrcode = qrcode; 
    } 

    public String getBoothID() { 
     return Boothid; 
    } 

    public void setBoothID(String Boothid) { 
     this.Boothid = Boothid; 
    } 

} 

}

回答

9

一個ListView僅僅是數據的表現。數據通過Adapter提供給ListView。這就是說,我可以解釋你做錯了什麼:

在你的OnClickListener裏面,你是(不)檢查ListView本身的View。這意味着數據未被更改,並且代表此數據的ListView不顯示想要的行爲。

一個簡單的解決辦法是增加一個布爾值,你Model -object這表明選中狀態:

Model { 
    boolean isChecked = false; 

    public void setChecked(boolean checked){ 
     isChecked = checked; 
    } 

    public boolean isChecked(){ 
     return isChecked; 
    } 
} 

每當你點擊「selectall'按鈕,你將簡單的切換這個布爾並通知他的數據已根據方法notifyDataSetChanged更改。這將在他輪到更新ListView本身。在僞代碼:

onClick(..) { 
    foreach (model inside my adapter){ 
     model.setChecked(true); 
    } 
    listview.getAdapter().notifyDataSetChanged() 
} 

最後但並非最不重要的,在裏面getView() - 方法您Adapter你可以看到,如果一個Model檢查或不採取相應的行動。

getView(...){ 

    if (listOfModel.get(position).isChecked){ 
     // do stuff, probably check a CheckBox 
    } else { 
     // uncheck... 
    } 

祝你好運!

+1

+1對於MVC的優秀解釋 – baske

+0

+100對於這個偉大的答案,你爲我省了很多次。謝謝 ! – Lithium

+0

非常有用的解決方案和解釋,謝謝 – audiophile121