我创建了包含标题和复选框的listview。除了一个问题,它的工作就像一个魅力。如果我的列表小于屏幕大小那么没问题。如果我的列表长于屏幕大小说20项或更多,当我选择一个项目时它正在选择项目,当我滚动它时也选择一些其他随机项目。我已经搜索了这么多,但却无法找到解决方案。以下是我的采用者。我真的很感激帮助。
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.TreeSet;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.provider.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.TextView;
public class SectionHeader extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
ViewHolder holder;
private static int mSelectedItem = -1;
private ArrayList<String> mData = new ArrayList<String>();
private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();
static Boolean checkboxstate1[];
private LayoutInflater mInflater;
Boolean selected;
public void setSelectedItem(int position) {
mSelectedItem = position;
}
public SectionHeader(Context context) {
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
checkboxstate1=new Boolean[mData.size()];
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSectionHeaderItem(final String item) {
mData.add(item);
sectionHeader.add(mData.size() - 1);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
int rowType = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (rowType) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.list_main_item, null);
holder.textView = (TextView) convertView.findViewById(R.id.list_item);
holder.chk = (CheckBox) convertView.findViewById(R.id.checkbox);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.list_section_header, null);
holder.textView = (TextView) convertView.findViewById(R.id.list_header);
holder.chk = (CheckBox) convertView.findViewById(R.id.checkbox);
break;
}
convertView.setTag(holder);
convertView.setTag(R.id.list_main_item, holder.textView);
convertView.setTag(R.id.checkbox, holder.chk);
//else condition for getTag
} else {
holder = (ViewHolder) convertView.getTag();
}
convertView.setTag(holder);
holder.textView.setText(mData.get(position));
return convertView;
}
public static class ViewHolder {
public ImageButton imageButton;
public TextView textView;
public CheckBox chk;
}
}
答案 0 :(得分:0)
问题是由于您的适配器在CheckBox
方法中没有正确设置检查状态而重用getView(...)
对象这一事实。
您可以通过在checkboxstate1
数组中存储每个列表条目的已检查状态,然后在checkBox.setChecked(checkboxstate1[position])
内调用getView(...)
来执行此操作。
public class SectionHeader extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static int mSelectedItem = -1;
private List<String> mData = new ArrayList<String>();
private List<Boolean> mCheckedState = new ArrayList<Boolean>();
private Set<Integer> sectionHeader = new HashSet<Integer>();
private LayoutInflater mInflater;
private ViewHolder holder;
Boolean selected; // what is this for?
public void setSelectedItem(int position) {
mSelectedItem = position;
mCheckedState.set(position, true);
}
public SectionHeader(Context context) {
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
mCheckedState.add(false);
notifyDataSetChanged();
}
public void addSectionHeaderItem(final String item) {
mData.add(item);
mCheckedState.add(false);
sectionHeader.add(mData.size() - 1);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
int rowType = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (rowType) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.list_main_item, null);
holder.textView = (TextView) convertView.findViewById(R.id.list_item);
holder.chk = (CheckBox) convertView.findViewById(R.id.checkbox);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.list_section_header, null);
holder.textView = (TextView) convertView.findViewById(R.id.list_header);
holder.chk = (CheckBox) convertView.findViewById(R.id.checkbox);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
convertView.setTag(holder);
holder.textView.setText(mData.get(position));
holder.chk.setChecked(mCheckedState.get(position));
return convertView;
}
static class ViewHolder {
public ImageButton imageButton;
public TextView textView;
public CheckBox chk;
}
}