我制作了一个具有不同视图类型的自定义适配器。我显示两个视图是textview和radiogroup有可变按钮。我写了这段代码:
这是我的不同观点的模型类
public class OrthoScaleModel {
public static int SECONDARY_TEXTVIEW = 0;
public static int RADIOGROUP_TEXTVIEW = 1;
public int type;
public String[] radioButtonArray;
public String headingTitle;
public int numberOfRadioButtons;
public OrthoScaleModel(int type, String[] radioButtonArray, String headingTitle
, int numberOfRadioButtons) {
this.type = type;
this.radioButtonArray = radioButtonArray;
this.headingTitle = headingTitle;
this.numberOfRadioButtons = numberOfRadioButtons;
}
}
这是我的自定义适配器getView()方法代码
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
int type = getItemViewType(position);
OrthoScaleModel model = list.get(position);
for (String data : model.radioButtonArray){
Log.v("Data", data);
}
RadioButton[] buttons = new RadioButton[model.numberOfRadioButtons];
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
holder = new ViewHolder();
if (type == 0){
convertView = inflater.inflate(R.layout.secondary_heading, parent, false);
holder.textView = (TextView) convertView.findViewById(R.id.secondaryHeadingTextView);
convertView.setTag(holder);
} else if (type == 1){
convertView = inflater.inflate(R.layout.textview_radiogroup, parent, false);
holder.radioGroup = (RadioGroup) convertView.findViewById(R.id.radio_group1);
for (int i = 0; i < model.numberOfRadioButtons; i++){
buttons[i] = new RadioButton(context);
holder.radioGroup.addView(buttons[i]);
buttons[i].setText(list.get(position).radioButtonArray[i]);
buttons[i].setGravity(Gravity.TOP);
buttons[i].setPadding(0, 5, 5, 0);
buttons[i].setTextSize(18);
convertView.setTag(holder);
}
convertView.setTag(holder);
}
} else {
holder = (ViewHolder) convertView.getTag();
}
if (type == 0){
holder.textView.setText(model.headingTitle);
}
return convertView;
}
static class ViewHolder{
private TextView textView;
private RadioGroup radioGroup;
}
我用它来获取我的列表视图
ListView listView = (ListView) findViewById(R.id.listView);
String[] orthoScalesData = getResources().getStringArray(R.array.ortho_scales_data);
ArrayList<OrthoScaleModel> list1 = new ArrayList<>();
list1.add(new OrthoScaleModel(OrthoScaleModel.SECONDARY_TEXTVIEW, new String[]{}, orthoScalesData[1], 0));
list1.add(new OrthoScaleModel(OrthoScaleModel.SECONDARY_TEXTVIEW, new String[]{}, orthoScalesData[2], 0));
list1.add(new OrthoScaleModel(OrthoScaleModel.RADIOGROUP_TEXTVIEW, new String[]{orthoScalesData[3],
orthoScalesData[4], orthoScalesData[5], orthoScalesData[6], orthoScalesData[7], orthoScalesData[8]},
" ", 6));
list1.add(new OrthoScaleModel(OrthoScaleModel.SECONDARY_TEXTVIEW, new String[]{}, orthoScalesData[9], 0));
list1.add(new OrthoScaleModel(OrthoScaleModel.RADIOGROUP_TEXTVIEW, new String[]{orthoScalesData[10],
orthoScalesData[11], orthoScalesData[12], orthoScalesData[13], orthoScalesData[14], orthoScalesData[15]},
" ", 6));
CustomOrthoScaleAdapter adapter = new CustomOrthoScaleAdapter(this, list1);
listView.setAdapter(adapter);
但是在我的listview中,我收到了两个无线电组位置的单选按钮文本。请有人帮忙。
答案 0 :(得分:0)
在设置标记
之前,在for循环结束时添加holder.radioGroup.addView(buttons[i])
for (int i = 0; i < model.numberOfRadioButtons; i++){
buttons[i] = new RadioButton(context);
buttons[i].setText(list.get(position).radioButtonArray[i]);
buttons[i].setGravity(Gravity.TOP);
buttons[i].setPadding(0, 5, 5, 0);
buttons[i].setTextSize(18);
holder.radioGroup.addView(buttons[i]);
convertView.setTag(holder);
}
答案 1 :(得分:0)
getView方法的常见错误。首先,您应该获得convertView并最终为它们设置内容。试试这个..
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
int type = getItemViewType(position);
OrthoScaleModel model = list.get(position);
for (String data : model.radioButtonArray){
Log.v("Data", data);
}
RadioButton[] buttons = new RadioButton[model.numberOfRadioButtons];
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
holder = new ViewHolder();
if (type == 0){
convertView = inflater.inflate(R.layout.secondary_heading, parent, false);
holder.textView = (TextView) convertView.findViewById(R.id.secondaryHeadingTextView);
convertView.setTag(holder);
} else if (type == 1){
convertView = inflater.inflate(R.layout.textview_radiogroup, parent, false);
holder.radioGroup = (RadioGroup) convertView.findViewById(R.id.radio_group1);
convertView.setTag(holder);
}
} else {
holder = (ViewHolder) convertView.getTag();
}
///Added///////////////////////////////
if(holder.radioGroup!=null){
for (int i = 0; i < model.numberOfRadioButtons; i++){
buttons[i] = new RadioButton(context);
holder.radioGroup.addView(buttons[i]);
buttons[i].setText(list.get(position).radioButtonArray[i]);
buttons[i].setGravity(Gravity.TOP);
buttons[i].setPadding(0, 5, 5, 0);
buttons[i].setTextSize(18);
//convertView.setTag(holder);///not needed///
}
}
///Added///////////////////////////////
if (type == 0){
holder.textView.setText(model.headingTitle);
}
return convertView;
}