2011-04-12 85 views
0

我可以添加到数据库和列表作为一个列表视图。 当我使用onListItemClick单击一个列表项时,我需要获得什么声明 的值?onListItemClick,我需要什么声明来获取值?

在此先感谢。

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); 
} 
+0

参见http://stackoverflow.com/questions/5603429/how-to-get-value-from-database-onlistitemclick/5603584# 5603584 – rajath 2011-04-12 12:59:26

回答

8

首先,你需要点击的位置,以获得项目:

Cursor cursor = getListAdapter().getItem(position); 

然后你可以从这个游标数据(我不知道你用什么类型,所以就例如它将intString):

int id = cursor.getInt(cursor.getColumnIndex(_ID)); 
String description = cursor.getString(cursor.getColumnIndex(DESCRIPTION)); 

documentation的游标的使用方法。

0

添加到sergey答案,你也可以直接看到什么查看被点击,并使用findViewById()找到组件,因此,你想要的值。

这仅仅是一种替代是所述方法塞吉少得多的“片状”

相关问题