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

來源:互聯網
上載者:User

主介面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="http://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="http://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>

囧神的世界你不懂,蟲哥的生活你沒有,只有程式猿的世界大家才知道。程式猿們,為了自己的精彩世界奮鬥吧,努力吧!加油……
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.