100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > android listview 中的checkbox Android中ListView与CheckBox的使用 及问题解决

android listview 中的checkbox Android中ListView与CheckBox的使用 及问题解决

时间:2019-11-18 02:55:55

相关推荐

android listview 中的checkbox Android中ListView与CheckBox的使用 及问题解决

最近自己在编写有关SIM卡管理的软件做练习,其中使用到了ListView与CheckBox的的组合,遇到了和大家有同样的问题:

1.选中一个checkbox对应位置的其他checkbox也会被选中

2.选中一个checkbox之后,滑动滚动条,之前选中的checkbox会莫名其妙的取消选中

于是网上搜之,具体的问题所在,网上答得也是很含糊,由于接触android时间不长,

其中的原因我也不是很清楚,不过总的来说代码还是搞定了

写好的代码供大家参考下:

import java.util.HashMap;

import java.util.List;

import com.rice.activity.R;

import com.rice.domain.ViewHolder;

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 poundButton;

import android.widget.TextView;

import poundButton.OnCheckedChangeListener;

public class MyAdapter extends BaseAdapter

{

private Context context;

private

List

Object>> data;

//用来记录所有ListView记录对应checkbox的状态

public HashMap

Boolean> isSelected;

private int resource;

private int to[];

private String from[];

private LayoutInflater inflater = null;

private ViewHolder holder = null;

//构造函数

public MyAdapter(Context context,

List

Object>> data, int resource, String

from[], int to[])

{

this.context = context;

this.data = data;

this.resource = resource;

this.from = new

String[from.length];

this.to = new

int[to.length];

System.arraycopy(from, 0,

this.from, 0, from.length);

System.arraycopy(to, 0,

this.to, 0, to.length);

init();

}

public HashMap

Boolean> getIsSelected()

{

return isSelected;

}

//初始化设置所有checkbox都为未选择状态

private void init()

{

isSelected = new

HashMap();

for (int i = 0; i

< data.size(); i++)

{

isSelected.put(i,

false);

}

}

@Override

public int getCount()

{

return data.size();

}

@Override

public Object getItem(int arg0)

{

return data.get(arg0);

}

@Override

public long getItemId(int arg0)

{

return 0;

}

@Override

public View getView(final int position, View

view, ViewGroup arg2)

{

holder = null;

if(null == holder)

{

//总是新建一个ViewHolder对象,用来保存每一个listview条目的信息

holder = new

ViewHolder();

if(null ==

view)

{

inflater

= LayoutInflater.from(context);

view

= inflater.inflate(resource, null);

}

holder.name =

(TextView) view.findViewById(R.id.name);

holder.mobile

= (TextView) view.findViewById(R.id.mobile);

holder.checkBox

= (CheckBox) view.findViewById(R.id.item_checkBox);

}

HashMap

Object> map = data.get(position);

if(null != map)

{

String name =

(String) map.get("name");

String mobile

= (String) map.get("mobile");

holder.name.setText(name);

holder.mobile.setText(mobile);

}

//添加checkBox监听

holder.checkBox.setOnCheckedChangeListener(new

OnCheckedChangeListener()

{

@Override

public void

onCheckedChanged(CompoundButton arg0, boolean isCheck)

{

if(isCheck)

{

isSelected.put(position,

true);

//System.out.println("add

checked=" + position);

}

else

if(!isCheck)

{

isSelected.put(position,

false);

//System.out.println("remove

checked=" + position);

}

}

});

//根据isSelected中记录的信息,设置checkbox的状态

holder.checkBox.setChecked(isSelected.get(position));

return view;

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。