2011-06-13 16 views
2

我想選擇全部/取消全選複選框。如何在BaseAdapter中選擇全部/取消選擇Listview中的所有複選框

這是我的代碼。

private class BaseAdpterSendToServer extends BaseAdapter{ 

     String latti,calulationVal; 
     String longi; 
     String name; 


     public BaseAdpterSendToServer(Context context) { 
     layoutInflater = LayoutInflater.from(context); 
     } 

     @Override 
     public int getCount() { 

      return AndroidCamera.imagelistcount; 
     } 

     @Override 
     public Object getItem(int arg0) { 
      return arg0; 
     } 

     @Override 
     public long getItemId(int arg0) { 
      return arg0; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup arg2) { 

      viewHolder holder; 

      if(convertView==null){ 
       holder = new viewHolder(); 
       convertView =layoutInflater.inflate(R.layout.row_send_to_server, null); 

       holder.showImage = (ImageView)convertView.findViewById(R.id.imgView);    
       holder.txtCalulation = (TextView)convertView.findViewById(R.id.txtCalualtionVal); 
       holder.txtLatti = (TextView)convertView.findViewById(R.id.txtValLat); 
       holder.txtLongi = (TextView)convertView.findViewById(R.id.txtValLong); 
       holder.txtName = (TextView)convertView.findViewById(R.id.txtValName); 
       holder.checkBox = (CheckBox)convertView.findViewById(R.id.chkBox); 
       convertView.setTag(holder); 
      } 
      else{ 
       holder =(viewHolder)convertView.getTag(); 
      } 
      int pos = 0; 
      DBConnect d1 = new DBConnect(getApplicationContext(),"colorCode.db"); 

      pos = position+1; 
      Cursor c = d1.selectedImageId(pos); 


      String path = c.getString(1); 
      calulationVal = c.getString(2); 
      String s= calulationVal.toString(); 
      latti = c.getString(3); 
      longi = c.getString(4); 
      name = c.getString(5); 
       d1.close(); 

      Bitmap b1 = BitmapFactory.decodeFile(path); 


      System.out.println("THE BITMAP ISK ----- "+b1); 

      holder.showImage.setImageBitmap(b1); 
      holder.txtCalulation.setText(""+s); 
      holder.txtLatti.setText(""+latti); 
      holder.txtLongi.setText(""+longi); 
      holder.txtName.setText(""+name); 

      bt_f_unsel.setOnClickListener(new OnClickListener() { 


       } 
      }); 

      return convertView; 
     } 

     class viewHolder{ 
      TextView txtCalulation ,txtLatti, txtLongi,txtName; 
      ImageView showImage; 
      CheckBox checkBox; 
     } 

任何幫助對我都有好處。

回答

-4

我的建議只是做一個按鈕,將選擇/取消選中所有複選框,並點擊按鈕使一個for循環手動。我不知道其他的方式。

通常我們做出像這樣,

for(int i=0; i < listView.getChildCount(); i++){ 
RelativeLayout itemLayout = (RelativeLayout)listView.getChildAt(i); 
CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.MyListViewCheckBox); 
cb.setChecked(true); 

我希望這會幫助你。

+2

它不會幫助,因爲模型保持不變。在用戶下載完列表後,android會根據適配器返回的內容,使用舊複選框重新繪製它。 – damluar 2011-06-13 07:50:58

0

您可以製作一個按鈕,它將通過您的模型(您擁有的數據)並設置爲false/true。那麼你應該打電話notifyDataSetChanged()告訴視圖更新屏幕

+0

好吧,我拿了按鈕。在btn點擊...我應該寫什麼? – Siten 2011-06-13 07:58:01

+0

這取決於你從哪裏獲取數據。它應該循環訪問爲視圖提供數據的數據數組。據我所知,每次調用getView()時都會向DB請求數據。我不會這麼做......只需將它保存在某個數組中並從中獲取數據即可。然後,通過它進行循環,並在最後調用notifyDataSetChanged() – damluar 2011-06-13 08:04:08

相關問題