How to add an item to SimpleCursorAdapter?
Asked Answered
P

3

2

I have a simple database table with 2 columns "_id" and "title". and I'm displaying the data in a spinner, and it works well.

but I need to add one more item at the top of the spinner list that is not from the database with id = 0 and title = "not specified";

Spinner list = (Spinner) findViewById(R.id.spinner);
Cursor cursor = database.getAll(); // returns cursor with objects

String[] columns = new String[] {"title"};
int[] to = new int[] {R.id.title};

list.setAdapter(new SimpleCursorAdapter(this, R.layout.object_item_simple, cursor, columns, to));

I need to know the selected item id from the database, i can do this with list.getSelectedItemId();

so I can't use ArrayAdapter instead of SimpleCursorAdapter, because i don't think that there is a method for setting the id for each item on the adapter. is there a way to do this?

Thanks.

Pompidou answered 14/12, 2011 at 17:10 Comment(0)
K
2

You could create an object out of your id and title and build a list of these objects with the cursor. Then insert your artificial entry at the top top of that list.

Then when you construct your Adapter pass in this list.

Alternatively you could put a dummy value into your database, although that would be weird and maybe not possible depending on your query and data. The ArrayAdapter is much more sensible

Kavanagh answered 14/12, 2011 at 17:26 Comment(0)
L
1

How to Do This With SimpleCursorAdapter

This method:

  • Is Efficient
  • Works with standard CursorLoader and SimpleCursorAdapter idioms
  • Great with ContentProvider data

I create the item i want to insert into the cursor as a static MatrixCursor

private static final MatrixCursor PLATFORM_HEADER_CURSOR = new MatrixCursor(
        //These are the names of the columns in my other cursor
        new String[]{ 
                DataContract.ReflashPackage._ID,
                DataContract.ReflashPackage.COLUMN_PLATFORM
        });
static {
    PLATFORM_HEADER_CURSOR.addRow(new String[]{
            "0",
            "Select a Platform")
    });
}

Here is my implementation of onLoadFinished which merges the cursor and passes it to the adapter.

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    switch (loader.getId()) {
        case PLATFORM_CURSOR_LOADER_ID:
            Cursor mergedCursor = addPlatformHeaderToCursor(data);
            mPlatformAdapter.swapCursor(mergedCursor);
            break;
    }
}

@NonNull
private static Cursor addPlatformHeaderToCursor(Cursor platforms) {
    Cursor[] cursorToMerge = new Cursor[2];
    cursorToMerge[0] = PLATFORM_HEADER_CURSOR;
    cursorToMerge[1] = platforms;
    return new MergeCursor(cursorToMerge);
}
Lazaretto answered 3/6, 2015 at 16:3 Comment(0)
L
0

One technique that I use often is I will define an object (such as EntryObject) that has the variables I am going to need from the cursor to display. Once I have this I can iterate through the cursor and place the information into those EntryObjects and place them in an ArrayList or an array.

Then you can build a customer ArrayAdapter that will work with your new object to pull as much data as you need and display it how you want to.

Laurynlausanne answered 14/12, 2011 at 17:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.