rosu alin
rosu alin

Reputation: 5830

Adapter show one kind of row instead of another

This is the code to my 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;

}
}

Now this list looks similar to the one from the Android Settings page. It has headers, that only have the date in a TextView (Type.HEADER), and then it has buttons (Type.CLICKBLE). The problem is, that when it creates the list, after the second header ("Tuesday, Jan 12 2012"), the next button, which is CLICKBLE (item6) is created as a HEADER, and then the application crashes because, the HEADER has only 1 TextView, while the CLICKBLE type has 3 TextViews., so it crashes when i try to setText to the second TextView (being in HEADER, it doesn't exist). I need to know, why, the adapter, messes up my list, and it also messes up the type of the items. Currently with this code, i don't get a ANR error, but i cannot write the 2nd and 3rd texts to the CLICKBLE type.

Upvotes: 0

Views: 84

Answers (1)

Blackbelt
Blackbelt

Reputation: 157457

For what your attempt to achive you should also override getViewTypeCount() getItemViewType(int position). Call getItemViewType to choose what kind of view getView should use. getViewTypeCount has to return the number of different view you want to inflate (two in your case I think).

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 ;
}

Upvotes: 2

Related Questions