qq_29500013
2016-05-15 23:49
采纳率: 90%
浏览 2.7k
已采纳

expandablelistview和checkbox结合使用,复用问题,头疼好几天,大神帮帮忙啊

了viewholder复用了,当子项滑出范围,选中的checkbox变为未选中,不过选中的删除时删除是正确的,只是这样重复选择(不知道怎么说),体验不好,头疼好几天了

expandablelistviewadapter代码如下:
关键代码:

 /*
 * 解决复用问题的关键代码,那里好像出错了。导致自动让滑出屏幕的checkbox不选,这里
 */
if (checkposition!=null) {
itemHolder.cb.setChecked(checkposition.contains(itemHolder.cb.getText().toString())?true:false);
}
else{
itemHolder.cb.setChecked(false);
}
itemHolder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
String i = itemHolder.cb.getText().toString();
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if (isChecked) {
if (!checkposition.contains(i)) {
idlists.add(i);
checkposition.add(i);
}
Fragment1.tv.setText("您已选中"+idlists.size()+"个联系人");
}
else{
if (checkposition.contains(i)) {
idlists.remove(i);
checkposition.remove(i);
}
Fragment1.tv.setText("您已选中"+idlists.size()+"个联系人");
}
}
});

适配器完整代码:

 package com.daisy.work.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.daisy.work.R;
import com.daisy.work.atys.fragments.Fragment1;
import com.daisy.work.db.ContactDB;

public class MyExpandableListViewAdapter extends BaseExpandableListAdapter {
private Context context;
private Cursor cursor;
private ArrayList<String> group_list;
private ArrayList<List<Integer>> gr_list2;
private ContactDB ctDB;
private SQLiteDatabase dbRead;
private ItemHolder itemHolder;
public ArrayList<String> idlists;
public List<String> checkposition;

public MyExpandableListViewAdapter(Context context, Cursor cursor) {
this.context = context;
this.cursor = cursor;//cursor用来获取填充childview的数据
ctDB = new ContactDB(context);
dbRead = ctDB.getReadableDatabase();
idlists = new ArrayList<String>();//用来记录checkboxbox的值,和数据库的name一列的值相同,便于删除
checkposition = new ArrayList<String>();//用来解决复用滑动错乱问题
group_list = new ArrayList<String>();//填充组
group_list.add("默认");
group_list.add("亲人");
group_list.add("同事");
group_list.add("同学");
group_list.add("客户");

List<Integer> gr_list = new ArrayList<Integer>();
gr_list.add(R.drawable.tree_ec);
gr_list.add(R.drawable.tree_ec);
gr_list.add(R.drawable.tree_ec);
gr_list.add(R.drawable.tree_ec);
gr_list.add(R.drawable.tree_ec);

gr_list2 = new ArrayList<List<Integer>>();//{{1,2,3,4,5},{1,2,3,4,5}...}
gr_list2.add(gr_list);
gr_list2.add(gr_list);
gr_list2.add(gr_list);
gr_list2.add(gr_list);
gr_list2.add(gr_list);


}

@Override
public int getGroupCount() {
return group_list.size();
}

@Override
public int getChildrenCount(int groupPosition) {
String belong = group_list.get(groupPosition);//将特定的数据填充到分组中,用belong标识。
Cursor c = dbRead.query(ContactDB.CONTACT_TABLE_NAME, null,
"belong like ?", new String[] { "%" + belong + "%" }, null,
null, null);
if (!c.moveToFirst()) {
return 0;
} else {
return c.getCount();
}

}

@Override
public Object getGroup(int groupPosition) {
return group_list.get(groupPosition);
}
//多考虑一下
@Override
public Object getChild(int groupPosition, int childPosition) {

return getCombinedChildId(getGroupId(groupPosition),childPosition);//这个没搞太明白,填什么都不会出错

}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
GroupHolder groupHolder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.expendlist_group, null);
groupHolder = new GroupHolder();
groupHolder.txt = (TextView) convertView.findViewById(R.id.txt);
groupHolder.txt2 = (TextView) convertView.findViewById(R.id.txt2);
groupHolder.img = (ImageView) convertView.findViewById(R.id.img);
convertView.setTag(groupHolder);

} else {
groupHolder = (GroupHolder) convertView.getTag();
}

if (!isExpanded) {
groupHolder.img.setBackgroundResource(R.drawable.tree_ec);
} else {
groupHolder.img.setBackgroundResource(R.drawable.tree_ex);
}

