2012-01-30 41 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(); 
} 

因此,有必要首先将光标移动到。

相关问题