从列表中获取listItem数据

发布于 2024-11-07 06:24:27 字数 2083 浏览 0 评论 0原文

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

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();
} }

I hate a list created by simple cursor adapter:

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) {

    }

The layout involved with the list are two simple textviews.

What i want to do is create a listener

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

The part im failing at is retrieving the BookTitle part of the specific entry(row) in order to requery the database and present the data with AlertDialog.Builder.

When i try doing :

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

i'm only getting android.database.sqlite SQLiteCursor@44f99e80 and im rather confused on how this should be done (I know why it's crashign just can;t get my mind around on how it should be done properly.

Full code 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();
} }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肥爪爪 2024-11-14 06:24:27

尝试这样的事情......

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

Try something like this...

Cursor theCursor = ((SimpleCursorAdapter)((ListView)l).getAdapter()).getCursor();
String selection = theCursor.getString(theCursor.getColumnIndex("Books.BookTitle"));
一枫情书 2024-11-14 06:24:27

只需从光标获取数据即可:

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

请参阅此处

Just get the data from the cursor:

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

See here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文