自定义的ListView与复选框选中检查项目 [英] custom ListView with checkboxes checking unchecked items
问题描述
我有一个BaseAdapter为我的ListView的片段,看起来像这里面的:
i have a BaseAdapter for my ListView inside a fragment that looks like this:
public class SelectionMucListAdapter extends BaseAdapter {
private LayoutInflater inflater = null;
Typeface titleFace;
ArrayList<UsersData> innerList = new ArrayList<UsersData>();
public SelectionMucListAdapter(ArrayList<UsersData> users) {
inflater = (LayoutInflater) mainActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
titleFace = Typeface.createFromAsset(mainActivity.getAssets(), "fonts/bradybun.ttf");
innerList = users;
}
@Override
public int getCount() {
return innerList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int id) {
return id;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.muc_list_item, null);
viewHolder = new ViewHolder();
viewHolder.image = (ImageView)convertView.findViewById(R.id.item_image);
viewHolder.check = (CheckBox)convertView.findViewById(R.id.cb_choose_user);
viewHolder.title = (TextView)convertView.findViewById(R.id.item_text_1);
viewHolder.rating=(RatingBar)convertView.findViewById(R.id.item_text_2);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
}
viewHolder.title.setTypeface(titleFace);
Bitmap avatar = innerList.get(position).getUserAvatar();//UsersManager.getInstance().getUsers().get(position).getUserAvatar();
if(avatar != null)
viewHolder.image.setImageBitmap(avatar);
viewHolder.title.setText(innerList.get(position).getUserName());
viewHolder.rating.setRating((float)Double.parseDouble(innerList.get(position).getRating()));
viewHolder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton cb, boolean isChecked) {
if(cb.isChecked()) {// if this item is checked then add to list
Log.v("CheckBox", "pos: "+position+" checked");
if(!choices.contains(innerList.get(position))) {
choices.add(innerList.get(position));
}
} else {// if its unchecked then remove from list if exist
Log.v("CheckBox", "pos: "+position+" checked");
if(choices.contains(innerList.get(position))) {
choices.remove(innerList.get(position));
}
}
isChecked = cb.isChecked();
}
});
viewHolder.check.setTag(position);
return convertView;
}
}
static class ViewHolder{
ImageView image;
CheckBox check;
TextView title;
RatingBar rating;
}
class CheckState{
boolean isChecked;
UsersData usersData;
public UsersData getUsersData() {
return usersData;
}
public void setUsersData(UsersData usersData) {
this.usersData = usersData;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
}
我有20个列表项,而问题是,当我首先检查2项,例如在指数[0]和[1],不知何故,还检查自动其他物品指数[9] [10]当我向下滚动列表。如果我检查其他项目,它会重复我每次向下滚动列表,检查了相同的模式相同的情况下发生的。
i have 20 list items, and the problem is that when i checked first 2 items for example at index [0] and [1], somehow it also checked automatically other items at index [9] and [10] when i scroll down the list. same case happened if i checked other items and it will checked repeatedly with the same pattern every time i scroll down the list.
其实,我不知道发生了什么,所以我要问,如果有人在这里可以阻止这种混乱。谢谢!
i actually dont know what's happening, so i want to ask if anyone here can stop this messy. Thank you!
推荐答案
您自定义适配器必须实现 CompoundButton.OnCheckedChangeListener
。使用 SparseBooleanArray。
Your Custom Adapter must implement CompoundButton.OnCheckedChangeListener
. Use a SparseBooleanArray.
然后
cb.setChecked(mCheckStates.get(position, false)); // cb is checkbox
cb.setOnCheckedChangeListener(this);
接着用选中状态,设置文本复选框
Then use the checked state to set text to check box
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
探讨的话题@
Discussion on the topic @
的https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M
例如:
使用一个 ViewHolder
。下面的例子不使用视图持有人。
Use a ViewHolder
. The below example does not use a View Holder.
有一个按钮在屏幕的底部。当您检查检查行项目显示在举杯的复选框,当你点击按钮的下方。
There is a Button at the bottom of the screen. When you check the check boxes items of the checked rows are displayed in a toast when you click the button at the bottom.
根据您的要求使用以下和修改。
Use the below and modify according to your requirements.
public class MainActivity extends Activity implements
AdapterView.OnItemClickListener {
int count;
private CheckBoxAdapter mCheckBoxAdapter;
String[] GENRES = new String[] {
"Action", "Adventure", "Animation", "Children", "Comedy",
"Documentary", "Drama",
"Foreign", "History", "Independent", "Romance", "Sci-Fi",
"Television", "Thriller"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView) findViewById(R.id.lv);
listView.setItemsCanFocus(false);
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(this);
mCheckBoxAdapter = new CheckBoxAdapter(this, GENRES);
listView.setAdapter(mCheckBoxAdapter);
Button b= (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
StringBuilder result = new StringBuilder();
for(int i=0;i<mCheckBoxAdapter.mCheckStates.size();i++)
{
if(mCheckBoxAdapter.mCheckStates.get(i)==true)
{
result.append(GENRES[i]);
result.append("\n");
}
}
Toast.makeText(MainActivity.this, result, 1000).show();
}
});
}
public void onItemClick(AdapterView parent, View view, int
position, long id) {
mCheckBoxAdapter.toggle(position);
}
class CheckBoxAdapter extends ArrayAdapter implements CompoundButton.OnCheckedChangeListener
{ private SparseBooleanArray mCheckStates;
LayoutInflater mInflater;
TextView tv1,tv;
CheckBox cb;
String[] gen;
CheckBoxAdapter(MainActivity context, String[] genres)
{
super(context,0,genres);
mCheckStates = new SparseBooleanArray(genres.length);
mInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
gen= genres;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return gen.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = mInflater.inflate(R.layout.checkbox, null);
tv= (TextView) vi.findViewById(R.id.textView1);
cb = (CheckBox) vi.findViewById(R.id.checkBox1);
tv.setText("Name :"+ gen [position]);
cb.setTag(position);
cb.setChecked(mCheckStates.get(position, false));
cb.setOnCheckedChangeListener(this);
return vi;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
}
}
这篇关于自定义的ListView与复选框选中检查项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!