2016-03-10 42 views
0

我想選擇所有的checkBox,當點擊「selectAll」按鈕。我有自定義列表視圖與checkBox和textView。下面我把我的代碼和屏幕截圖作爲參考。如何選擇按鈕點擊自定義列表視圖的所有複選框?

screen shot of listView

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <ListView 
      android:id="@+id/lvMain" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 

      <Button 
       android:id="@+id/btnSelectAll" 
       android:layout_height="wrap_content" 
       android:layout_width="match_parent" 
       android:text="Select All" > 
      </Button> 
     </LinearLayout> 

list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" > 

     <CheckBox 
      android:id="@+id/cbItem" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:buttonTint="#000000"> 
     </CheckBox> 

     <TextView 
      android:id="@+id/tvItem" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#000000"> 
     </TextView> 

    </LinearLayout> 

MainActivity.java

public class MainActivity extends Activity { 
ListView lvMain; 
String[] name = { "Jenis", "Pratik", "Jaydeep", "Hiren", "Himansu", 
     "yagnik", "Atul", "Prakas", "Nihal", "Darshan", "Chetan", "Sagar", 
     "Nikhil", "Sanket", "Rahul", "Jigar" }; 
Button btnSelectAll; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    lvMain = (ListView) findViewById(R.id.lvMain); 
    btnSelectAll = (Button) findViewById(R.id.btnSelectAll); 
    ListAdapter adapter = new ListAdapter(getApplicationContext(), name); 
    lvMain.setAdapter(adapter); 
    adapter.notifyDataSetChanged(); 
} 
} 

ListAdapter.java

package com.example.checkallcheckbox; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.CheckBox; 
import android.widget.TextView; 
public class ListAdapter extends BaseAdapter { 
    Context mContext; 
    String[] name; 
    private static LayoutInflater inflater = null; 
    public ListAdapter(Context c, String[] name) { 
     // TODO Auto-generated constructor stub 
     mContext = c; 
     this.name = name; 
     inflater = (LayoutInflater) mContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 
    @Override 
    public int getCount() { 
     return name.length; 
    } 
    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 
    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 
    public class ViewHolder { 
     TextView tvName; 
     CheckBox cbName; 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup arg2) { 
     // TODO Auto-generated method stub 
     ViewHolder viewHolder; 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.list_item, null); 
      viewHolder = new ViewHolder(); 
      viewHolder.tvName = (TextView) convertView 
        .findViewById(R.id.tvItem); 
      viewHolder.cbName = (CheckBox) convertView 
        .findViewById(R.id.cbItem); 
      convertView.setTag(viewHolder); 
     } else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
     } 
     viewHolder.tvName.setText(name[position]); 
     return convertView; 
    } 
} 
+1

看到這個http://stackoverflow.com/questions/4553186/android-checkbox-listview-select-all-disable-enable如果您在這裏找到您的解決方案 –

回答

1

傳遞一個標誌和更新適配器

 ListAdapter adapter = new ListAdapter(getApplicationContext(), name,false); 
     lvMain.setAdapter(adapter); 

    ListAdapter adapter = new ListAdapter(getApplicationContext(), name,true); 
     adapter.notifyDataSetChanged(); 

    public ListAdapter(Context c, String[] name,Boolean flag) { 
      // TODO Auto-generated constructor stub 
      mContext = c; 
      this.name = name; 
    this.flag=flag 
      inflater = (LayoutInflater) mContext 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     } 


@Override 
    public View getView(int position, View convertView, ViewGroup arg2) { 
     // TODO Auto-generated method stub 
     ViewHolder viewHolder; 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.list_item, null); 
      viewHolder = new ViewHolder(); 
      viewHolder.tvName = (TextView) convertView 
        .findViewById(R.id.tvItem); 
      viewHolder.cbName = (CheckBox) convertView 
        .findViewById(R.id.cbItem); 
      convertView.setTag(viewHolder); 
     } else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
     } 
     viewHolder.tvName.setText(name[position]); 

if(flag==true) 
{ 
cbName .setChecked(true); 
} 
     return convertView; 
    } 
+0

謝謝你,它爲我工作。 –

0
public class MainActivity extends Activity { 
ListView lvMain; 
boolean isSelectAll = false; 

On Button click make isSelectAll value as true and call notifydatasetchage on adapter. 

    public boolean isSelectALL(){ 
     return isSelectAll; 
    } 

在適配器級,獲得acivity類的實例,

if(activity.isSelectALL()){ 
    viewHolder.cbName.setChecked(true) 
}else{ 
    viewHolder.cbName.setChecked(false) 
} 

希望這會幫助你。

0

你的代碼應該看起來像這樣。選中的狀態應該來自包含數據的ArrayList <>或Array []。並記住在複選框通過調用persons.get(position).setChecked(true/false)來切換狀態時更新對象的選中狀態;

class Person{ 
    String name; 
    boolean checked; 
    public Person(String name){ 
     this.name=name; 
    } 
    public String getName(){ 
     return name; 
    } 
    public boolean isChecked(){ 
     return checked; 
    } 
    public void setChecked(boolean checked){ 
     this.checked=checked; 
    } 
} 

class PersonsAdapter extends BaseAdapter{ 
    LayoutInflater inflater; 
    ArrayList<Person> persons; 
    public PersonsAdapter(Context context, ArrayList<Person> persons){ 
     this.persons=persons; 
     inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder viewHolder; 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.list_item, null); 
      viewHolder = new ViewHolder(); 
      viewHolder.tvName = (TextView) convertView 
        .findViewById(R.id.tvItem); 
      viewHolder.cbName = (CheckBox) convertView 
        .findViewById(R.id.cbItem); 
      convertView.setTag(viewHolder); 
     } else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
     } 
     viewHolder.tvName.setText(persons.get(position).getName()); 
     viewHolder.cbName.setChecked(persons.get(position).isChecked()); 
    } 

} 

和點擊收聽

 selectAllButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       for(Person person:persons){ 
        person.setChecked(true); 
       } 
       adapter.notifyDataSetChanged(); 
      } 
     }); 
相關問題