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. Indosat tidak bisa tweeter

Setelah 3 hari error trs,,,Akhirnya bisa pake seesmic,,dikira hh nya yg
error..
Berkat info2 suhu2 nih..

Spica

-- 
===============

2. [WTS] Dell Strak BNIB 4.449jt

Bantuin jualan temen,

Dell Streak BNIB 4.449jt garansi resmi indo

Yg tertarik silahkan kontak ke penjualnya langsung di 08568615339 (BJ)
ato di www.facebook.com/bjshop tersedia beberapa gedget lainnnya di sana

Terima Kasih.

-- 
"

3. Intel Android Tablet Siap Meluncur!

4. Fragment Support forward compatibility?

5. Android kernel2.6.32 source code

6. new to andriod

7. java/lang/IllegalStateException