Android在listview添加checkbox實現原理與代碼

Android在listview添加checkbox實現原理與代碼

主界面CheckBoxinListViewActivity.java代碼如下:

public class CheckBoxinListViewActivity extends Activity { 
/** Called when the activity is first created. */ 
private MyAdapter adapter; 
private ListView listview; 
private Button checkAll; 
private Button noCheckAll; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
listview = (ListView)findViewById(R.id.listview); 
checkAll = (Button)findViewById(R.id.button1); 
noCheckAll = (Button)findViewById(R.id.button2); 
adapter = new MyAdapter(); 
listview.setAdapter(adapter); 
checkAll.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
adapter.checkAll(); 
} 
}); 
noCheckAll.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
adapter.noCheckAll(); 
} 
}); 
} 
private class MyAdapter extends BaseAdapter{ 
private ArrayList<Message> list= new ArrayList<Message>(); 
public MyAdapter(){ 
for(int i = 1 ;i<=100 ; i++){ 
list.add(new Message("item_"+i)); 
} 
} 
public void checkAll(){ 
for(Message msg:list){ 
msg.isCheck = true; 
} 
notifyDataSetChanged(); 
} 
public void noCheckAll(){ 
for(Message msg:list){ 
msg.isCheck = false; 
} 
notifyDataSetChanged(); 
} 
@Override 
public int getCount() { 
return list.size(); 
} 
@Override 
public Object getItem(int position) { 
return null; 
} 
@Override 
public long getItemId(int position) { 
return 0; 
} 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
ViewHolder viewHolder; 
if(convertView == null){ 
LayoutInflater inflater = LayoutInflater.from(CheckBoxinListViewActivity.this); 
convertView = inflater.inflate(R.layout.listview_item, null); 
viewHolder = new ViewHolder(); 
viewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkBox1); 
convertView.setTag(viewHolder); 
}else{ 
viewHolder = (ViewHolder)convertView.getTag(); 
} 
final Message msg = list.get(position); 
viewHolder.checkBox.setText(msg.str); 
viewHolder.checkBox.setChecked(msg.isCheck); 
//註意這裡設置的不是onCheckedChangListener,還是值得思考一下的 
viewHolder.checkBox.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) { 
if(msg.isCheck){ 
msg.isCheck = false; 
}else{ 
msg.isCheck = true; 
} 
} 
}); 
return convertView; 
} 
} 
private class ViewHolder{ 
CheckBox checkBox; 
} 
} 

適配器所適配的消息Message.java如下:

public class Message { 
public boolean isCheck; 
public String str; 
public Message(String str){ 
this.str = str; 
} 
} 

main.xml代碼如下:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 
<Button android:text="全選" android:id="@+id/button1" 
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
<Button android:text="撤消" android:id="@+id/button2" 
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
</LinearLayout> 
<ListView android:id="@+id/listview" android:layout_height="fill_parent" 
android:layout_width="fill_parent" /> 
</LinearLayout> 

listview_item.xml代碼如下:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:descendantFocusability="blocksDescendants" 
> 
<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:gravity="center"> 
<CheckBox 
android:text="CheckBox" 
android:id="@+id/checkBox1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"/> 
</LinearLayout> 
</LinearLayout> 

You May Also Like