标签:
package com.example.selectall; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.ListView; import android.widget.Spinner; import android.widget.SpinnerAdapter; import android.widget.TextView; public class MainActivity extends Activity { private ListView listView; private TextView tvCount; private CheckBox checkAll; private MyAdapter adapter; private List<User> datas; private boolean isSelectAll;//全选状态(学会玩这种的) private int selectCount;//统计选中的个数 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); adapter = new MyAdapter(datas, this); listView.setAdapter(adapter); checkAll.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { isSelectAll = !isSelectAll; //通过全选按钮更新视图 updateSelectAllStatus(); } }); } private void updateSelectAllStatus() { for(User user:datas){ user.setChecked(isSelectAll); } if (isSelectAll) { selectCount = datas.size(); }else{ selectCount = 0; } tvCount.setText("你选择了"+selectCount+"项"); adapter.notifyDataSetChanged(); } private void initData() { datas = new ArrayList<User>(); User user; for(int i= 0;i<15;i++){ user = new User("name"+i,10+i); datas.add(user); } } private void initView() { listView = (ListView) findViewById(R.id.listView); tvCount = (TextView) findViewById(R.id.tvCount); checkAll = (CheckBox) findViewById(R.id.checkAll); } public void oprate(String oprate){ if ("add".equals(oprate)) { selectCount++; }else if("sub".equals(oprate)){ selectCount--; } if (selectCount==datas.size()) { checkAll.setChecked(true); isSelectAll = true; }else{ checkAll.setChecked(false); isSelectAll = false; } tvCount.setText("你选择了"+selectCount+"项"); } }
package com.example.selectall; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.webkit.WebView.FindListener; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.TextView; public class MyAdapter extends BaseAdapter { private List<User> datas; private Context context; public MyAdapter( List<User> datas,Context context) { this.context = context; this.datas = datas; } @Override public int getCount() { return datas.size(); } @Override public Object getItem(int position) { return datas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if (convertView==null) { convertView = LayoutInflater.from(context).inflate(R.layout.item, null); viewHolder = new ViewHolder(); viewHolder.tvname = (TextView) convertView.findViewById(R.id.name); viewHolder.tvage = (TextView) convertView.findViewById(R.id.age); viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.check); //设置一个tag 以后就从缓存里取对象 convertView.setTag(viewHolder); }else{ viewHolder = (ViewHolder) convertView.getTag(); } final User user = datas.get(position); viewHolder.tvname.setText(user.getName()); viewHolder.tvage.setText(user.getAge()+""); viewHolder.checkBox.setChecked(user.isChecked()); viewHolder.checkBox.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { boolean isChecked = !user.isChecked(); user.setChecked(isChecked); if (isChecked) { //加(学会玩这种) ((MainActivity)context).oprate("add"); }else{ //减 ((MainActivity)context).oprate("sub"); } } }); return convertView; } class ViewHolder{ TextView tvname,tvage; CheckBox checkBox; } }
package com.example.selectall; public class User { private String name; private int age; private boolean isChecked; public User() { } public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public boolean isChecked() { return isChecked; } public void setChecked(boolean isChecked) { this.isChecked = isChecked; } }
标签:
原文地址:http://my.oschina.net/u/2282721/blog/477074