我想選擇所有的checkBox,當點擊「selectAll」按鈕。我有自定義列表視圖與checkBox和textView。下面我把我的代碼和屏幕截圖作爲參考。如何選擇按鈕點擊自定義列表視圖的所有複選框?
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;
}
}
看到這個http://stackoverflow.com/questions/4553186/android-checkbox-listview-select-all-disable-enable如果您在這裏找到您的解決方案 –