57新建群聊---选择联系人逻辑的完成

最终实现的效果如下图:


在查看群组详情的Activity中,会向本Activity传一个groupId,根据这个值是否为null来判断是否是新建群组。

String groupId = getIntent().getStringExtra("groupId");

		if (groupId == null) {// 创建群组
			isCreatingNewGroup = true;
		} else {
			EMGroup group = EMGroupManager.getInstance().getGroup(groupId);
			exitingMembers = group.getMembers();
		}
		if (exitingMembers == null) {
			exitingMembers = new ArrayList<String>();

		}

获取列表,当然要去掉“申请与通知”和“群聊”两个item.

List<User> alluserList = new ArrayList<User>();

		for (User user : IMApplication.getInstance().getContactList().values()) {
			
		/*	电路问题总结:

			对于:&   -- >  不管怎样,都会执行"&"符号左右两边的程序

			对于:&& -- >  只有当符号"&&"左边程序为真(true)后,才会执行符号"&&"右边的程序。

			下面来说说运算规则:

			对于:&  -- >  只要左右两边有一个为false,则为false;只有全部都为true的时候,结果为true

			对于:&& -- > 只要符号左边为false,则结果为false;当左边为true,同时右边也为true,则结果为true*/
			
			if (!user.getUsername().equals(Constant.NEW_FRIENDS_USERNAME)
					& !user.getUsername().equals(Constant.GROUP_USERNAME)) {
				alluserList.add(user);
			}
		}
对列表进行排序:

Collections.sort(alluserList, new Comparator<User>() {

			@Override
			public int compare(User lhs, User rhs) {
				return (lhs.getUsername().compareTo(rhs.getUsername()));
			}
		});

设置Adapter:

contactAdapter = new PickContactAdapter(this,
				R.layout.row_contact_with_checkbox, alluserList);
listView.setAdapter(contactAdapter);

SiderBar的ListView的设置:

((SiderBar) findViewById(R.id.sidebar)).setListView(listView);
点击每个Item,CheckBox相应点击事件,事先在xml文件中已经禁用了CheckBox的点击事件:

listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
				checkBox.toggle();
			}
		});
保存按钮的点击事件,将结果返回给上一个Activity:

public void save(View view) {
		setResult(RESULT_OK, new Intent().putExtra("newmembers",
				getToBeAddMembers().toArray(new String[0])));
		
		finish();
		
		//====================测试===========================
		
		String[] array = getToBeAddMembers().toArray(new String[0]);
		
		for (int i = 0; i < array.length; i++) {
			LogUtil.d(TAG, "getToBeAddMembers().toArray(new String[0]):" + array[i]);
		}
		
		List<String> toBeAddMembers = getToBeAddMembers();
		
		for (String string : toBeAddMembers) {
			LogUtil.d(TAG, string.toString());
		}
		
		
		
		
		//=====================================================
		
		
		
	}

获取将要添加的好友的列表:

/**
	 * 获取要被添加的成员
	 * 
	 * @return
	 */
	private List<String> getToBeAddMembers() {

		List<String> members = new ArrayList<String>();

		int length = contactAdapter.isCheckedArray.length;
		
		LogUtil.d(TAG, "contactAdapter.isCheckedArray.length:" + length);

		for (int i = 0; i < length; i++) {
			String username = contactAdapter.getItem(i + 1).getUsername();
			
			
			LogUtil.d(TAG, "contactAdapter.getItem(0).getUsername():" + contactAdapter.getItem(0).getUsername() );
			LogUtil.d(TAG, "contactAdapter.getItem(1).getUsername():" + contactAdapter.getItem(1).getUsername() );
			
			
			if (contactAdapter.isCheckedArray[i]
					&& !exitingMembers.contains(username)) {
				members.add(username);
			}
		}

		return members;
	}

这个列表是CheckBox选中的,如果之前已经在群组中的好友,则不在内,注意item从1开始,因为0这个位置是搜索框,用户名是null:




