标签:
package com.example.checks; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import com.example.adapter.MyBaseadapter; import com.example.vo.User; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { private ListView mylist; private List<User> list = new ArrayList<User>(); private TextView bian, fan, quan,shan; // private Button quan; private LinearLayout lin2; private MyBaseadapter adapter; private CheckBox che; View view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); init(); } private void init() { // TODO Auto-generated method stub mylist = (ListView) findViewById(R.id.mylist); bian = (TextView) findViewById(R.id.bian); quan = (TextView) findViewById(R.id.quan); fan = (TextView) findViewById(R.id.fan); shan = (TextView) findViewById(R.id.shan); view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.base, null); che=(CheckBox) view.findViewById(R.id.checkBox1); //xuan = (TextView) findViewById(R.id.xuan); lin2 = (LinearLayout) findViewById(R.id.lin2); for (int i = 0; i < 30; i++) { User u = new User(); u.setName("我爱我家,更爱大家" + i); u.setBoo(false); list.add(u); } adapter = new MyBaseadapter(getApplicationContext(), list); mylist.setAdapter(adapter); bian.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(bian.getText().equals("编辑")){ lin2.setVisibility(View.VISIBLE); bian.setText("取消"); quan.setVisibility(View.VISIBLE); che.setVisibility(View.GONE); }else if(bian.getText().equals("取消")){ lin2.setVisibility(View.GONE); bian.setText("编辑"); quan.setVisibility(View.INVISIBLE); che.setVisibility(View.VISIBLE); } } }); mylist.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub lin2.setVisibility(View.VISIBLE); bian.setText("取消"); quan.setVisibility(View.VISIBLE); che.setVisibility(View.GONE); if (list.get(arg2).isBoo()) { list.get(arg2).setBoo(false); } else { list.get(arg2).setBoo(true); } adapter.notifyDataSetChanged(); } }); shan.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Iterator<User> it=list.iterator(); while(it.hasNext()){ User user=it.next(); if(user.isBoo()==true){ it.remove(); adapter.notifyDataSetChanged(); } } // for (int i = 0; i < list.size(); i++) { // if(list.get(i).isBoo()){ // // list.remove(i); // } // // adapter.notifyDataSetChanged(); // } } }); quan.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (quan.getText().equals("全选")) { for (int i = 0; i < list.size(); i++) { list.get(i).setBoo(true); adapter.notifyDataSetChanged(); } quan.setText("全不选"); quan.setTextColor(Color.RED); } else if (quan.getText().equals("全不选")) { for (int i = 0; i < list.size(); i++) { list.get(i).setBoo(false); adapter.notifyDataSetChanged(); } quan.setText("全选"); quan.setTextColor(Color.BLACK); } } }); fan.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub for (int i = 0; i < list.size(); i++) { // if (list.get(i).isBoo()) { list.get(i).setBoo(false); } else { list.get(i).setBoo(true); } adapter.notifyDataSetChanged(); } } }); } // public List<User> setData() { // // list = new ArrayList<User>(); // for (int i = 0; i < 20; i++) { // //默认为全部选中 // list.add(new User("商品:"+i, false)); // } // return list; // } }
package com.example.adapter; import java.util.List; import com.example.checks.R; import com.example.vo.User; 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.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.TextView; public class MyBaseadapter extends BaseAdapter { private Context context; private List<User>list; private CheckBox checkBox; int num=0; public MyBaseadapter(Context context,List<User>list){ this.context=context; this.list=list; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder vh; if(convertView==null){ vh=new ViewHolder(); convertView=LayoutInflater.from(context).inflate(R.layout.base, null); vh.cb=(CheckBox) convertView.findViewById(R.id.checkBox1); vh.tv=(TextView) convertView.findViewById(R.id.tv); convertView.setTag(vh); }else{ vh=(ViewHolder) convertView.getTag(); } vh.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked){ list.get(position).setBoo(isChecked); }else{ list.get(position).setBoo(isChecked); } } }); vh.cb.setChecked(list.get(position).isBoo()); vh.tv.setText(list.get(position).getName()); return convertView; } class ViewHolder{ TextView tv; CheckBox cb; } }
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_selected="true" android:drawable="@drawable/hookicon_repost_pressed"></item> <item android:state_checked="true" android:drawable="@drawable/hookicon_repost_pressed"></item> <item android:state_pressed="true" android:drawable="@drawable/hookicon_repost_pressed"></item> <item android:drawable="@drawable/ic_checkbox_normal"></item> </selector>
标签:
原文地址:http://www.cnblogs.com/yr520/p/5402269.html