android - 从游标适配器删除和更新值

标签 android sqlite cursor android-cursoradapter

我是 Android 开发新手,我已使用光标适配器将值填充到 ListView 中。我正在尝试使用 ListView 删除和更新值,但我不确定如何使用游标适配器来完成此操作。我也无法单击 ListView 项目

我在数据库处理程序类中使用以下方法来删除和更新值

删除方法

 public void DeletingCustodian(Custodians custodians)
    {
        SQLiteDatabase db_database = getWritableDatabase();
        //Deleting the custodian from the Database where the custodian ID matches to the selcted ID
        db_database.delete(TABLE_CUSTODIAN,CUSTODIAN_ID + "=?" , new String[]{String.valueOf(custodians.getCust_id())});
        db_database.close();
    }

更新方法

public  int updateCustodian(Custodians cust)
    {
        SQLiteDatabase db_database = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(CUSTODIAN_NAME,cust.getCust_Name());
        values.put(CUSTODIAN_DESIGNATION,cust.getCust_Design());
        values.put(CUSTODIAN_DEPARTMENT,cust.getDepartment());

        int roweffected = db_database.update(TABLE_CUSTODIAN,values,CUSTODIAN_ID + "=?", new String[]{String.valueOf(cust.getCust_id())});
        db_database.close();
        return roweffected;
    }

我创建了一个上下文菜单,其中显示编辑和删除,当选择某个项目时会显示此菜单。

 public void onCreateContxtManu(ContextMenu menu,View view, ContextMenu.ContextMenuInfo menuInfo)
    {
        super.onCreateContextMenu(menu,view,menuInfo);

        menu.setHeaderTitle("Custodian Options");
        menu.add(Menu.NONE,EDIT,menu.NONE,"Edit Custodian");
        menu.add(Menu.NONE,DELETE,menu.NONE,"Delete Custodian");
    }

 public void deletingitemsfromlist()
    {
        CustodianListview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                return false;
            }
        });

    }
   public boolean onContextItemSelected(MenuItem item)
    {
        switch(item.getItemId())
        {
            case EDIT:


                break;
            case DELETE:


                break;
        }

        return false;
    }

最佳答案

试试这个,可能对你有帮助

删除数据库连接器中的代码

// Delete a row in Local database
    public void delete(int ids) {

        database.delete(TABLE_NAME, KEY_ROWID + " = " + ids, null);
    }

在您的Listview页面代码Onitem中长按删除

@Override
               public boolean onItemLongClick(AdapterView arg0, View v,
                 int position, long arg3)
             {
                 try
                 {

                TextView id = (TextView) v.findViewById(R.id.textView4);

                 ids1 = Integer.parseInt(id.getText().toString());
                AlertDialog.Builder ad = new AlertDialog.Builder(Page1_Landing.this);
                //ad.setTitle("Notice");
                ad.setMessage("Sure you want to delete this Item ?");

                //Delete Positive Button
                ad.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() 
                {


                @SuppressWarnings("deprecation")
                @Override
                 public void onClick(DialogInterface dialog, int which)
                 {
                     try
                     {



                          //Delete of record from Database and List view.
                          helper.delete(ids1);
                          cur.requery();
                          myCursorAdap.notifyDataSetChanged();
                          List.setAdapter(myCursorAdap);
                          Toast.makeText(Page1_Landing.this,"Selected Product is Successfully Deleted...!", Toast.LENGTH_SHORT).show();



                     }
                     catch(Exception e)
                     {
                         e.printStackTrace();
                           Log.i("Exception ", "in Delete a Product");
                     }
                 }
                });

                //long press delete cancel
                ad.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() 
                {

                 @Override
                 public void onClick(DialogInterface dialog, int which) 
                 {


                     dialog.dismiss();


                 }
                });
                ad.show();



                 }
                 catch(Exception e)
                 {
                     e.printStackTrace();
                 }
                 return true;
               }

更新代码

DatabaseConnector helper = new DatabaseConnector(this);

helper.open();

helper.updatedetails(rowID,name1,desc, dept);
Toast.makeText(getApplicationContext(),"Updated Successfully...!", Toast.LENGTH_SHORT).show();

更新数据库代码

// updating the data ....
    public boolean updatedetails(long rowId, String name,
            String desc, String dept)
    {
        ContentValues args = new ContentValues();

        args.put(KEY_ROWID, rowId);
        args.put(KEY_NAME, product);
        args.put(KEY_DESC, cat);
        args.put(KEY_DEPT, serial);



        return database.update(TABLE_NAME, args, KEY_ROWID + "=" + rowId, null) > 0;
    }

关于android - 从游标适配器删除和更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30724048/

相关文章:

android - 防止 Android 关闭在后台播放音频的 Progressive Web App

Android Google Maps GeoJson,如何为整个 map 着色一种颜色

JAVA:sqlite写几条记录后抛出异常?

java - list 更改后 Android 上出现 NoClassDefFOoundError

android - React Native 突然构建失败

database - SQLite3 : Delete record without overwriting it with nullbytes

java - 扫描二维码链接到 Android 应用程序中的特定页面

android - 在服务中使用游标或通过绑定(bind)到服务发送数据

Java jtattoo与自定义光标冲突

java - OnListItemClick 从未被调用?