1

I am trying to achieve a sort of alternating row effect on my ListView using a CursorAdapter, that is so certain rows with lets say number data I would like to use blue.xml for its layout while for text rows I would like to use red.xml for their layout. I am getting data from Sqlite so I figured it might be better to use a CursorAdapter, although I was wondering if the CursorAdapter has a significantly optimized implementation since it uses the cursor from a query compared to using a BaseAdapter. So I am looking for some assistance getting this alternating row effect for the different types of rows: eg.

Text - red_row
Text - red_row
Numbers - blue_row
Text - red_row
Numbers - blue_row
Numers - blue_row
Text - red_row

     @Override
    public View newView( Context context, Cursor cursor, ViewGroup parent ) {
        View retView;
        Holder  holder = new Holder();
        LayoutInflater inflater = LayoutInflater.from( context );


    if(cursor.getString( cursor.getColumnIndex( "from" ) ).equals( myId )){

        retView = inflater.inflate( R.layout.row_red, parent, false);
        holder.message = (TextView) retView.findViewById(R.id.msg1);
        holder.time = (TextView) retView.findViewById(R.id.msg2);
        holder.viewType = 1;
    }else{
        retView = inflater.inflate( R.layout.row_blue, parent, false);
        holder.message = (TextView) retView.findViewById(R.id.txt1);
        holder.time = (TextView) retView.findViewById(R.id.txt1);
        holder.viewType = 2;
    }


    retView.setTag( holder );
    return retView;
}

@Override
public void bindView( View view, Context context, Cursor cursor ) {
    Holder  holder = (Holder)view.getTag();
    String message = cursor.getString( cursor.getColumnIndex( "text" ) );
    String time =   cursor.getString( cursor.getColumnIndex( "time" ) );

        holder.message.setText( message );
        holder.time.setText( time );

}


static class Holder{
    TextView message;
    TextView time;
    int viewType;
}

I attempted the code above and it does work for all visible rows, but once data has to be scrolled all the newly populated rows are showing the wrong layout and I have examined the data in sqlite and the row ID's that I want to be a certain layout are correct, not sure what would cause this exactly but I narrowed it down to something in the newView method or maybe the query is just too slow for the rate of scroll causing the wrong layout to be inflated. Any help is appreciated.

kabuto178
  • 3,129
  • 3
  • 40
  • 61

1 Answers1

0

Solution 1:

bypass the default implementation of cursor adapter and overwrite

public View getView(int position, View convertView, ViewGroup parent) {...}

getViewTypeCount() { return 2}

@Override
public int getItemViewType(int position) {
    return BLUE_vs_RED_logic(position); // return 0 or 1
}   

like for a normal adapter. It should work fine. (disclaimer: I never did it, but look at https://stackoverflow.com/a/8617766/154272)

Solution 2:

assuming the differences between the two view are not too big (from a memory usage point of view).

Create a meta row layout that contain both layout. then in the bindView hide/show what you need.

Recommendation

If you do not use images, I would recommend you the solution number 2.

Community
  • 1
  • 1
Loda
  • 1,970
  • 2
  • 20
  • 40