我有一个ListView
带有自己的适配器:CityZipCodesAdapter
. 在构造函数中,我给出了一个List<CityZipCodesDataModel>
我保留的位置String
,String
和boolen
(RadioButton
无论是否被选中)。这是我写的SharedPreferences
,然后我读了这个。这一切都有效。
public class CityZipCodesAdapter extends ArrayAdapter<CityZipCodesDataModel> {
private final List<CityZipCodesDataModel> list;
private final Activity context;
private RadioButton selectedRB;
private int selectedPosition = -1;
public CityZipCodesAdapter(Activity context,
List<CityZipCodesDataModel> list) {
super(context, R.layout.city_list_item, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView cityOrZipCode;
protected TextView listOfZipCodesInNear;
protected RadioButton radioBtn;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder viewHolder = null;
if (view == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.city_list_item, null);
viewHolder = new ViewHolder();
viewHolder.cityOrZipCode = (TextView) view.findViewById(R.id.cityOrZipCode);
viewHolder.listOfZipCodesInNear = (TextView) view.findViewById(R.id.listOfZipCodesInNear);
viewHolder.radioBtn = (RadioButton)view.findViewById(R.id.radioButton);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag() ;
}
viewHolder.radioBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(position != selectedPosition && selectedRB != null){
selectedRB.setChecked(false);
}
selectedPosition = position;
selectedRB = (RadioButton)v;
}
});
if(selectedPosition != position){
viewHolder.radioBtn.setChecked(false);
}else{
viewHolder.radioBtn.setChecked(true);
if(selectedRB != null && viewHolder.radioBtn != selectedRB){
selectedRB = viewHolder.radioBtn;
}
}
viewHolder.listOfZipCodesInNear.setText(list.get(position).getZipCodes());
viewHolder.cityOrZipCode.setText(list.get(position).getLocation());
viewHolder.radioBtn.setText(list.get(position).getLocation());
viewHolder.radioBtn.setChecked(list.get(position).isSelectedLocation());
return view;
}
}
问题是当我打开应用程序时,RadioButton
选择了正确的,但它处于非活动状态。只有正确显示。例如,
x 字符串1
o 字符串 2
o 字符串 3
当我单击 String2 时(最初选择了 String1),然后选择了 String2,但也选择了 String1。当我单击 String1,然后单击 String2 时,选择是正确的。
你知道有什么问题吗?非常感谢您的帮助!