2012-01-30 42 views
0

當我點擊一個ListView項目時,我收到以下消息: 如何讀出項目並將項目名稱轉換爲字符串?爲什麼它無法讀取ListView?

[email protected]

public void onListItemClick(
ListView parent, View v, 
int position, long id) 
{ 
    Object o = this.getListAdapter().getItem(position); 
    String keyword = o.toString(); 

    Toast.makeText(this, keyword, Toast.LENGTH_SHORT).show(); 

} 

這樣我可以讀從項目位置:

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

    Object itemId = this.getListAdapter().getItemId(position); 
    String item = itemId.toString(); 

    Toast.makeText(this, item, Toast.LENGTH_SHORT).show(); 

} 

現在我想選擇的數據讀出到舉杯。我嘗試過這種方式,但它沒有工作:

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

    Cursor cursor = db.query("tbl_homework", new String[] {"hw"}, 
      "_id = 1", null, null, null, null); 

    int column = cursor.getColumnIndex("hw"); 
    String item = cursor.getString(column); 

    Toast.makeText(this, item, Toast.LENGTH_SHORT).show(); 

} 

ERRORMESSAGE: android.database.CursorIndexOutOfBoundsException:指數-1要求,具有尺寸1個

問題已經解決了!我必須將光標移動到第一個 cursor.moveToFirst();

全部工作代碼:

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

    Object itemId = this.getListAdapter().getItemId(position); 
    String item = itemId.toString(); 

    Cursor cursor = db.query("tbl_homework", new String[] {"hw"}, 
      "_id =" + item, null, null, null, null); 

    cursor.moveToFirst(); 
    int column = cursor.getColumnIndex("hw"); 
    String hw = cursor.getString(column); 
    Toast.makeText(this, hw, Toast.LENGTH_SHORT).show(); 
} 
+0

你怎麼設置你的列表適配器?添加該代碼 – waqaslam 2012-01-30 14:09:29

+0

我沒有一個^^如何在沒有列表適配器的情況下讀出這些數據? – 2012-01-30 14:13:44

回答

1

調用getItem()返回與Adapter給定位置相關的 「項目」。在CursorAdapter的情況下,getItem()返回Cursor,設置到正確的位置。如果您希望從Cursor中獲取數據,請撥打getString(),getInt()或任何其他列獲取者on the Cursor interface

+0

感謝您的幫助!現在我有一個新問題(在我的問題上編輯) – 2012-01-30 15:49:30

+0

@MarcoSeiz:在我的回答中,我寫道:「如果您希望從遊標中獲取數據,請調用getString(),getInt()或任何其他列Cursor界面上的獲得者。「你沒有這樣做。 – CommonsWare 2012-01-30 16:38:37

+0

我試過這種方式:Cursor cursor = db.query(「tbl_homework」,new String [] {「hw」},「_id = 1」,null,null,null,null); \t \t String item = cursor.getString(1); \t Toast.makeText(this,item,Toast.LENGTH_SHORT).show(); 仍然不能正常工作... ErrorMessage:請求索引-1,大小爲1 – 2012-01-31 08:29:19

2

如果您不知道您在ListView中插入了什麼,那麼如何指定要讀取的列?

無論如何,你可以通過這樣試試你的運氣:

public void onListItemClick(ListView parent, View v,int position, long id) 
{ 
    Cursor c = (Cursor) this.getListAdapter().getItem(position); 

    // if 0 doesn't work then try 1, 2, 3 
    // and so on (depending on your columns length) 
    String keyword = c.getString(0); 

    Toast.makeText(this, keyword, Toast.LENGTH_SHORT).show(); 
} 
+0

這對我來說很好......謝謝!下一個問題:我不能做一個選擇查詢到我的數據庫... – 2012-01-30 15:29:21

+0

你需要通過光標迭代你想要的列 – waqaslam 2012-01-30 20:45:24

+0

我在那裏添加了新的代碼...這不起作用 – 2012-01-31 08:38:19

0

下面的代碼將解決這個問題:

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

    Object itemId = this.getListAdapter().getItemId(position); 
    String item = itemId.toString(); 

    Cursor cursor = db.query("tbl_homework", new String[] {"hw"}, 
      "_id =" + item, null, null, null, null); 

    cursor.moveToFirst(); 
    int column = cursor.getColumnIndex("hw"); 
    String hw = cursor.getString(column); 
    Toast.makeText(this, hw, Toast.LENGTH_SHORT).show(); 
} 

因此,有必要首先將光標移動到。

相關問題