【问题标题】:Get ID from database onClick ListView从数据库中获取 ID onClick ListView
【发布时间】:2017-09-12 20:59:12
【问题描述】:

我在我的应用程序中使用 SQLite。我有一个 listView,其中填充了数据库中的值。

当我从列表视图中单击一个项目时,我会转到另一个页面,并且我想(有意)从数据库中传输 ID。问题是我只是从我的listView的位置转移ID,这是错误的。例如,假设我有:

ID 名称 1 大卫 2约瑟夫

我的 listView 将显示两个名称。但是,如果我删除 David,我在 Joseph 中单击时得到的 ID 是 1 而不是 2 。这就是问题所在!

所以:当我点击一个项目时,我想从我的数据库中而不是从我的 listView 中检索 ID

这是我在助手中的方法:已更新!

   public Cursor getAllCours()
{
    String Query = ("select ID as _id, nom from " + TABLE_COURS);
    Open();
    Cursor cursor = db.rawQuery(Query, null);
       return cursor;
}

以及我如何在我的活动中显示它:

  Cursor cursor = dbhelper.getAllCours();
    String[] from = { "nom" }; int[] to = { android.R.id.text1 };
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, from, to, 0);
    lv.setAdapter(adapter);

最后,我的 listviewClickListener :

  lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            //I THINK THE ERROR IS HERE !
            long itemid= lv.getItemIdAtPosition(i);
            int id= (int)itemid;
            String a =lv.getItemAtPosition(i).toString();
            Intent b = new Intent(AffichageCours.this,AffichageNotes.class);
            Bundle args = new Bundle();
            args.putString("nom_cours",a);
            args.putInt("id",id);
            b.putExtras(args);
            startActivity(b);
        }

    });
}

我的 TABLE_COURS 列:

 private static final String TABLE_COURS = "cours";
private static final String COLONNE_IDCOURS = "ID";
private static final String COLONNE_COURS = "nom";

 private static final String CREATE_COURS ="CREATE TABLE cours " +
        "("+COLONNE_IDCOURS+" INTEGER PRIMARY KEY AUTOINCREMENT , "
        +COLONNE_COURS+" TEXT NOT NULL)";

我如何使用上下文菜单删除:

 @Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    switch(item.getItemId())
    {
        case R.id.supprimer:
            dbhelper.deleteCours(lv.getItemAtPosition(info.position).toString());
            adapter.remove(adapter.getItem(info.position)); //the error is here..
            adapter.notifyDataSetChanged();
            return true;

谢谢你们!

【问题讨论】:

  • 你不需要getItemIdAtPosition:查看传递给onItemClick方法的最后一个参数,当然你需要使用支持你的数据库ID的正确适配器,比如SimpleCursorAdapter,而不是ArrayAdapter
  • 好的,但是我该如何使用 SimpleCursorAdapter 呢?
  • 就这么简单:thinkandroid.wordpress.com/2010/01/09/…,它使用this.setListAdapter(mAdapter);,你可以直接调用lv.setAdapter(...);
  • 你没有我的问题的例子吗?喜欢代码?你发给我的链接有点复杂..
  • 你有 6 行代码,其中有什么不清楚的地方? TABLE_COURS 中有哪些列?

标签: android sqlite listview android-studio android-database


【解决方案1】:

我认为问题在于您要的是 ListView 中行的 Id,而不是该行保存的实际数据。在onItemClick 方法中更改:

long itemid= lv.getItemIdAtPosition(i);

为此:

String courId = adapter.getItem(i);

这里的关键是尝试从ArrayAdapter而不是直接从ListView获取数据。

【讨论】:

  • "not directly from the ListView" 什么意思?从ListView 获取的数据是什么?
  • ListView 如何拥有你想要的 ID 概念?您在 ArrayAdapter 中放入的数据到底是什么?
  • 我的 listView 包含列 NAME。但是当我单击时,我想从选择的名称中传递列 ID。但是来自数据库的 ID
  • 如果您没有以任何方式将 ID 传递给 ListView 或适配器,除非您以某种方式按位置同步列表中显示的内容和内存中的数据,否则无法获取此类 ID数组/数据列表。我建议您遵循@M 的方法。阿什。将您的 sql 数据转储到 POJO 中,创建一个自定义适配器以显示数据并通过查询适配器获取原始数据,正如我在答案中所说。
  • "there's no way to obtain such ID",这就是为什么 OP 应该使用CursorAdapter,文档说:“光标必须包含一个名为“_id”的列,否则这个类将不起作用” i>,这个_id 列用于long CursorAdapter#getItemId(int position) 方法实现 - 查看他编辑的代码以及现在使用SimpleCursorAdapter 是多么简单
【解决方案2】:

您应该使用 Java 对象,这样对您来说很容易。

假设您已将联系人存储在数据库中(包含字段:姓名、手机和地址)。所以为此创建一个 POJO:

class Contacts{
    private String name;
    private String mobile;
    private String address;

    //write getters and setters
}

然后,每当您访问数据库时,获取您想要显示的所有联系人并将其存储在ArrayList&lt;Contacts&gt; mContactList;

然后在 ListView 的适配器中传递这个列表以在 listView 中显示数据。

CustomAdapter adapter = new CustomAdapter(context, layoutResource, mContactList);

然后您可以根据位置在单击特定列表项时检索对象;

lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
           Contacts contact = mContactList.get(i);
           // now you can do whatever you want;
        }

    });

【讨论】:

  • "You should have use Java Objects",不,OP的数据模型是Cursor,所以他应该使用某种CursorAdapter,而不是ArrayAdapter
  • 是的,他也可以,但主要是为了小事频繁访问数据库不是一个好主意。
  • 这就是sqlitedb的用途,不需要在ram中复制它的数据
  • CursorAdapter 已弃用,不建议 Google 使用。我同意@M.Ashish 建议的方法。
  • @mikehc "CursorAdapter is deprecated and not recommended for usage by Google ",从什么时候开始?你的意思是一个 CursorAdapter 被弃用的构造函数(但推荐另外两个)?
【解决方案3】:

既然你没有使用自定义适配器,为什么不在 onItemClickListener 里面试试呢

String idThatYouWant = listeCours.get(i).getId();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-31
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多