这是我的适配器的代码:
public class LogHistoryAdapter extends BaseAdapter {
private Context context = null;
private ArrayList<LogListItem> logList = null;
public LogHistoryAdapter(Context context) {
this.context = context;
logList = new ArrayList<LogHistoryAdapter.LogListItem>();
LogListItem item1 = new LogListItem(Type.HEADER, "", "", "", "Wednesday, Jan 13 2012");
logList.add(item1);
LogListItem item2 = new LogListItem(Type.CLICKBLE, "ID ", "bla" , "Range 1m" , "");
logList.add(item2);
LogListItem item3 = new LogListItem(Type.CLICKBLE, "ID ", "bla" , "Range 1m" ,"");
logList.add(item3);
LogListItem item4 = new LogListItem(Type.CLICKBLE, "ID ", "bla" , "Range 1m", "" );
logList.add(item4);
LogListItem item5 = new LogListItem(Type.HEADER, "", "", "", "Tuesday, Jan 12 2012");
logList.add(item5);
LogListItem item6 = new LogListItem(Type.CLICKBLE, "ID ", "bla" , "Range 1m" , "");
logList.add(item6);
LogListItem item7 = new LogListItem(Type.CLICKBLE, "ID ", "bla" , "Range 1m" ,"");
logList.add(item7);
LogListItem item8 = new LogListItem(Type.CLICKBLE, "ID ", "bla" , "Range 1m" , "");
logList.add(item8);
}
public enum Type {
HEADER, CLICKBLE;
}
public class LogListItem {
Type type;
String name;
String details;
String id;
String header;
public LogListItem(Type type, String id, String name, String details, String header) {
this.type = type;
this.name = name;
this.id = id;
this.details = details;
this.header = header;
}
}
public int getCount() {
return logList.size();
}
public Object getItem(int position) {
return logList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View rowView, ViewGroup parent) {
final SettingsViewHolder holder;
LogListItem currentObj = logList.get(position);
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.row_settings_log, parent, false);
holder = new SettingsViewHolder();
switch (currentObj.type) {
case HEADER:
holder.layout = (RelativeLayout) rowView.findViewById(R.id.settingsItemLayoutLog);
holder.text = (TextView) rowView.findViewById(R.id.settingsItemLog);
break;
case CLICKBLE:
holder.layout = (RelativeLayout) rowView.findViewById(R.id.settingsSubitem1LayoutLog);
holder.text = (TextView) rowView.findViewById(R.id.settingsSubItem1TxtViewLog);
holder.text2 = (TextView) rowView.findViewById(R.id.settingsSubItem1TxtView2Log);
holder.text3 = (TextView) rowView.findViewById(R.id.settingsSubItem1TxtView3Log);
break;
default:
break;
}
rowView.setTag(holder);
} else {
holder = (SettingsViewHolder) rowView.getTag();
}
switch (currentObj.type) {
case HEADER:
LogService.log("header", "header: " + currentObj.header);
LogService.log("header", "id: " + currentObj.id);
LogService.log("header", "name: " + currentObj.name);
LogService.log("header", "details: " + currentObj.details);
LogService.log("=====", "===============================");
holder.layout.setVisibility(View.VISIBLE);
holder.text.setText(currentObj.header);
break;
case CLICKBLE:
holder.layout.setVisibility(View.VISIBLE);
LogService.log("clickable", "header: " + currentObj.header);
LogService.log("clickable", "id: " + currentObj.id);
LogService.log("clickable", "name: " + currentObj.name);
LogService.log("clickable", "details: " + currentObj.details);
LogService.log("clickable", "===============================");
holder.text.setText(currentObj.id);
// holder.text2.setText(currentObj.name);
// holder.text3.setText(currentObj.details);
break;
default:
break;
}
return rowView;
}
static class SettingsViewHolder {
CheckBox checked;
TextView text;
TextView text2;
TextView text3;
RelativeLayout layout;
}
}
现在,此列表看起来类似于 Android 设置页面中的列表。它有标题,只有 TextView (Type.HEADER) 中的日期,然后它有按钮 (Type.CLICKBLE)。问题是,当它创建列表时,在第二个标题(“Tuesday, Jan 12 2012”)之后,下一个按钮,即 CLICKBLE(item6)被创建为 HEADER,然后应用程序崩溃,因为 HEADER只有 1 个 TextView,而 CLICKBLE 类型有 3 个 TextView。所以当我尝试将 setText 设置为第二个 TextView 时它崩溃(在 HEADER 中,它不存在)。我需要知道,为什么适配器会弄乱我的列表,并且还会弄乱项目的类型。目前使用此代码,我没有收到 ANR 错误,但我无法将第二个和第三个文本写入 CLICKBLE 类型。