Android 可以多选的分组联系人列表

时间:2021-07-22 15:13:10

只是作为自己的一个笔记
进入选择联系人页面的入口 以及返回选择结果 展示选择联系人AutolinefeedView使用的是网上找得(点击联系人删除)

        add_people.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// 选择联系人
Intent intent = new Intent(SendXiaoGaoActivity.this,
SelectPeopleActivity.class);
intent.putExtra("userlist", userlist);
startActivityForResult(intent, 20);
}
});

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case 20:
userlist = new ArrayList<String>();
autolinefeedView1.removeAllViews();
ArrayList<Map<String, Object>> result = (ArrayList<Map<String, Object>>) data
.getExtras().get("result");// 得到新Activity 关闭后返回的数据
for (int i = 0; i < result.size(); i++) {
userlist.add(result.get(i).get("peopleid").toString());
final Button bt = new Button(context);
bt.setText(result.get(i).get("name").toString());
bt.setTag(result.get(i).get("peopleid").toString());
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
autolinefeedView1.removeView(bt);
userlist.remove(bt.getTag());
}
});
autolinefeedView1.addView(bt);
}
break;
default:
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}

选择联系人列表页面,两次网络请求,一次获得部门列表(包含了部门人数,为后面的部门全选做准备),一次获得人员列表,处理数据,做成部门人员的完整列表。

package com.ytint.wloaa.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import cn.jpush.android.api.JPushInterface;

import com.ab.activity.AbActivity;
import com.ab.http.AbHttpUtil;
import com.ab.http.AbStringHttpResponseListener;
import com.ab.view.ioc.AbIocView;
import com.ab.view.titlebar.AbTitleBar;
import com.ytint.wloaa.R;
import com.ytint.wloaa.adapter.SelectPeopleAdapter;
import com.ytint.wloaa.adapter.SelectPeopleAdapter.ViewHolder;
import com.ytint.wloaa.app.MyApplication;
import com.ytint.wloaa.bean.Department;
import com.ytint.wloaa.bean.DepartmentList;
import com.ytint.wloaa.bean.People;
import com.ytint.wloaa.bean.PeopleList;
import com.ytint.wloaa.bean.URLs;
import com.ytint.wloaa.widget.TitleBar;

/**
* 选择联系人
*
* @author wlj
*
*/

public class SelectPeopleActivity extends AbActivity {

String TAG = "SelectPeopleActivity";
private MyApplication application;
Context context = null;
private String loginKey;
String host;
@AbIocView(id = R.id.listView_people_list)
ListView listView_people_list;
public static Map<Integer, Boolean> isSelected;
private List<People> peoples;
private List<Department> deptartments;
private SelectPeopleAdapter adapter;
private List<Map<String, Object>> mData;
private List<String> userlist=new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
application = (MyApplication) this.getApplication();
host = URLs.HTTP + application.getProperty("HOST") + ":"
+ application.getProperty("PORT");
Intent intent = getIntent();
userlist=intent.getStringArrayListExtra("userlist");
setAbContentView(R.layout.layout_select_people);
context = SelectPeopleActivity.this;
loginKey = application.getProperty("loginKey");
AbTitleBar mAbTitleBar = this.getTitleBar();
mAbTitleBar.setVisibility(View.GONE);
final TitleBar titleBar = (TitleBar) findViewById(R.id.title_bar);
titleBar.setLeftImageResource(R.drawable.back_green);
titleBar.setLeftText("返回");
titleBar.setLeftTextColor(Color.WHITE);
titleBar.setLeftClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
titleBar.setTitle("选择联系人");
titleBar.setTitleColor(Color.WHITE);
titleBar.setDividerColor(Color.GRAY);
titleBar.setActionTextColor(Color.WHITE);
titleBar.addAction(new TitleBar.TextAction("完成") {
@Override
public void performAction(View view) {
//数据是使用Intent返回
Intent intent = new Intent();
ArrayList<Map<String, Object>> datas=new ArrayList<Map<String, Object>>();
//获得选择的人员列表
for (int i = 0; i < isSelected.size(); i++) {
if (isSelected.get(i)) {
if (!mData.get(i).get("peopleid").toString().equals("0")) {
datas.add(mData.get(i));
}
}
}
//把返回数据存入Intent
intent.putExtra("result", datas);
//设置返回数据
SelectPeopleActivity.this.setResult(RESULT_OK, intent);
//关闭Activity
SelectPeopleActivity.this.finish();
}
});
loadDept();

}

