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. How to enable Fingerpainting (i.e. API demo example) on a surface, but not only with finger touch



Only if, by software emulation, you can change the laws of physics. If
you figure out how to do that, Roland Emmerich would like to have a word
with you.

Now, when you slide your finger on the screen, whether the software
draws a fat line or a thin line or a dashed line or a dotted line or
whatever is up to the software. So, the *results* on the screen may be
whatever you wish to draw with your finger, but the drawing still needs
to be done by a finger.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

_The Busy Coder's Guide to Android Development_ Version 2.8
Available!

-- 

2. Contradictory documentation - Is it ok to override ViewGroups.invalidateChildInParent() ?

The SDK reference documentation (from the comment in the class) says
it isn't, but it's not declared as final as the other non-overridable
functions are in ViewGroup.  Whilst this isn't a reason to doubt the
documentation by itself, one of the SDK examples overrides it:

http://developer.android.com/guide/samples/ApiDemos/src/com/example/android/apis/graphics/PictureLayout.html

It is also the only way currently to get notified of a child View
being invalidated.

Can someone please confirm whether this is intended for overriding or
not?

Cheers
Simon

-- 

3. 2.0 AccountManager - Implementing a new account type with Google, Facebook and Exchange

4. Hi...

5. Sending raw query or SQLitedatabase.query to a database that only is reachable through ContentResolver

6. Size of display

7. Access voice before sending