【问题标题】:SQLite ID column and onItemLongClickSQLite ID 列和 onItemLongClick
【发布时间】:2016-09-04 07:43:28
【问题描述】:

我一直在寻找和寻找没有运气,这是我的代码

// Product List
    public void setProductList() {

        productDB = new DatabaseHelper(this);
        listView = (ListView) findViewById(R.id.list);
        listAdapter = listView.getAdapter();


        Cursor prodView = productDB.getAllData();
        prodView.moveToFirst();
        ArrayAdapter<String> listAdaptor = new ArrayAdapter<String>(this, R.layout.medicinelistmenu, R.id.productlistsTextView1);

        while (prodView.moveToNext()) {
            listAdaptor.add(prodView.getString(1));
            
        }

        listView.setAdapter(listAdaptor);


        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id) {

                productDB.deleteProduct(id);
                Toast.makeText(medicineList.this, "Item Deleted", Toast.LENGTH_LONG).show();
                finish();
                startActivity(getIntent());

                return true;
            }

        });
    }

使用 onItemLongClick 函数,其他帖子告诉我 AdapterView 中的 long 值将是 SQLLite ID 列,但它与数组中的位置值相同。

这是我的 DatabaseHelper 类中的删除函数

    public void deleteProduct(long ID)
{
    SQLiteDatabase productsdb = this.getWritableDatabase();
    productsdb.execSQL("DELETE FROM  "+ PRODTABLE_NAME +" WHERE ID = "+ ID);

}

任何人都可以阐明如何获取数据库 ID 值。

【问题讨论】:

  • 不要使用ArrayAdapter,而是使用CursorAdapter,很可能是SimpleCursorAdapter
  • 好的,谢谢,我试试看。
  • 如果你使用SimpleCursorAdapteronItemLongClick(AdapterView&lt;?&gt; arg0, View arg1, int pos, long id)的最后一个参数是一个数据库ID

标签: java android sqlite android-studio


【解决方案1】:

Long 是被点击项目的行 ID,而不是数据库中产品的 ID。

在 DBHelper 类中创建一个返回 ID 的方法,具有参数 String productName

编辑onItemLongClick()

 @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id) {
            TextView prdName = arg1.findViewById(R.id.textView_prdname);

            // now call the method which take productName as parameter and return productId
            int prdID = productDB.getProductID(prdName.getText().toString());

            // Now Pass that ID to deleteProduct
            productDB.deleteProduct(prdID);
            Toast.makeText(medicineList.this, "Item Deleted", Toast.LENGTH_LONG).show();
            finish();
            startActivity(getIntent());

            return true;
        }

另外,如果名称是唯一的而不是DELETE FROM table WHERE ID = id,您可以使用
DELETE FROM table WHERE NAME = name 这里名称可以从prdName.getText() 获取
这样您只需要执行一个查询。

【讨论】:

  • "Long is the row id of the item that was clicked, not the id of the product in the DB."不,long是Adapter#getItemId(int position)返回的值,CursorAdapter通过返回_id列实现该方法
  • 那么positionidonItemLongClick / onItemClick 方法中有什么区别?只需使用SimpleCursorAdapter,您就会看到id 是真正的数据库ID,而不是"row/list_item position"
  • 没关系,但正如您在问题中看到的,它是 arrayadapter 而不是 cursoradapter,这就是我发布此替代方案的原因。
  • 这就是为什么(以及其他几个原因)ArrayAdapter 在处理基于Cursor 的数据时不应该使用,而是需要使用CursorAdapter
  • ** 我知道。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
  • 2013-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-09
  • 1970-01-01
相关资源
最近更新 更多