阳光天下

学无止境-与大家共同进步

导航

直接上代码  主要思路是修改List<String[]>中的值。

Activity类

 1 package com.test.listact;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import android.app.Activity;
7 import android.os.Bundle;
8 import android.view.View;
9 import android.widget.Button;
10 import android.widget.ListView;
11 import android.widget.Toast;
12
13 public class ListSelectAllTestActivity extends Activity {
14
15 private ListView mListView ;
16 private Button selBtn,selectedBtn;
17 private SelectAdapter myAdapter;
18 private boolean isSelectAll = false;
19 private List<String[]> data = null;
20 public void onCreate(Bundle savedInstanceState) {
21 super.onCreate(savedInstanceState);
22 setContentView(R.layout.main);
23
24 mListView = (ListView)findViewById(R.id.listview);
25
26 data = getData();
27
28 myAdapter = new SelectAdapter(this,data);
29
30 mListView.setAdapter(myAdapter);
31
32 selBtn = (Button)findViewById(R.id.selBtn);
33
34 selBtn.setOnClickListener(new Button.OnClickListener(){
35 public void onClick(View arg0) {
36 if(isSelectAll){
37 myAdapter.cancelAll();
38 isSelectAll = false;
39 }else{
40 myAdapter.selectAll();
41 isSelectAll = true;
42 }
43 }
44 });
45
46 selectedBtn = (Button)findViewById(R.id.selectedBtn);
47 selectedBtn.setOnClickListener(new Button.OnClickListener(){
48 public void onClick(View arg0) {
49 if(data!=null){
50 String []d;
51 int select = 0,cancel = 0;
52 for(String [] dd:data){
53 if(!dd[1].equals("0")){ // '0' 代表未选中
54 select++;
55 }else{
56 cancel++;
57 }
58 }
59 Toast.makeText(ListSelectAllTestActivity.this, "选中数:"+select +"\t未选中数:"+cancel,Toast.LENGTH_LONG).show();
60 }
61 }
62 });
63 }
64
65 private List<String[]> getData(){
66 List<String[]> list = new ArrayList<String[]>();
67 for(int i = 0;i<20;i++){
68 String [] data = new String[2];
69 data[0] = "Hello" + (i+1);
70 data[1] = "0";
71 list.add(data);
72 }
73 return list;
74 }
75
76 }

Adapter 类,主要逻辑在这里

package com.test.listact;

import java.util.List;

import android.content.Context;
import android.util.Log;
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.TextView;

public class SelectAdapter extends BaseAdapter {


private LayoutInflater mInflater;
private Context mContext;
private List <String[]> mList;

public SelectAdapter(Context mContext,List <String[]> mlist){
this.mList = mlist;
this.mContext = mContext;
this.mInflater = LayoutInflater.from(mContext);
}

public int getCount() {
return mList.size();
}

public Object getItem(int arg0) {
return mList.get(arg0);
}

public long getItemId(int arg0) {
return arg0;
}

public View getView(int position, View view, ViewGroup parent) {
ViewHold mViewHold;
if(view == null){
Log.d("","View VVVVV");
mViewHold = new ViewHold();
view = mInflater.inflate(R.layout.item,null);
mViewHold.mTextView = (TextView)view.findViewById(R.id.label);
mViewHold.mCheckBox = (CheckBox)view.findViewById(R.id.labelCheckBox);
view.setTag(mViewHold);
}else{
mViewHold = (ViewHold)view.getTag();
Log.d("","View VVVVVEEEEEEEEEE");
}

String data[] = mList.get(position);
mViewHold.mTextView.setText(data[0]);

String isSelected = data[1];

mViewHold.mCheckBox.setId(position);
mViewHold.mCheckBox.setChecked(!isSelected.equals("0"));

//点击时把值修改下
mViewHold.mCheckBox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

String dat[] = mList.get(buttonView.getId());
dat[1] = isChecked?"1":"0";
mList.set(buttonView.getId(), dat);
}

});



return view;
}
//全选
public void selectAll(){
if(mList!=null){
for(int i=0;i<mList.size();i++){
mList.get(i)[1] = "1";
}
this.notifyDataSetChanged();
}
}
//取消全选
public void cancelAll(){
if(mList!=null){
for(int i=0;i<mList.size();i++){
mList.get(i)[1] = "0";
}
this.notifyDataSetChanged();
}
}


class ViewHold {
public TextView mTextView;
public CheckBox mCheckBox;
}

}

XML文件这里就省略了。。。