Return object from CursorAdapter.get()
Asked Answered
S

3

9

I'm overriding CursorAdapter and I need to get the last item, problem is that CursorAdapter has actually a get() method...but source is a db and it returns a plain object!! (I don't even know what is it, I'd expect it returning a Cursor object instead...)

Neverthless, how can I make it return an instance of my Wrapper db row class?

Example: say my db has rows like these:

id|first name| surname

I'd make a class Person from that.

Now I'd like to have a Person get(int i) method from cursor adapter...

Sixtyfourmo answered 29/8, 2012 at 15:46 Comment(0)
M
21

Now I'd like to have a Person get(int i) method from cursor adapter...

This seems like a strange request. I would pass the Cursor itself (or the Cursor returned from CursorAdapter's getItem()) to a regular method in my Activity instead. But here are the basic steps to create a Person get() method.

Create your Person class:

public class Person {
    long id;
    String firstName;
    String surname;
}

And in your custom CursorAdapter simply use a method like this:

public Person get(int position) {
    Cursor cursor = getCursor();
    Person person;
    if(cursor.moveToPosition(position)) {
        person = new Person();
        person.id = cursor.getLong(cursor.getColumnIndex("_id"));
        person.firstName = cursor.getString(cursor.getColumnIndex("firstName"));
        person.surname = cursor.getString(cursor.getColumnIndex("surname"));
        results.add(person);
    }

    return person;
}
Mudskipper answered 29/8, 2012 at 16:42 Comment(0)
W
44

well just use the adapter.getItem() and cast it to Cursor, and there is no need to move the cursor manually like in accepted answer

Cursor cursor = (Cursor) myCursorAdapter.getItem(position);
String myColumnValue = cursor.getString(cursor.getColumnIndex("YOUR_COLUMN_NAME"));
Westley answered 25/12, 2012 at 2:44 Comment(1)
Superb! Never knew. Saved several hours of my life!Palaver
M
21

Now I'd like to have a Person get(int i) method from cursor adapter...

This seems like a strange request. I would pass the Cursor itself (or the Cursor returned from CursorAdapter's getItem()) to a regular method in my Activity instead. But here are the basic steps to create a Person get() method.

Create your Person class:

public class Person {
    long id;
    String firstName;
    String surname;
}

And in your custom CursorAdapter simply use a method like this:

public Person get(int position) {
    Cursor cursor = getCursor();
    Person person;
    if(cursor.moveToPosition(position)) {
        person = new Person();
        person.id = cursor.getLong(cursor.getColumnIndex("_id"));
        person.firstName = cursor.getString(cursor.getColumnIndex("firstName"));
        person.surname = cursor.getString(cursor.getColumnIndex("surname"));
        results.add(person);
    }

    return person;
}
Mudskipper answered 29/8, 2012 at 16:42 Comment(0)
B
0

Well, i had to debug to find the answer. Suppose you have something like this with

SimpleCursorAdapter

themesAdapter = new SimpleCursorAdapter(getContext(), R.layout.spinner_with_count, null,
                new String[]{ThemeData.COLUMN_ID, ThemeData.COLUMN_SET_COUNT}, new int[] { R.id.spinnerTxLabel, R.id.spinnerTxCount }, 0);
        inputTheme.setAdapter(themesAdapter);

Then you have to retrieve like this way.

inputTheme.setOnItemSelectedListener(new OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                    // if not all themes, get subthemes items
                    if(position > 0){

                            // load subtheme
                            Cursor currentCursor = (Cursor)inputTheme.getAdapter().getItem(position);
                            if(currentCursor != null) {
                                String value = currentCursor.getString(currentCursor.getColumnIndexOrThrow(Data.COLUMN_ID));
                            }
                        }
                    }

};

Bitters answered 19/6, 2020 at 10:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.