所以我列出了不同类型的单元格,以及应该包含的内容。我试图加载每个,但我的适配器只会加载这么多,然后重复这些单元格。这是我的适配器类
public class FormCellAdapter extends ArrayAdapter<Cell> {
private final Context context;
private static class ViewHolder {
// All cells
TextView questionLabel;
// Checkbox cell
LinearLayout checkGroup;
// Date cell
Button dateButton;
// Dropdown cell
Spinner spinner;
// Slider cell
SeekBar seekBar;
TextView sliderLabel;
// Text area cell
EditText textArea;
// Dropdown cell, text field cell, text field toggle cell
EditText textField;
// Text field toggle cell, toggle cell
RadioGroup radioGroup;
}
public FormCellAdapter(Context context, ArrayList<Cell> cells) {
super(context, 0, cells);
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Cell cell = getItem(position);
final int layout = cell.getLayoutFile();
final CellType type = cell.getCellType();
final ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(layout, parent, false);
viewHolder.questionLabel = (TextView) convertView.findViewById(R.id.questionTextLabel);
viewHolder.questionLabel.setText(cell.getQuestion());
switch (type) {
case CHECKBOX:
viewHolder.checkGroup = (LinearLayout) convertView.findViewById(R.id.checkGroup);
break;
case DATE:
viewHolder.dateButton = (Button) convertView.findViewById(R.id.dateButton);
break;
case DROPDOWN:
viewHolder.spinner = (Spinner) convertView.findViewById(R.id.spinner);
viewHolder.textField = (EditText) convertView.findViewById(R.id.textField);
break;
case SLIDER:
viewHolder.seekBar = (SeekBar) convertView.findViewById(R.id.seekbar);
viewHolder.sliderLabel = (TextView) convertView.findViewById(R.id.sliderLabel);
break;
case TEXTAREA:
viewHolder.textArea = (EditText) convertView.findViewById(R.id.textArea);
break;
case TEXTFIELD:
viewHolder.textField = (EditText) convertView.findViewById(R.id.textField);
break;
case TEXTFIELDTOGGLE:
viewHolder.radioGroup = (RadioGroup) convertView.findViewById(R.id.radioGroup);
viewHolder.textField = (EditText) convertView.findViewById(R.id.textField);
case TOGGLE:
viewHolder.radioGroup = (RadioGroup) convertView.findViewById(R.id.radioGroup);
break;
}
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// This will be filled in later, populating the labels and whatever else needs done.
switch (type) {
case CHECKBOX:
break;
case DATE:
break;
case DROPDOWN:
break;
case SLIDER:
break;
case TEXTAREA:
break;
case TEXTFIELD:
break;
case TEXTFIELDTOGGLE:
case TOGGLE:
break;
}
return convertView;
}
}
我已经从中删除了很多代码,因为我想我也可以重新开始。在它随机选择进入的细胞之前,通常在向下滚动然后再向上移动时顶部的细胞会有所不同。
任何想法,为什么我没有看到我的所有细胞,以及为什么他们会重复自己?
答案 0 :(得分:0)
覆盖getCount()以返回要显示的单元格数。
答案 1 :(得分:0)
您的问题是您正在使用ViewHolder
模式。使用ViewHolder模式的目的是将布局文件jsut膨胀一次,然后在下一个列表项中重用该膨胀的视图。为了扩展不同的布局文件,每次布局文件在getView
中更改时,都必须对它们进行膨胀。
您的getView
方法将如下所示:
// All cells
TextView questionLabel;
// Checkbox cell
LinearLayout checkGroup;
// Date cell
Button dateButton;
// Dropdown cell
Spinner spinner;
// Slider cell
SeekBar seekBar;
TextView sliderLabel;
// Text area cell
EditText textArea;
// Dropdown cell, text field cell, text field toggle cell
EditText textField;
// Text field toggle cell, toggle cell
RadioGroup radioGroup;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Cell cell = getItem(position);
final int layout = cell.getLayoutFile();
final CellType type = cell.getCellType();
View view = LayoutInflater.from(context).inflate(layout, parent, false);
questionLabel = (TextView) view.findViewById(R.id.questionTextLabel);
questionLabel.setText(cell.getQuestion());
switch (type) {
case CHECKBOX:
checkGroup = (LinearLayout) view.findViewById(R.id.checkGroup);
break;
case DATE:
dateButton = (Button) view.findViewById(R.id.dateButton);
break;
case DROPDOWN:
spinner = (Spinner) view.findViewById(R.id.spinner);
textField = (EditText) view.findViewById(R.id.textField);
break;
case SLIDER:
seekBar = (SeekBar) view.findViewById(R.id.seekbar);
sliderLabel = (TextView) view.findViewById(R.id.sliderLabel);
break;
case TEXTAREA:
textArea = (EditText) view.findViewById(R.id.textArea);
break;
case TEXTFIELD:
textField = (EditText) view.findViewById(R.id.textField);
break;
case TEXTFIELDTOGGLE:
radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
textField = (EditText) view.findViewById(R.id.textField);
case TOGGLE:
radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
break;
}
// This will be filled in later, populating the labels and whatever else needs done.
switch (type) {
case CHECKBOX:
break;
case DATE:
break;
case DROPDOWN:
break;
case SLIDER:
break;
case TEXTAREA:
break;
case TEXTFIELD:
break;
case TEXTFIELDTOGGLE:
case TOGGLE:
break;
}
return view;
}