SimpleCursorAdapter and OnListItemClick - Simple question

by Gavin Aiken » Mon, 09 Mar 2009 03:57:05 GMT


Sponsored Links
 Hi guys,

I use a SimpleCursorAdapter like this;

        @Override
        public void onCreate(Bundle icicle) {
                super.onCreate(icicle);
                this.getIntent().getIntExtra("category", -1);
                Integer category = this.getIntent().getIntExtra("category",
-1);
                SqliteFilterMediator mediator = new
SqliteFilterMediator(this);
                ListAdapter adapter =
                        new SimpleCursorAdapter(this,
                                        R.layout.row,
                                        c,
                                        new String[] {column},
                                        new int[] {R.id.name});
                setListAdapter(adapter);
        }

        public void onListItemClick(ListView parent, View v, int position,
long id) {
           // Position doesn't necessarily map to _id
            setResult(RESULT_OK, this.getIntent().putExtra("_id",
position));
            finish();
        }

I want to return the 'id' which is paired with the text on the ListItem in
the database (And in the cursor). Currently I return the position of the
list item clicked.

The cursor might return items with ids that are out of order with the
position of the list, or it could contain gaps in the sequence of integers,
as such the position isn't enough to map to the id of the row selected in
the database so that I can act acordingly.

I hope my situation makes sense, I think it might involve displaying the id,
casing the view clicked and somehow grabbing the displayed integer back as
the id, this seems a bit ridiculous though so I'm hoping there is a simpler
way to retrieve the id without making another database query.

Many thanks!

Gav

--~--~---------~--~----~------------~-------~--~----~



SimpleCursorAdapter and OnListItemClick - Simple question

by Gavin Aiken » Mon, 09 Mar 2009 05:58:03 GMT


 This works for me;

    private Cursor c;
    @Override
    public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            Integer category = this.getIntent().getIntExtra("category", -1);
            mediator = new SqliteMediator(this);
            c = mediator.getFitlerNames(category);
            ListAdapter adapter =
                    new SimpleCursorAdapter(this,
                                    R.layout.row,
                                    c,
                                    new String[] {column},
                                    new int[] {R.id.name});
            setListAdapter(adapter);
    }

    public void onListItemClick(ListView parent, View v, int position, long
id) {
        c.moveToPosition(position);
        int filterId = c.getInt(0);
        c.close();
        mediator.onStop();
        setResult(RESULT_OK, new Intent().putExtra("_id", filterId));
        finish();
    }





--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Other Threads

1. Pagi ; tanya backup

Pagi semua..........

Mau tanya nih, sama or beda sih antara Backup MemoryCard dgn Copy-Paste (termasuk hiden-file) MemoryCard-nya dah Swap/Partisi -- Jus* ~ send using pc jangkrik krik krik ~ -- "Indonesian Android Community [id-android]"

2. A little bird, not the blue bird twitter...had informed me...

Wah bsk g ke GI ada janji sih jam 6an
btw thx

~ white dream ~





Kalo hari Sabtu 27 Feb 2010 di Fountain Area Grand Indonesia akan ada
launching Samsung Galaxy Spica dengan harga 2.999.000 plus pilihan berbagai
paket data dari semua operator (abjat: 3, AXIS, Indosat, Telkomsel, XL)

So...ga usah lama kalo yg udah keracunan sama android & merasa android koq
mahal banged....

Juga...si spica ini....kalo mau 2.1 ecclair, udah ada unofficial romnya ;)


Enjoy!


Ps: am just a messenger, please don't shoot me. ;)


_____
have you googled anything today?

--
"Indonesian Android Community [id-android]"

3. Twicca

4. About metastate of hard keyboard

5. Please allow me to introduce myself.

6. Root Xperia

7. Salam kenal - kepincut bintang