PickContactAdapter继承自ContactAdapter,这里面的SiderBar的东西就不需要去重写了。在getView里面,需要做的事情,设置CheckBox的状态,


		private boolean[] isCheckedArray;

		public PickContactAdapter(Context context, int resource,
				List<User> users) {
			super(context, resource, users, null);

			LogUtil.d(TAG, "users.size():" + users.size());

			isCheckedArray = new boolean[users.size()];
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {

			View view = super.getView(position, convertView, parent);

			if (position > 0) {
				final String username = getItem(position).getUsername();
				
				
				LogUtil.d(TAG, "username:" + username);
				
				final CheckBox checkBox = (CheckBox) view
						.findViewById(R.id.checkbox);

				if (exitingMembers != null && exitingMembers.contains(username)) {

					checkBox.setButtonDrawable(R.drawable.checkbox_bg_gray_selector);

				} else {
					checkBox.setButtonDrawable(R.drawable.checkbox_bg_selector);
				}
				if (checkBox != null) {
					checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {

							if (exitingMembers.contains(username)) {
								isChecked = true;

								checkBox.setChecked(true);

							}
							
							LogUtil.d(TAG, "position -1:" + (position -1));
							isCheckedArray[position - 1] = isChecked;

							// 如果是单选模式
							if (isSignleChecked && isChecked) {
								for (int i = 0; i < isCheckedArray.length; i++) {
									if (i != position - 1) {
										isCheckedArray[i] = false;
									}
								}
								contactAdapter.notifyDataSetChanged();
							}

						}
					});

					if (exitingMembers.contains(username)) {
						checkBox.setChecked(true);
						isCheckedArray[position - 1] = true;

					} else {
						checkBox.setChecked(isCheckedArray[position - 1]);
					}

				}

			}

			return view;

		}

	

完整的代码:

package com.dystu.impro.activity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;

import com.dystu.impro.R;
import com.dystu.impro.adapter.ContactAdapter;
import com.dystu.impro.app.IMApplication;
import com.dystu.impro.constant.Constant;
import com.dystu.impro.debug.LogUtil;
import com.dystu.impro.domain.User;
import com.dystu.impro.widget.SiderBar;
import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupManager;

public class GroupPickContactsActivity extends BaseActivity {

	public static final String TAG = "GroupPickContactsActivity";

	private ListView listView;

	/**
	 * 是否为新建的群组
	 * 
	 */
	private boolean isCreatingNewGroup;
	/**
	 * 
	 * 是否为单选
	 */
	private boolean isSignleChecked;

	private PickContactAdapter contactAdapter;

