简单的单选全选直接上代码 附 :下载地址http://download.csdn.net/detail/u012303938/8397441
解析的json类
package com.example.selectall;
public class JsonResult {
public static final String json="{\"cid\":\"101\",\"mobile\":\"13569874561\",\"flag\":\"success\",\"cause\":\"\",\"ename\":\"pk\",\"employno\":\"456\",\"dept\":\"客服部\",\"emplist\":[{\"employno\":\"001\",\"employ\":\"张美美\"},{\"employno\":\"002\",\"employ\":\"张三\"},{\"employno\":\"003\",\"employ\":\"小le\"},{\"employno\":\"456\",\"employ\":\"pk\"}]}";
}
主要的类
package com.example.selectall;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class MainActivity extends Activity {
private ListView listView;
private Button btn_cancle,btn_right;
private Map<String, String> map;
private ArrayList<Map<String, Object>> list;
private Map<Integer, Boolean> isCheckedMap;
private CheckBox selectAll;
private MyAdapter adapter;
private String name,no;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
findView();
loadData();
addListener();
}
private void addListener() {
// TODO Auto-generated method stub
btn_right.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(list!=null){
Set<Integer> set=isCheckedMap.keySet();
Iterator<Integer> iterator=set.iterator();
while(iterator.hasNext()){
Integer pos=iterator.next();
if(isCheckedMap.get(pos)){
name+=list.get(pos).get("Employ").toString()+",";
no+=list.get(pos).get("EmployNo").toString()+",";
}
}
Toast.makeText(MainActivity.this, name, 1).show();
}
}
});
btn_cancle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
selectAll.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean flag) {
// TODO Auto-generated method stub
if(list!=null&&list.size()>0){
Set<Integer> set=isCheckedMap.keySet();
Iterator<Integer> iterator=set.iterator();
if(flag){
while(iterator.hasNext()){
isCheckedMap.put(iterator.next(), true);
}
}else{
while(iterator.hasNext()){
isCheckedMap.put(iterator.next(), false);
}
}
adapter.notifyDataSetChanged();
}
}
});
}
private void loadData() {
// TODO Auto-generated method stub
try {
JSONObject jsonObject = new JSONObject(JsonResult.json);
if ("success".equals(jsonObject.getString("flag"))) {
JSONArray array = jsonObject.getJSONArray("emplist");
//tdcyData.setPageall(jsonObject.getString("pageall"));
int l = array.length();
list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < l; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("EmployNo", array.getJSONObject(i)
.getString("employno"));
map.put("Employ", array.getJSONObject(i).getString("employ"));
list.add(map);
isCheckedMap.put(i, false);
}
adapter= new MyAdapter(MainActivity.this, list);
listView.setAdapter(adapter);
} else {
listView.setAdapter(null);
Toast.makeText(MainActivity.this, "暂无相关数据", Toast.LENGTH_SHORT)
.show();
}
} catch (Exception e) {
// TODO: handle exception
}
}
private void findView() {
// TODO Auto-generated method stub
selectAll=(CheckBox) findViewById(R.id.selectAll);
btn_cancle=(Button) findViewById(R.id.btn_cancle);
btn_right=(Button) findViewById(R.id.btn_right);
listView=(ListView) findViewById(R.id.listview);
isCheckedMap = new HashMap<Integer, Boolean>();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class MyAdapter extends BaseAdapter {
private Context context;
private ViewHolder holder;
private List<Map<String, Object>> list;
public MyAdapter(Context context, List<Map<String, Object>> list
) {
this.context = context;
if (list == null) {
list = new ArrayList<Map<String, Object>>();
} else {
this.list = list;
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
if (list != null)
return list.size();
else
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
if (list != null)
return list.get(position);
else
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convervierw, ViewGroup arg2) {
// TODO Auto-generated method stub
holder = new ViewHolder();
if (convervierw == null) {
convervierw = View.inflate(context, R.layout.people_item, null);
holder.Employ = (TextView) convervierw
.findViewById(R.id.Employ);
holder.checkBox1 = (CheckBox) convervierw.findViewById(R.id.checkBox1);
holder.employNo = (TextView) convervierw.findViewById(R.id.employNo);
convervierw.setTag(holder);
} else {
holder = (ViewHolder) convervierw.getTag();
}
holder.Employ.setText((String) list.get(position).get("Employ"));
holder.employNo.setText((String) list.get(position).get("EmployNo")
);
holder.checkBox1.setChecked(isCheckedMap.get(position));
holder.checkBox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
isCheckedMap.put(position, true);
} else {
isCheckedMap.put(position, false);
}
}
});
return convervierw;
}
public final class ViewHolder {
public TextView Employ;
public TextView employNo;
public CheckBox checkBox1;
}
}
}
主布局Main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/title"
android:background="@drawable/abg"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:text="返回"
android:id="@+id/btn_cancle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_centerVertical="true"
android:gravity="center"
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/btn_right"
android:layout_toRightOf="@+id/btn_cancle"
android:text="选择列表" />
<Button
android:id="@+id/btn_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:padding="5dp"
android:text="提交" />
</RelativeLayout>
<CheckBox
android:layout_marginLeft="10dp"
android:id="@+id/selectAll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" 全选"
android:padding="10dp"
android:textSize="20sp"
android:textColor="#007700" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#999999" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="0dp"
android:background="#BBFFFFFF"
android:cacheColorHint="#00000000"
android:dividerHeight="2dp"
android:fastScrollEnabled="true"
android:focusable="true" >
</ListView>
</LinearLayout>
</LinearLayout>
列表item布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RelativeLayout
android:layout_centerVertical="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/checkBox1"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/ss"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:text="工号:" />
<TextView
android:id="@+id/employNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:text="ssss" />
</LinearLayout>
<LinearLayout
android:layout_below="@+id/ss"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:text="姓名:" />
<TextView
android:id="@+id/Employ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:text="ssss" />
</LinearLayout>
</RelativeLayout>
<CheckBox
android:id="@+id/checkBox1"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
</RelativeLayout>
</LinearLayout>
效果图: