我正在制作一个Android应用程序,其中我为列表制作了一个自定义适配器,当我取消选中复选框并返回到上一个片段然后重新打开片段所有复选框被选中。如何克服这种情况
这是我的java代码
ArrayList<Boolean> checked_list;
public SettingsAdapter(Context context, int resource, RateItem[] objects) {
super(context, resource, objects);
checked_list=new ArrayList<Boolean>();
for(int i=0;i<this.getCount();i++){
checked_list.add(i,true);
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater=(LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.settings_single,null);
}
ImageView img= (ImageView) convertView.findViewById(R.id.imageView);
TextView title_txt= (TextView) convertView.findViewById(R.id.textViewtitle);
TextView subtitle_txt= (TextView) convertView.findViewById(R.id.textViewsubtitle);
final CheckBox checkBox= (CheckBox) convertView.findViewById(R.id.checkBoxselected);
img.setImageResource(R.drawable.australia);
title_txt.setText(settings_array[position].title_rate);
subtitle_txt.setText(settings_array[position].subtitle_rate);
checkBox.setChecked(checked_list.get(position));
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!checkBox.isChecked()){
Set<RateItem> asSet = new HashSet<RateItem>(Arrays.asList(FragmentRateActivity.rate_array));
asSet.remove(FragmentRateActivity.rate_array[position]);
FragmentRateActivity.rate_array = asSet.toArray(new RateItem[]{});
checked_list.set(position,false);
}
else {
/*Set<RateItem> asSet = new HashSet<RateItem>(Arrays.asList(FragmentRateActivity.rate_array));
asSet.remove(FragmentRateActivity.rate_array[position]);
FragmentRateActivity.rate_array = asSet.toArray(new RateItem[]{});*/
checked_list.set(position,true);
}
}
});
return convertView;
}
答案 0 :(得分:0)
使用checkBox.setOnCheckedChangeListener而不是checkBox.setOnClickListener。 您正在构造函数中将所有checked_list设置为true