gpt4 book ai didi

android - 在 listView 中滚动时 CheckBox 状态发生变化

转载 作者:行者123 更新时间:2023-11-30 02:41:09 25 4
gpt4 key购买 nike

我很困惑,在我滚动时可以在 ListView 中保存 CheckBox 的状态之前,但现在不保存!我看到了很多解决方案,但在我的适配器类中没有看到任何错误。请查看它并告诉我应该更改代码的位置!

public class CustomListAdapter extends BaseAdapter {

protected ArrayList listData;
protected LayoutInflater layoutInflater;

private SharedPreferences sp;


private Editor editor;

public CustomListAdapter(Context context, ArrayList listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
sp=context.getSharedPreferences("sp", Activity.MODE_PRIVATE);
editor=sp.edit();

}

public CustomListAdapter(OnClickListener onClickListener,
ArrayList image_details) {
// TODO Auto-generated constructor stub

}

@Override
public int getCount() {
return listData.size();
}

@Override
public Object getItem(int position) {
return listData.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

public void setListData(ArrayList listdata){
this.listData=listdata;
}

public void remove(int i){
listData.remove(i);

}

@SuppressLint("NewApi")
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();

holder.cb_mark=(CheckBox) convertView.findViewById(R.id.cb_search_mark);

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

final Aye aye = (Aye)listData.get(position);
aye.setMarked(sp.getBoolean("CheckValue"+position, false));
holder.cb_mark.setChecked(aye.isMarked());


holder.cb_mark.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
aye.setMarked(arg1);
editor.putBoolean("CheckValue"+position, arg1);
editor.commit();
Log.i("CheckValue"+position, sp.getBoolean("CheckValue"+position, false)+"");

}
});



holder.cb_mark.setChecked(aye.isMarked());




return convertView;
}

static class ViewHolder {

CheckBox cb_mark;

}

}

最佳答案

可能是由于 onCheckedChanged 上的错误 position 值导致出现问题。尝试使用 setTag 方法保存当前项目位置,然后在 onCheckedChanged 中使用 getTag 获取位置:

aye.setMarked(sp.getBoolean("CheckValue"+position, false));
holder.cb_mark.setChecked(aye.isMarked());
holder.cb_mark.setTag(position); //<< store position of view

holder.cb_mark.setOnCheckedChangeListener(....{

@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
aye.setMarked(arg1);
int pos=Integer.parseInt(arg0.getTag().toString());
editor.putBoolean("CheckValue"+pos, arg1);
editor.commit();

}
});

关于android - 在 listView 中滚动时 CheckBox 状态发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25736219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com