Re: [android-developers] How to get value from ListView onListItemClick?

2010-09-11 Thread Mark Murphy
On Sat, Sep 11, 2010 at 7:54 AM, Mystique  wrote:
> Hi, i can add to a db and list as a listview.
> When I click a list item using onListItemClick, what statement do I
> need to get the value?

:: snip ::

>    public void onListItemClick(ListView parent, View v, int position,
> long id) {
>
>        // What statement to put here to get the value of _ID,
> DESCRIPTION, UN
>        // selected?
>    }

The fourth parameter (id) is _ID. The third parameter (position) can
be used with moveToPosition() on your Cursor to access anything else
you may need.

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

_The Busy Coder's Guide to *Advanced* Android Development_ Version 1.9
Available!

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


[android-developers] How to get value from ListView onListItemClick?

2010-09-11 Thread Mystique
Hi, i can add to a db and list as a listview.
When I click a list item using onListItemClick, what statement do I
need to get the value?

Thanks in advance.

---code---
public class Main extends ListActivity {
private static String[] FROM = { _ID, DESCRIPTION, UN };
private static String ORDER_BY = DESCRIPTION + " ASC";
private static int[] TO = {R.id.itemrowid, R.id.itemdescription,
R.id.itemun };
private EventsData events;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
events = new EventsData(this);
try {
Cursor cursor = getEvents();
showEvents(cursor);
 } finally {
events.close();
 }

public void onListItemClick(ListView parent, View v, int position,
long id) {

// What statement to put here to get the value of _ID,
DESCRIPTION, UN
// selected?
}

private Cursor getEvents() {
// Perform a managed query. The Activity will handle closing
// and re-querying the cursor when needed.
SQLiteDatabase db = events.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null,
null, ORDER_BY);
startManagingCursor(cursor);
return cursor;
}

private void showEvents(Cursor cursor) {
// Set up data binding
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.item, cursor, FROM, TO);
setListAdapter(adapter);
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en