cursoradapter with different row layouts

不问归期 提交于 2019-11-26 15:17:00

问题


I'm trying to create a custom cursoradapter that will use two different layouts depending on some data in the cursor. I keep reading about 'overriding getViewTypeCount() and getItemViewType()' to make this happen but I can't seem to figure out how to implement this..

This is my code for the bindView and new View methods:

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView tView = (TextView) view.findViewById(R.id.TextView1);
    tView.setText("The text");
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return mInflater.inflate(R.layout.item1, parent, false);
}

.

EDIT: Now I got this working, but I want to choose what rowlayout to use depending on some data in the cursor and I can't get that working.. anybody got some ideas?

@Override
public int getItemViewType(int position) {
    return position % 2;
}

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.txtAddress
            .setText("blabla");
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    ViewHolder holder = new ViewHolder();
    View v = null;
    int type = cursor.getPosition() % 2;
    if(type == 0) {
        v = mInflater.inflate(R.layout.item, parent, false); 
    } else {
        v = mInflater.inflate(R.layout.item2, parent, false);
    }

    holder.txtAddress = (TextView) v.findViewById(R.id.tvName);

    v.setTag(holder);
    return v;
}

回答1:


So I finally got it work. For the ones interested the working code is below:

private int getItemViewType(Cursor cursor) {
    String type = cursor.getString(cursor.getColumnIndex("type"));
    if (type.equals("1")) {
        return 0;
    } else {
        return 1;
    }
}

@Override
public int getItemViewType(int position) {
    Cursor cursor = (Cursor) getItem(position);
    return getItemViewType(cursor);
}

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.textView
            .setText(cursor.getString(cursor.getColumnIndex("body")));
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    ViewHolder holder = new ViewHolder();
    View v = null;

    if (cursor.getString(cursor.getColumnIndex("type")).equals("1")) {
        v = mInflater.inflate(R.layout.message1, parent, false);
        holder.textView = (TextView) v
                .findViewById(R.id.textView1);
    } else {
        v = mInflater.inflate(R.layout.message2, parent, false);
        holder.textView = (TextView) v
                .findViewById(R.id.textView2);
    }

    v.setTag(holder);
    return v;
}

public static class ViewHolder {
    public TextView textView;
}



回答2:


Take a look to this example, you can easily adapt it for solving your problem. It's pretty straightforward.




回答3:


Another possible solution regarding the access to the cursor in the getItemViewType method is the following:

@Override
public int getChildType(int groupPosition, int childPosition) {
    Cursor c = getChild(groupPosition, childPosition);
    if(c.getString(c.getColumnIndex(Contract.MyColumn)).equals("value"))
        return 0;
    else return 1;
}


来源:https://stackoverflow.com/questions/8479833/cursoradapter-with-different-row-layouts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!