Hello stackoverflow
我正在尝试使用我自己的自定义适配器使用以下代码从SD卡列出图像文件
public class MyCustomArrayAdapter extends ArrayAdapter<Model> {
private final Activity context;
private final List<Model> list;
private static HashSet<String> checkedItems = new HashSet<String>();
private ArrayList<Boolean> checkList = new ArrayList<Boolean>();
public static HashSet<String> getCheckedItems() {
return checkedItems;
}//End of getCheckedItems()
private static void setCheckedItems(String packageName) {
if(!checkedItems.contains(packageName)){
checkedItems.add(packageName);
}else{
checkedItems.remove(packageName);
}
}//End of setCheckedItems()
public MyCustomArrayAdapter(Activity context, List<Model> list) {
super(context, R.layout.list_layout, list);
this.context = context;
this.list = list;
for (int i = 0; i < list.size(); i++) {
checkList.add(false);
}
}//End of MyCustomArrayAdapter() constructor
static class ViewHolder {
protected TextView text;
protected ImageView image;
protected ProgressBar pb;
protected CheckBox cb;
}//End of ViewHolder static inner class
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.list_layout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.text.setTextColor(Color.BLACK);
viewHolder.image = (ImageView) view.findViewById(R.id.image);
viewHolder.image.setVisibility(View.GONE);
viewHolder.cb = (CheckBox) view.findViewById(R.id.item_check_box);
viewHolder.cb.setTag(Integer.valueOf(position)); // set the tag so we can identify the correct row in the listener
viewHolder.cb.setChecked(checkList.get(position)); // set the status as we stored it
viewHolder.pb = (ProgressBar) view.findViewById(R.id.progressBar);
view.setTag(viewHolder);
} else {
view = convertView;
}
final int pos = position;
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.image.setTag(list.get(position).getURL());
holder.image.setId(position);
PbAndImage pb_and_image = new PbAndImage();
pb_and_image.setImg(holder.image);
pb_and_image.setPb(holder.pb);
new DownloadImageTask().execute(pb_and_image);
holder.image.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://"+list.get(pos).getURL()), "image/*");
context.startActivity(intent);
}//End of onClick method
});//End of holder.image anonymous class
holder.cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setCheckedItems(list.get(pos).getURL());
}
});
holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkList.set((Integer)buttonView.getTag(),isChecked);
}//End of onCheckedChanged()
});//End of holder.cb anonymous class
return view;
}//End of getView()
}//End of MyCustomArrayAdapter class
但问题是当我检查CheckBox
并滚动其他复选框时也会检查。
请帮我解决这个谜题。
感谢。
答案 0 :(得分:1)
使用包装器类并在该包装器类中取一个布尔值并根据该包装器类布尔值设置复选框,因为当我们滚动列表视图时我们必须保持复选框bcoz的值然后视图是新制作的并且它不能存储
的价值答案 1 :(得分:1)
从if
区块中取出这两行。应始终执行它们:
viewHolder.cb.setTag(Integer.valueOf(position)); // set the tag so we can identify the correct row in the listener
viewHolder.cb.setChecked(checkList.get(position)); // set the status as we stored it
像这样:
...
...
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.image.setTag(list.get(position).getURL());
holder.image.setId(position);
holder.cb.setTag(Integer.valueOf(position)); // set the tag so we can identify the correct row in the listener
holder.cb.setChecked(checkList.get(position)); // set the status as we stored it
...
...
由于您仅在tag
内设置checked
的{{1}}和checkbox
状态,因此滚动时不会更新这些值。因此行为很奇怪。这应该有效..
答案 2 :(得分:0)
设为true setChecked="true"
选中复选框
viewHolder.cb.setChecked="true";
答案 3 :(得分:0)
您必须在getView中选中或取消选中当前元素。 像
这样的东西holder.cb.setChecked(checkList.get((Integer)buttonView.getTag()));
那是因为你重复使用上次使用的对象的视图和“记住”复选框状态。这就是为什么你必须为getView
中的每个元素覆盖它答案 4 :(得分:0)
if(checkbox.isChecked){
..its display only checked checkbox
}
检查这个条件
答案 5 :(得分:0)
从if
viewHolder.cb.setTag(Integer.valueOf(position)); // set the tag so we can identify the correct row in the listener
viewHolder.cb.setChecked(checkList.get(position)); // set the status as we stored it
您还需要在2行之前添加holder.cb.setOnCheckedChangeListener(null);
,否则如果setOnCheckedChangeListener
CheckBox
触发View