Android 从CURSOR适配器中删除和更新值

Android 从CURSOR适配器中删除和更新值,android,sqlite,cursor,android-cursoradapter,Android,Sqlite,Cursor,Android Cursoradapter,我是android开发新手,我使用游标适配器将值填充到listview中。我试图使用列表视图删除和更新这些值,但我不确定如何使用游标适配器来完成。我也无法单击列表视图项 下面是我在数据库处理程序类中用于删除和更新值的方法 删除方法 public void DeletingCustodian(Custodians custodians) { SQLiteDatabase db_database = getWritableDatabase(); //Dele

我是android开发新手,我使用游标适配器将值填充到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;
    }
public void onCreateContxtManu(ContextMenu菜单、视图、ContextMenu.ContextMenuInfo菜单信息)
{
super.onCreateContextMenu(菜单、视图、菜单信息);
菜单。设置标题(“托管选项”);
menu.add(menu.NONE,EDIT,menu.NONE,“EDIT”);
menu.add(menu.NONE,DELETE,menu.NONE,“DELETE”);
}
public void deletingitemsfromlist()
{
保管人ListView.setOnItemLongClickListener(新的AdapterView.OnItemLongClickListener(){
@凌驾
公共布尔值长单击(AdapterView父项、视图、整型位置、长id){
返回false;
}
});
}
公共布尔值onContextItemSelected(MenuItem项)
{
开关(item.getItemId())
{
案例编辑:
打破
案例删除:
打破
}
返回false;
}

更新数据库中的数据,通过再次查询数据库获取新光标,然后调用

oldCursor = myCursorAdapter.swapCursor(newCursor); // hands you back oldCursor
或:


myCursorAdapter.notifyDataSetChanged()
通知
ListView
数据集已更改,它应该刷新自身

尝试此操作,它可能会对您有所帮助

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

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

        database.delete(TABLE_NAME, KEY_ROWID + " = " + ids, null);
    }
在Listview页面中,单击删除

@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;
    }

很抱歉,我不知道怎么做,但现在我正在尝试从列表视图中获取删除项。只需修改您的数据库,然后查询数据库中的新数据并调用上述方法。我确实尝试了您的步骤,但我面临一个小问题。问题是我已经在listview页面中声明了我的游标适配器类来填充列表视图。Thankx Mano您的步骤成功了。但是我得到了一个空点异常,因为我使用了与我在问题中提到的相同的更新方法,但我使用了void而不是Integer。您能告诉我如何解决更新问题吗问题
// 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;
    }