2011-01-06 91 views
1

朋友,如何以編程方式選擇自定義列表適配器中的所有複選框?

我正在使用下面的自定義適配器在列表視圖中顯示textview和checboxes。 現在如果textview是selectAll單擊checbox在它的列表中,我應該選擇列表中下面的所有複選框。 在我的案例中,selectall複選框將位於「0」位置,其他位於下方。

任何人指導我如何獲得所有複選框數組,以便我可以選擇或取消選擇它們。 任何幫助,將不勝感激。

public class EfficientAdapter extends BaseAdapter implements 
     Filterable { 
    private LayoutInflater mInflater; 
    //private Context context; 
    List<DalCategories> obj; 

    public EfficientAdapter(Context context,List<DalCategories> value) { 

     mInflater = LayoutInflater.from(context); 
     obj = value; 
     //this.context = context; 
    } 

    public View getView(final int position, View convertView, 
     final ViewGroup parent) { 
     ViewHolder holder; 

     convertView = mInflater.inflate(R.layout.list_category_item, null); 

     holder = new ViewHolder(); 
     holder.TextTitle = (TextView) convertView.findViewById(R.id.list_item_title); 
     holder.checkBoxes = (CheckBox) convertView.findViewById(R.id.chkSubCategory); 


     if(obj.get(position).isIs_Selected()) 
     { 
      holder.checkBoxes.setChecked(true); 
     } 


     holder.checkBoxes 
     .setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(
        CompoundButton buttonView, boolean isChecked) { 

       if(select_all == true) 
{    
       // select all here........ 


}     


      } 
     }); 

     convertView.setTag(holder); 


     holder.TextTitle.setText(obj.get(position).getTitle()); 



     return convertView; 
    } 

    class ViewHolder 
    { 
     public TextView TextTitle; 
     public CheckBox checkBoxes; 
    } 

    @Override 
    public Filter getFilter() { 
     return null; 
    } 

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

    @Override 
    public int getCount() { 
     return obj.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return obj.get(position); 
    } 

    }`enter code here` 

回答

1

要回答你的主要問題......你應該做的是迭代存儲的數據(List obj)並將值設置爲true,然後調用notifyDataSetChanged()。

然而,您應該解決的問題(有點諷刺地命名)EfficientAdapter存在很多問題。

首先,您使用的是ViewHolder模式,但在這裏完全沒用,因爲您忽略了convertView參數並每次都從頭開始創建一個新視圖。由於您的視圖永遠不會被重用,所以視圖持有者不會爲您做任何事情。如果convertView爲null,則只應該使新視圖膨脹。

其次,看起來你只有在點擊第一個項目時纔會點擊,但是你正在創建和設置每個項目的處理程序。只需將外部支票移到外面,而不是將有效的不操作事件處理程序附加到除您的一個元素之外的所有元素,就可以使代碼更高效。

我強烈建議你去/在列表視圖這段視頻來自谷歌I O:http://developer.android.com/videos/index.html#

0

您需要有一個全局布爾數組來存儲複選框的狀態。

聲明此陣外的onCreate()方法

private boolean[] itemToggled; 

並初始化它裏面的onCreate()方法如下

itemToggled = new boolean[Checkbox.length]; 
int i; 
     for(i=0;i<Checkbox.length;i++) 
     { 
      itemToggled[i]=false; 
     } 

然後您可以將適配器裏面有這個。

@Override 
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
if(select_all == true) 
{    
       // select all here........ 
     int i; 
     for(i=0;i<Checkbox.length;i++) 
     { 
      itemToggled[i]=true; 
     } 

}  

使用此數組中的值處理複選框。

0
selectAllButton.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) 
     { 
      Log.i("Children Count",""+myListView.getCount()+"Items from MyList Checked"); 
       for(int i=0;i<myListView.getCount();i++) 
       { 
        myListView.setItemChecked(i,true); 
       } 
     } 
    }); 

享受和保持androiding ...

相關問題