Sagar Maiyad
Sagar Maiyad

Reputation: 12733

Convert object arraylist to cursor in android

POJO CLASS GridItem:

public class GridItem {

    int id;

    String path;
    String name;
    String chr;
    int headerId;


    public GridItem(String path, String name, String chr){
        this.path = path;
        this.name = name;
        this.chr = chr;
    }
}

In activity:

ArrayList<GridItem> books = new ArrayList<GridItem>(); 

Now i want to convert this arraylist into cursor for making sectionindexer into gridview. so that i use that cursor in below code:

static class YOUR_ADAPTER extends SimpleCursorAdapter implements SectionIndexer {

private AlphabetIndexer mIndexer;

 YOUR_ADAPTER (Context context, AlbumBrowserActivity currentactivity,
            int layout, Cursor cursor, String[] from, int[] to) {
        super(context, layout, cursor, from, to);

        getColumnIndices(cursor);
    } 

    private void getColumnIndices(Cursor cursor) {
        if (cursor != null) {
            YOUR_COLUMN = cursor.getColumnIndexOrThrow(WHAT_YOU'RE_SORTING);

            if (mIndexer != null) {
                mIndexer.setCursor(cursor);
            } else {
                mIndexer = new AlphabetIndexer(cursor, YOUR_COLUMN, mResources.getString(
                        R.string.fast_scroll_alphabet));
            }
        }
    }

 public Object[] getSections() {
        return mIndexer.getSections();
    }

 public int getPositionForSection(int section) {
        return mIndexer.getPositionForSection(section);
    }

 public int getSectionForPosition(int position) {
        return 0;
    }
}

if anyone have solution then make it public here..

Upvotes: 3

Views: 4464

Answers (1)

Robin
Robin

Reputation: 10368

You might need to use MatrixCursor, it is a mutable cursor implementation backed by an array of Objects. Use newRow() to add rows. Automatically expands internal capacity as needed.

Upvotes: 2

Related Questions