我设计了一个场景,根据我对Java的理解,应该很好,但遗憾的是没有。该场景在代码中解释:
ListViewAdapter
public final class ListViewAdapter extends BaseAdapter {
private Context context;
private RadioGroup[] radioGroups;
private List<String> listOfData;
public OneForAllListViewAdapter(Context context, List<String> listOfData) {
super();
this.context = context;
this.radioGroups = new RadioGroup[listOfData.size()];
this.listOfData = listOfData;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final String timelyOfferedStr = "Yes";
final String lateOfferedStr = "Yes but late";
final String notOfferedStr = "No";
final String pName = listOfData.get(position);
if(convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.one_for_all_list_item, parent, false);
}
TextView pNameTextView = (TextView) convertView.findViewById(R.id.one_for_all_p_name_TextView);
RadioButton timelyOffered = (RadioButton) convertView.findViewById(R.id.one_for_all_timely_offered);
RadioButton lateOffered = (RadioButton) convertView.findViewById(R.id.one_for_all_late_offered);
RadioButton notOffered = (RadioButton) convertView.findViewById(R.id.one_for_all_not_offered);
this.radioGroups[position] = (RadioGroup) convertView.findViewById(R.id.one_for_all_radio_group);
pNameTextView.setText(pName);
timelyOffered.setText(timelyOfferedStr);
lateOffered.setText(lateOfferedStr);
notOffered.setText(notOfferedStr);
return convertView;
}
@Nullable
public ThatStatus[] getThoseStatuses()
{
ThatStatus[] thoseStatuses = new ThatStatus[radioGroups.length];
for(int i=0; i<radioGroups.length; i++) {
int selectedRadioButton = radioGroups[i].getCheckedRadioButtonId();
switch (selectedRadioButton) {
case R.id.one_for_all_timely_offered:
thoseStatuses [i] = ThatStatus.TimelyOffered;
break;
case R.id.one_for_all_late_offered:
thoseStatuses [i] = ThatStatus.Offered;
break;
case R.id.one_for_all_not_offered:
thoseStatuses [i] = ThatStatus.NotOffered;
break;
default:
return null;
}
}
return thoseStatuses;
}
}
上面代码中需要注意的重点是这一行:
this.radioGroups[position] = (RadioGroup) convertView.findViewById (R.id.one_for_all_radio_group);
我将所有RadioGroup
保存在RadioGroup
数组中,而getThoseStatuses()
我正在尝试从那些RadioButton
中获取已检查的RadioGroup
}秒。但是radioGroups[i].getCheckedRadioButtonId()
总是让我回归-1。
我错过了一些Java概念吗?这里似乎有什么问题?
答案 0 :(得分:0)
这是一个设计缺陷。您应该在模型中不存储视图。将数据存储在模型中。 而不是
this.radioGroups[position] = (RadioGroup) convertView.findViewById(R.id.one_for_all_radio_group);
根据您的模型设置RadioGroup的检查状态:
model = getItem(position);
switch(model.offered) {
case 0:
radioGroup.check(R.id.one_for_all_timely_offered);
break;
case 1:
radioGroup.check(R.id.one_for_all_late_offered);
break;
...
将所有数据保存在视图模型中。
考虑扩展ArrayListAdapter。访问适配器的项目而不是输入数据。
答案 1 :(得分:0)
尝试为您的Apapter创建一个View Houlder Pattern。
例如:将你的getView更改为
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Viewhoulder viewHoulder = null;
final String timelyOfferedStr = "Yes";
final String lateOfferedStr = "Yes but late";
final String notOfferedStr = "No";
final String pName = listOfData.get(position);
if(convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.one_for_all_list_item, parent, false);
viewHoulder = new Viewhoulder ();
viewHoulder.pNameTextView = (TextView) convertView.findViewById(R.id.one_for_all_p_name_TextView);
viewHoulder.timelyOffered = (RadioButton) convertView.findViewById(R.id.one_for_all_timely_offered);
viewHoulder.lateOffered = (RadioButton) convertView.findViewById(R.id.one_for_all_late_offered);
viewHoulder.notOffered = (RadioButton) convertView.findViewById(R.id.one_for_all_not_offered);
viewHoulder.radioGroup = (RadioGroup) convertView.findViewById(R.id.one_for_all_radio_group);
}
else
viewHoulder = (Viewhoulder )convertView .getTag();
this.radioGroups[position] = viewHoulder .radioGroup ;
viewHoulder . pNameTextView.setText(pName);
viewHoulder .timelyOffered.setText(timelyOfferedStr);
viewHoulder .lateOffered.setText(lateOfferedStr);
viewHoulder .notOffered.setText(notOfferedStr);
return convertView;
}
在适配器中创建此Viewhoulder类
static class Viewhoulder {
private TextView pNameTextView;
private RadioButton timelyOffered;
private RadioButton lateOffered ;
private RadioButton notOffered;
private RadioGroup radioGroup;
}