【问题标题】:Get database id from cursor to CustomAdapter (Android)从光标获取数据库 id 到 CustomAdapter (Android)
【发布时间】:2016-08-25 23:30:27
【问题描述】:

我知道这可能被称为重复问题,但我确信我的情况非常独特。基本上我需要一种方法来获取我的光标 id,即从数据库中为我的表解析到自定义适配器的 id。因此,当我单击该列表时,它会显示正确的 ID。这是我到目前为止所得到的......

请注意,我想使用 customadapter 而不是 cursoradapter。

//Get Categories from database
    final Cursor cursor = dbHandler.getCategories(0);
    if (cursor != null) {
        if(cursor.moveToFirst()){
            do{
                Categories categories = new Categories();
                categories.set_id(cursor.getInt(0));
                categories.set_categoryname(cursor.getString(2));
                categories.set_categoriescaption(cursor.getString(3));
                //list.add(cursor.getString(cursor.getColumnIndex(dbHandler.COLUMN_CATEGORY_NAME)));
                categoriesList.add(categories);

            }while (cursor.moveToNext());
        }
        cursor.close();
    }

光标从我的sqlite数据库中获取数据

listView = (ListView) view.findViewById(R.id.categories);

    adapter = new CategoryAdapter(view.getContext(), R.layout.cursor_row, categoriesList);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    String cid = String.valueOf(id);
                    Toast.makeText(view.getContext(), cid , Toast.LENGTH_SHORT).show();

我希望在单击按钮时显示表格的真实 ID,而不是职位 ID。真实ID应该从1开始,但我在这里得到0。帮助将不胜感激。谢谢

【问题讨论】:

    标签: java android sqlite android-sqlite custom-adapter


    【解决方案1】:

    在互联网上经过深思熟虑后,我找到了一个现在很有意义的解决方案。

    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    

    来自 onclick 适配器视图的 id。我发现如果它没有被覆盖,它将被设置为默认位置。所以我在我的 customadapter 中传递了我的 id 并且能够看到我的 id。

    @Override
    public long getItemId(int position) {
      return data.get(position).Id;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-31
      • 2015-04-18
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      相关资源
      最近更新 更多