Android 列表视图中具有不同行布局的复选框问题
我尝试使列表视图具有Android 列表视图中具有不同行布局的复选框问题,android,android-listview,android-checkbox,Android,Android Listview,Android Checkbox,我尝试使列表视图具有文本视图和复选框。它工作正常,但当在另一行中选择复选框时,另一行中的复选框也被选中。 示例:如果我选中第一行并向下滚动,我发现另一行也被选中 这是我的适配器代码 public class ReadersrListAdapter extends BaseAdapter{ private static final int TYPE_SEPARATOR = 0; private static final int TYPE_ITEM = 1; private static final
文本视图
和复选框
。它工作正常,但当在另一行中选择复选框
时,另一行中的复选框
也被选中。
示例:如果我选中第一行并向下滚动,我发现另一行也被选中
这是我的适配器代码
public class ReadersrListAdapter extends BaseAdapter{
private static final int TYPE_SEPARATOR = 0;
private static final int TYPE_ITEM = 1;
private static final int TYPE_MAX_COUNT = 2;
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private LayoutInflater mInflater=null;
public ReadersrListAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
mInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEnabled(int position) {
return !data.get(position).get(UtiliShare.KEY_TITLE).startsWith("-");
}
@Override
public int getItemViewType(int position) {
return data.get(position).get(UtiliShare.KEY_TITLE).startsWith("-") ? TYPE_SEPARATOR : TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
ViewHolder2 holder2 = null;
int type = getItemViewType(position);
if (convertView == null) {
//holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.reader_list_item, null);
holder.textView = (TextView)convertView.findViewById(R.id.readerTitle);
holder.textView.setTypeface(UtiliShare.getTf());
convertView.setTag(holder);
break;
case TYPE_SEPARATOR:
holder2 = new ViewHolder2();
convertView = mInflater.inflate(R.layout.reader_list_devider, null);
holder2.textView = (TextView)convertView.findViewById(R.id.readerTitle);
holder2.textView.setTypeface(UtiliShare.getTf());
convertView.setTag(holder2);
break;
}
//convertView.setTag(holder);
} else {
if(type==TYPE_ITEM) holder = (ViewHolder)convertView.getTag();
else holder2 = (ViewHolder2)convertView.getTag();
}
HashMap<String, String> curdata = new HashMap<String, String>();
curdata = data.get(position);
String txt = curdata.get(UtiliShare.KEY_TITLE);
if(type == TYPE_SEPARATOR){
txt = txt.replace("-", "");
holder2.textView.setText(txt);
}else{
//if(position <= 100) System.out.println("getView " + position + " " + convertView + " type = " + type);
holder.star = (CheckBox) convertView.findViewById(R.id.btn_Fav);
holder.star.setOnCheckedChangeListener(((ReadersListActivity) this.activity).mStarCheckedChanceChangeListener);
holder.textView.setText(txt);
}
//holder.textView.setText(txt);
return convertView;
}
private static class ViewHolder {
public CheckBox star;
public TextView textView;
}
private static class ViewHolder2 {
public TextView textView;
}
我怎样才能解决这个问题
谢谢。您可能知道,在滚动时会重复使用布局,因此您需要跟踪是否应该自己检查每一行。让我们向适配器添加一个新成员:
private SparseBooleanArray checked = new SparseBooleanArray();
还有一种新方法:
public void toggleCheck(int position) {
boolean state = expanded.get(position, false);
expanded.put(!state);
}
现在在onCheckedChanged()中调用我们的新方法
:
最后,让我们调整getView()
以更改选中状态(以及其他一些事项):
//不要创建一个永远不会使用的新HashMap
//HashMap curdata=新的HashMap();
HashMap curdata=data.get(位置);
字符串txt=curdata.get(usilishare.KEY\u TITLE);
if(type==type_分隔符){
txt=txt.replace(“-”,”);
holder2.textView.setText(txt);
}否则{
//如果(位置)
public void toggleCheck(int position) {
boolean state = expanded.get(position, false);
expanded.put(!state);
}
final int position = list.getPositionForView(buttonView);
adapter.toggleCheck(position);
// Don't create a new HashMap that you'll never use
//HashMap<String, String> curdata = new HashMap<String, String>();
HashMap<String, String> curdata = data.get(position);
String txt = curdata.get(UtiliShare.KEY_TITLE);
if(type == TYPE_SEPARATOR){
txt = txt.replace("-", "");
holder2.textView.setText(txt);
}else{
//if(position <= 100) System.out.println("getView " + position + " " + convertView + " type = " + type);
// These two lines should be in `if(convertView == null)` with the others
holder.star = (CheckBox) convertView.findViewById(R.id.btn_Fav);
holder.star.setOnCheckedChangeListener(((ReadersListActivity) this.activity).mStarCheckedChanceChangeListener);
// Set the appropriate values
holder.textView.setText(txt);
holder.star.setChecked(expanded.get(position, false));
}