@SuppressLint("NewApi")
private void loadDept() {
final AbHttpUtil mAbHttpUtil = AbHttpUtil.getInstance(this);
if (!application.isNetworkConnected()) {
showToast("请检查网络连接");
return;
}
String host=URLs.HTTP+application.getProperty("HOST")+":"+application.getProperty("PORT");
String url=host+URLs.DEPLIST+"?user_id="+loginKey;
Log.e(TAG, url);
mAbHttpUtil.get(url,
new AbStringHttpResponseListener() {
// 获取数据成功会调用这里
@Override
public void onSuccess(int statusCode, String content) {
try {
DepartmentList cList = DepartmentList.parseJson(content);
if (cList.code == 200) {
deptartments = cList.getInfo();
loadPeoples();
} else {
showToast(cList.msg);
}
} catch (Exception e) {
e.printStackTrace();
showToast("数据解析失败");
}
};

// 开始执行前
@Override
public void onStart() {
// 显示进度框
showProgressDialog();
}

@Override
public void onFailure(int statusCode, String content,
Throwable error) {
showToast("网络连接失败!");
}

// 完成后调用,失败,成功
@Override
public void onFinish() {
// 移除进度框
removeProgressDialog();
};

});
}
@SuppressLint("NewApi")
private void loadPeoples() {

final AbHttpUtil mAbHttpUtil = AbHttpUtil.getInstance(this);
if (!application.isNetworkConnected()) {
showToast("请检查网络连接");
return;
}
String host=URLs.HTTP+application.getProperty("HOST")+":"+application.getProperty("PORT");
mAbHttpUtil.get(host+URLs.USERLIST+"?user_id=0" ,
new AbStringHttpResponseListener() {
// 获取数据成功会调用这里
@Override
public void onSuccess(int statusCode, String content) {
try {
PeopleList cList = PeopleList.parseJson(content);
if (cList.code == 200) {
peoples = cList.getInfo();
for (int i = 0; i < peoples.size(); i++) {
if (loginKey.equals(peoples.get(i).id+"")) {
peoples.remove(i);
}
}
mData = new ArrayList<Map<String, Object>>();
for (Department department : deptartments) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", department.name);
map.put("isdept", "1");
map.put("dept_id", department.id);
map.put("peopleid", 0);
map.put("user_num", department.user_num);
mData.add(map);
for (People people : peoples) {
if (people.department_id.trim().equals(department.id.trim())) {
map = new HashMap<String, Object>();
map.put("name", people.name);
map.put("isdept", "2");
map.put("dept_id",people.department_id);
map.put("peopleid",people.id);
map.put("user_num",0);
mData.add(map);
}
}
}
// 这儿定义isSelected这个map是记录每个listitem的状态,初始状态全部为false。
isSelected = new HashMap<Integer, Boolean>();
for (int i = 0; i < mData.size(); i++) {
if (userlist.contains(mData.get(i).get("peopleid").toString())) {
isSelected.put(i, true);
}else{
isSelected.put(i, false);
}
}
adapter=new SelectPeopleAdapter(context,mData,isSelected);
listView_people_list.setAdapter(adapter);
listView_people_list.setItemsCanFocus(false);
listView_people_list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

listView_people_list.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ViewHolder vHollder = (ViewHolder) view.getTag();
//在每次获取点击的item时将对于的checkbox状态改变,同时修改map的值。
vHollder.cBox.toggle();
SelectPeopleAdapter.isSelected.put(position, vHollder.cBox.isChecked());
//判断是否是部门,选择部门下的用户
if (vHollder.isdept.equals("1")) {
for (int i = 1; i <= vHollder.user_num; i++) {
SelectPeopleAdapter.isSelected.put(position+i, vHollder.cBox.isChecked());
}
// 通知listView刷新
adapter.notifyDataSetChanged();
}
}
});
} else {
showToast(cList.msg);
}
} catch (Exception e) {
e.printStackTrace();
showToast("数据解析失败");
}
};

