【问题标题】:Getting listItem data from a list从列表中获取 listItem 数据
【发布时间】:2011-05-16 18:40:56
【问题描述】:

我讨厌由简单的光标适配器创建的列表:

Cursor c = myDbHelper.rawQ(select);
    startManagingCursor(c);

    // the desired columns to be bound
    String[] columns = new String[] { "Books.BookTitle",
            "Publishers.Publisher" };
    // the XML defined views which the data will be bound to
    int[] to = new int[] { R.id.ISBN_entry, R.id.Title_entry };

    // Getting results into our listview
    try {
        SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
                R.layout.listlayout, c, columns, to);
        this.setListAdapter(mAdapter);
    } catch (Exception e) {

    }

列表涉及的布局是两个简单的文本视图。

我想做的是创建一个监听器

 @Override
protected void onListItemClick(ListView l, View v, int position, long id) { 

我失败的部分是检索特定条目(行)的 BookTitle 部分,以便重新查询数据库并使用 AlertDialog.Builder 呈现数据。

当我尝试做的时候:

 String selection = l.getItemAtPosition(position).toString(); 

我只得到了 android.database.sqlite SQLiteCursor@44f99e80 并且我很困惑应该如何做到这一点(我知道它为什么会崩溃;我不知道应该如何正确地做到这一点。

完整代码atm:

...
Cursor c = myDbHelper.rawQ(select);
    startManagingCursor(c);

    // the desired columns to be bound
    String[] columns = new String[] { "Books.BookTitle",
            "Publishers.Publisher" };
    // the XML defined views which the data will be bound to
    int[] to = new int[] { R.id.ISBN_entry, R.id.Title_entry };

    // Getting results into our listview
    try {
        SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
                R.layout.listlayout, c, columns, to);
        this.setListAdapter(mAdapter);
    } catch (Exception e) {

    }



}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
 // TODO Auto-generated method stub
 //super.onListItemClick(l, v, position, id);
 String selection = l.getItemAtPosition(position).toString();  
 new AlertDialog.Builder(v.getContext())
 .setTitle("Title")
 .setMessage(selection)
 .setPositiveButton(android.R.string.ok, null)
 .show();
} }

【问题讨论】:

    标签: java android listview


    【解决方案1】:

    试试这样的...

    Cursor theCursor = ((SimpleCursorAdapter)((ListView)l).getAdapter()).getCursor();
    String selection = theCursor.getString(theCursor.getColumnIndex("Books.BookTitle"));
    

    【讨论】:

    • @GGe:很高兴我能提供帮助。
    【解决方案2】:

    只需从光标处获取数据:

     l.getItemAtPosition(position).getString(0); // it might be 1
    

    here

    【讨论】:

      猜你喜欢
      • 2011-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多