String belong = group_list.get(groupPosition);
cursor = dbRead.query(ContactDB.CONTACT_TABLE_NAME, null,
"belong like ?", new String[] { "%" + belong + "%" }, null,
null, null);
int nm = cursor.getCount();//获取特定组中子项的数目。
groupHolder.txt.setText(group_list.get(groupPosition));
groupHolder.txt2.setText(nm + "");//显示组中子项的数目。
return convertView;
}

@Override
public View getChildView(final int groupPosition,final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
itemHolder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.expendlist_item,null);
itemHolder = new ItemHolder();
itemHolder.ct_name = (TextView) convertView
.findViewById(R.id.ct_name);
itemHolder.ct_qq = (TextView) convertView.findViewById(R.id.ct_qq);
itemHolder.iv = (ImageView) convertView.findViewById(R.id.iv);
itemHolder.dial = (ImageButton) convertView.findViewById(R.id.dial);
itemHolder.cb = (CheckBox) convertView.findViewById(R.id.cb);
itemHolder.cb.setTag(itemHolder.cb.getText().toString());//child_item布局里的checkbox控件
convertView.setTag(itemHolder);
} else {
itemHolder = (ItemHolder) convertView.getTag();
}
String belong = group_list.get(groupPosition);
cursor = dbRead.query(ContactDB.CONTACT_TABLE_NAME, null,
"belong like ?", new String[] { "%" + belong + "%" }, null,
null, null);
if (!cursor.moveToFirst()) {
return null;
} else {
cursor.moveToPosition(childPosition);
if (Fragment1.xs_bs.isShown()) {//按多选时将checkbox展现出啦,取消则隐藏xs_bs在adapter中无法获取,把它设为了public static
itemHolder.cb.setChecked(false);
itemHolder.cb.setVisibility(CheckBox.GONE);
}
else {
itemHolder.cb.setVisibility(CheckBox.VISIBLE);
itemHolder.dial.setOnClickListener(null);
}
if(Fragment1.qx.isPressed()){    //取消则隐藏
itemHolder.cb.setChecked(false);
}
//显示子项中的数据
itemHolder.ct_name.setText(cursor.getString(cursor
.getColumnIndex("name")));
itemHolder.cb.setText(cursor.getString(cursor
.getColumnIndex("name")));
itemHolder.ct_qq.setText(cursor.getString(cursor
.getColumnIndex("qq")));
itemHolder.iv.setBackgroundResource(R.drawable.ic_ct);
//按钮,由于其特殊性(在expandablelistview子项中的控件,在adapter中设置监听)
itemHolder.dial.setOnClickListener(new View.OnClickListener() {
String phone = cursor.getString(cursor.getColumnIndex("phone"));
@Override
public void onClick(View v) {
context.startActivity(new Intent(Intent.ACTION_DIAL, Uri
.parse("tel:" +phone)));
}
});
/*
 * 解决复用问题的关键代码,那里好像出错了。导致自动让滑出屏幕的checkbox不选,这里
 */
if (checkposition!=null) {
itemHolder.cb.setChecked(checkposition.contains(itemHolder.cb.getText().toString())?true:false);
}
else{
itemHolder.cb.setChecked(false);
}
itemHolder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
String i = itemHolder.cb.getText().toString();
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if (isChecked) {
if (!checkposition.contains(i)) {
idlists.add(i);
checkposition.add(i);
}
Fragment1.tv.setText("您已选中"+idlists.size()+"个联系人");
}
else{
if (checkposition.contains(i)) {
idlists.remove(i);
checkposition.remove(i);
}
Fragment1.tv.setText("您已选中"+idlists.size()+"个联系人");
}
}
});
return convertView;
}
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}


class GroupHolder {
public TextView txt;
public TextView txt2;

public ImageView img;
}

class ItemHolder {
public ImageView iv;

public TextView ct_name;
public TextView ct_qq;
public ImageButton dial;
public CheckBox cb;

}
}

图片说明

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • bdmh 2016-05-16 00:29
    已采纳

    自己多写几句,把listview的数据源定义为一个类,里面除了你要的信息外,在增加一个字段,checked,表示当前item是否处于选中状态,每次勾选时,设置checked=true,然后当你滑动时,只需要在getview中根据checked属性来设置checkbox的状态即可

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • qq_29500013 2016-05-16 05:16

    谢谢啦,做完了我把代码贴出来

    评论
    解决 无用
    打赏 举报
  • qq_34776784 2016-10-24 05:52

    能不能把解决的代码给我下
    我也出现一样的问题

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题