// 开始执行前
@Override
public void onStart() {
// 显示进度框
showProgressDialog();
}

@Override
public void onFailure(int statusCode, String content,
Throwable error) {
showToast("网络连接失败!");
}

// 完成后调用,失败,成功
@Override
public void onFinish() {
// 移除进度框
removeProgressDialog();
};

});
}
}
package com.ytint.wloaa.adapter;

import java.util.List;
import java.util.Map;

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;

import com.ytint.wloaa.R;

public class SelectPeopleAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<Map<String, Object>> mData;
public static Map<Integer, Boolean> isSelected;

public SelectPeopleAdapter(Context context, List<Map<String, Object>> mDatas,Map<Integer, Boolean> isSelecteds) {
mInflater = LayoutInflater.from(context);
init(mDatas,isSelecteds);
}

// 初始化
private void init(List<Map<String, Object>> mDatas,Map<Integer, Boolean> isSelecteds) {
mData=mDatas;
isSelected=isSelecteds;
}

@Override
public int getCount() {
return mData.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mData.get(position).get("name");
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// convertView为null的时候初始化convertView。
// if (convertView == null) {
holder = new ViewHolder();
if (mData.get(position).get("isdept").toString().equals("1")) {
convertView = mInflater.inflate(
R.layout.addpeople_list_item_tag, null);
holder.title = (TextView) convertView
.findViewById(R.id.addpeople_list_item_text);
holder.cBox = (CheckBox) convertView
.findViewById(R.id.addpeople_list_item_check);
} else {
convertView = mInflater.inflate(R.layout.addpeople_list_item,
null);
holder.title = (TextView) convertView
.findViewById(R.id.people_one);
holder.cBox = (CheckBox) convertView
.findViewById(R.id.people_check);
}
convertView.setTag(holder);
// } else {
// holder = (ViewHolder) convertView.getTag();
// }
holder.title.setText(mData.get(position).get("name").toString());
holder.cBox.setChecked(isSelected.get(position));
holder.dept_id=mData.get(position).get("dept_id").toString();
holder.isdept=mData.get(position).get("isdept").toString();
holder.peopleid=mData.get(position).get("peopleid").toString();
holder.user_num=Integer.parseInt(mData.get(position).get("user_num").toString());
return convertView;
}

public final class ViewHolder {
public String dept_id=new String();
public String isdept=new String();
public String peopleid=new String();
public int user_num;
public TextView title;
public CheckBox cBox;
}
}

addpeople_list_item_tag

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/darkgrey"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="10dp" >


<TextView
android:id="@+id/addpeople_list_item_text"
android:layout_width="wrap_content"
android:layout_height="35dip"
android:layout_weight="0.8"
android:gravity="center_vertical"
android:text="金融考试"
android:textColor="#ffffff"
android:textSize="17sp" />


<CheckBox
android:id="@+id/addpeople_list_item_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false" />


</LinearLayout>

addpeople_list_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="horizontal"
android:padding="5dip" >


<TextView
android:id="@+id/people_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_weight="1"
android:text="TextView"
android:textColor="@color/black"
android:textSize="17sp" />


<CheckBox
android:id="@+id/people_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false" />


</LinearLayout>

layout_select_people

<?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:background="#ffffff"
android:orientation="vertical" >


<com.ytint.wloaa.widget.TitleBar
android:id="@+id/title_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/titlebar" >

</com.ytint.wloaa.widget.TitleBar>

<ListView
android:id="@+id/listView_people_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

</ListView>

</LinearLayout>