适配器显示一种行而不是另一种

时间:2012-12-14 15:50:30

标签: android list arraylist adapter

这是我的适配器的代码:

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)。问题是,当它创建列表时,在第二个标题之后(“2012年1月12日星期二”),下一个按钮,即CLICKBLE(item6)被创建为HEADER,然后应用程序崩溃,因为,HEADER只有1个TextView,而CLICKBLE类型有3个TextViews。所以当我尝试将setText设置为第二个TextView时(在HEADER中,它不存在)它会崩溃。 我需要知道,为什么,适配器,弄乱了我的列表,它也弄乱了项目的类型。 目前使用此代码,我没有得到ANR错误,但我无法将第2和第3文本写入CLICKBLE类型。

1 个答案:

答案 0 :(得分:2)

为了实现您的目标,您还应该覆盖getViewTypeCount() getItemViewType(int position)。致电getItemViewType以选择getView应使用哪种视图。 getViewTypeCount必须返回您要膨胀的不同视图的数量(我认为这两个视图)。

public static final int NORMAL_ROW = 0;
public static final int DARKGRAY_ROW = 1;
@override
public int getViewTypeCount() {
      return 2;
}

@override
public int getItemViewType(int position) {
  LogListItem currentObj = logList.get(position);
  return (currentObj.type == HEADER) ? NORMAL_ROW :  DARKGRAY_ROW ;
}