Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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..

like image 574
Sagar Maiyad Avatar asked May 29 '26 08:05

Sagar Maiyad


1 Answers

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.

like image 175
Robin Avatar answered Jun 01 '26 00:06

Robin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!