package com.example.choose;
import java.util.ArrayList;
import java.util.List;
import com.example.adapter.MyChooseAdapter;
import com.example.adapter.MyChooseAdapter.ViewHolder;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ListView lv;
private MyChooseAdapter mAdapter;
private ArrayList<String> list;
private Button bt_selectall;
private Button bt_cancel;
private Button bt_deselectall;
private Button deleteitem;
private int checkNum;
private TextView tv_show;
private List<String> data;
private TextView add;
private int allnumber=0;
private boolean flag=true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
bt_selectall = (Button) findViewById(R.id.bt_selectall);
bt_cancel = (Button) findViewById(R.id.bt_cancleselectall);
bt_deselectall = (Button) findViewById(R.id.bt_deselectall);
deleteitem=(Button) findViewById(R.id.deleteItem);
tv_show = (TextView) findViewById(R.id.tv);
add=(TextView) findViewById(R.id.add);
list = new ArrayList<String>();
initDate();
mAdapter = new MyChooseAdapter(list, this);
lv.setAdapter(mAdapter);
bt_selectall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < list.size(); i++) {
if (MyChooseAdapter.getIsSelected().get(i)==false) {
MyChooseAdapter.getIsSelected().put(i, true);
if (flag) {
allnumber+=Integer.valueOf(list.get(i));
}
}
}
flag=false;
checkNum = list.size();
dataChanged();
}
});
bt_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < list.size(); i++) {
if (MyChooseAdapter.getIsSelected().get(i)) {
MyChooseAdapter.getIsSelected().put(i, false);
checkNum--;
allnumber-=Integer.valueOf(list.get(i));
} else {
MyChooseAdapter.getIsSelected().put(i, true);
checkNum++;
allnumber+=Integer.valueOf(list.get(i));
}
}
flag=true;
dataChanged();
}
});
bt_deselectall.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
for (int i = 0; i < list.size(); i++) {
if (MyChooseAdapter.getIsSelected().get(i)) {
MyChooseAdapter.getIsSelected().put(i, false);
checkNum--;
allnumber-=Integer.valueOf(list.get(i));
}
}
flag=true;
dataChanged();
}
});
deleteitem.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
data=new ArrayList<String>();
for (int i = 0; i < list.size(); i++) {
if (MyChooseAdapter.getIsSelected().get(i)) {
data.add(list.get(i));
MyChooseAdapter.getIsSelected().put(i, false);
checkNum--;
allnumber-=Integer.valueOf(list.get(i));
}
}
list.removeAll(data);
dataChanged();
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
ViewHolder holder = (ViewHolder) arg1.getTag();
holder.cb.toggle();
MyChooseAdapter.getIsSelected().put(arg2, holder.cb.isChecked());
if (holder.cb.isChecked() == true) {
checkNum++;
allnumber+=Integer.valueOf(list.get(arg2));![这里写图片描述](http:
} else {
checkNum--;
allnumber-=Integer.valueOf(list.get(arg2));
}
dataChanged();
}
});
}
private void initDate() {
for (int i = 0; i < 50; i++) {
list.add(i+"");
}
}
private void dataChanged() {
mAdapter.notifyDataSetChanged();
tv_show.setText("已选中" + checkNum + "项");
add.setText("勾选的商品的总和是"+allnumber);
};
}
package com.example.adapter;
import java.util.ArrayList;
import java.util.HashMap;
import com.example.choose.R;
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.TextView;
public class MyChooseAdapter extends BaseAdapter{
private ArrayList<String> list;
private static HashMap<Integer, Boolean> isSelected;
private Context context;
private LayoutInflater inflater = null;
public MyChooseAdapter(ArrayList<String> list, Context context) {
super();
this.list = list;
this.context = context;
inflater = LayoutInflater.from(context);
isSelected = new HashMap<Integer, Boolean>();
initDate();
}
private void initDate() {
for (int i = 0; i < list.size(); i++) {
getIsSelected().put(i, false);
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.choose_item, null);
holder.tv = (TextView) convertView.findViewById(R.id.item_tv);
holder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText(list.get(position));
holder.cb.setChecked(getIsSelected().get(position));
return convertView;
}
public static HashMap<Integer, Boolean> getIsSelected() {
return isSelected;
}
public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
MyChooseAdapter.isSelected = isSelected;
}
public static class ViewHolder {
TextView tv;
public CheckBox cb;
}
}