	/**
	 * 
	 * group中一开始就有的成员
	 */
	private List<String> exitingMembers;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_group_pick_contacts);

		String groupId = getIntent().getStringExtra("groupId");

		if (groupId == null) {// 创建群组
			isCreatingNewGroup = true;
		} else {
			EMGroup group = EMGroupManager.getInstance().getGroup(groupId);
			exitingMembers = group.getMembers();
		}
		if (exitingMembers == null) {
			exitingMembers = new ArrayList<String>();

		}

		List<User> alluserList = new ArrayList<User>();

		for (User user : IMApplication.getInstance().getContactList().values()) {
			
		/*	电路问题总结:

			对于:&   -- >  不管怎样,都会执行"&"符号左右两边的程序

			对于:&& -- >  只有当符号"&&"左边程序为真(true)后,才会执行符号"&&"右边的程序。

			下面来说说运算规则:

			对于:&  -- >  只要左右两边有一个为false,则为false;只有全部都为true的时候,结果为true

			对于:&& -- > 只要符号左边为false,则结果为false;当左边为true,同时右边也为true,则结果为true*/
			
			if (!user.getUsername().equals(Constant.NEW_FRIENDS_USERNAME)
					& !user.getUsername().equals(Constant.GROUP_USERNAME)) {
				alluserList.add(user);
			}
		}

		Collections.sort(alluserList, new Comparator<User>() {

			@Override
			public int compare(User lhs, User rhs) {
				return (lhs.getUsername().compareTo(rhs.getUsername()));
			}
		});

		listView = (ListView) findViewById(R.id.list);

		contactAdapter = new PickContactAdapter(this,
				R.layout.row_contact_with_checkbox, alluserList);

		listView.setAdapter(contactAdapter);

		((SiderBar) findViewById(R.id.sidebar)).setListView(listView);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
				checkBox.toggle();
			}
		});

	}

	public void save(View view) {
		setResult(RESULT_OK, new Intent().putExtra("newmembers",
				getToBeAddMembers().toArray(new String[0])));
		
		finish();
		
		//====================测试===========================
		
		String[] array = getToBeAddMembers().toArray(new String[0]);
		
		for (int i = 0; i < array.length; i++) {
			LogUtil.d(TAG, "getToBeAddMembers().toArray(new String[0]):" + array[i]);
		}
		
		List<String> toBeAddMembers = getToBeAddMembers();
		
		for (String string : toBeAddMembers) {
			LogUtil.d(TAG, string.toString());
		}
		
		
		
		
		//=====================================================
		
		
		
	}

	/**
	 * 获取要被添加的成员
	 * 
	 * @return
	 */
	private List<String> getToBeAddMembers() {

		List<String> members = new ArrayList<String>();

		int length = contactAdapter.isCheckedArray.length;
		
		LogUtil.d(TAG, "contactAdapter.isCheckedArray.length:" + length);

		for (int i = 0; i < length; i++) {
			String username = contactAdapter.getItem(i + 1).getUsername();
			
			
			LogUtil.d(TAG, "contactAdapter.getItem(0).getUsername():" + contactAdapter.getItem(0).getUsername() );
			LogUtil.d(TAG, "contactAdapter.getItem(1).getUsername():" + contactAdapter.getItem(1).getUsername() );
			
			
			if (contactAdapter.isCheckedArray[i]
					&& !exitingMembers.contains(username)) {
				members.add(username);
			}
		}

		return members;
	}

	private class PickContactAdapter extends ContactAdapter {

		private boolean[] isCheckedArray;

		public PickContactAdapter(Context context, int resource,
				List<User> users) {
			super(context, resource, users, null);

			LogUtil.d(TAG, "users.size():" + users.size());

			isCheckedArray = new boolean[users.size()];
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {

			View view = super.getView(position, convertView, parent);

			if (position > 0) {
				final String username = getItem(position).getUsername();
				
				
				LogUtil.d(TAG, "username:" + username);
				
				final CheckBox checkBox = (CheckBox) view
						.findViewById(R.id.checkbox);

				if (exitingMembers != null && exitingMembers.contains(username)) {

					checkBox.setButtonDrawable(R.drawable.checkbox_bg_gray_selector);

				} else {
					checkBox.setButtonDrawable(R.drawable.checkbox_bg_selector);
				}
				if (checkBox != null) {
					checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {

							if (exitingMembers.contains(username)) {
								isChecked = true;

								checkBox.setChecked(true);

							}
							
							LogUtil.d(TAG, "position -1:" + (position -1));
							isCheckedArray[position - 1] = isChecked;

							// 如果是单选模式
							if (isSignleChecked && isChecked) {
								for (int i = 0; i < isCheckedArray.length; i++) {
									if (i != position - 1) {
										isCheckedArray[i] = false;
									}
								}
								contactAdapter.notifyDataSetChanged();
							}

						}
					});

					if (exitingMembers.contains(username)) {
						checkBox.setChecked(true);
						isCheckedArray[position - 1] = true;

					} else {
						checkBox.setChecked(isCheckedArray[position - 1]);
					}

				}

			}

			return view;

		}

	}

	public void back(View view) {
		finish();
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值