单击“删除”按钮,从数据库和列表视图中删除项目 [英] Delete item from both, database and listview on clicking Delete Button

查看:208
本文介绍了单击“删除”按钮,从数据库和列表视图中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从ListView中永久删除一个项目,然后从数据库中删除。我有一个DatabaseHandler.java类,它具有以下删除功能:

I need to delete an item permanently from ListView and then from database. I have a DatabaseHandler.java class, which has the delete function as:

// Deleting single contact, in DatabaseHandler.java class
public void deleteContact(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
            new String[] { String.valueOf(contact.getID()) });
    db.close();
}

然后我有一个FriendList.java类,当用户的朋友显示为ListView中的项目。当我长按一个项目时,我在对话框中选择删除和取消。现在,当我点击删除时,该项目将从ListView中删除,但不会从数据库中删除。如何从数据库中删除它?
获取删除和取消选项的代码

Then I have a FriendList.java class, when the user's friends are displayed as an item in ListView. When I long press on an item, then I get the option of "Delete" and "Cancel" in Dialog Box. Now, when I click on delete, the item is deleted from the ListView, but, not from the database. How can I delete it from database as well? The code for getting the option of "Delete" and "Cancel"

listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {


      @Override
      public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
              int position, long id) {
          // TODO Auto-generated method stub
                  Intent i = new Intent(FriendList.this, Delete_Confirm.class).addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
  //I am sending position of listitem in putExtra below//     
          i.putExtra("position", position);
          startActivityForResult(i,CONFIRM);
          item2 = (String) arg0.getItemAtPosition(position);


          //Toast.makeText(FriendList.this, "Clicked"+item2, Toast.LENGTH_SHORT).show();
          int l = item2.length();
          c=0;
          for(int j=0; j<=l; j++){
              if(item2.charAt(j) != '9' || item2.charAt(j+1) != '1'){
                  c++;                   
              }
              else {
                  //Do nothing
                  break;
              }
              num = item2.substring(c, l);  

          }


          Toast.makeText(FriendList.this, "Clicked: "+num, Toast.LENGTH_SHORT).show();
          return true;


      }
      }); 

onActivityResult的相应代码如下:

The corresponding code for onActivityResult is as follows:

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
  super.onActivityResult(reqCode, resultCode, data);

  switch (reqCode) {
  case (CONFIRM) :
  if(resultCode==RESULT_OK){
      int posi = data.getIntExtra("position",0);
          Log.d("msg","position is " + posi);

          Log.d("msg","Do we reach here?");
          final StableArrayAdapter adapter = new StableArrayAdapter(this,
                    android.R.layout.simple_list_item_1, list);
                    //db.deleteContact(posi);
                    list.remove(posi);

                  listview.setAdapter(adapter);
          adapter.notifyDataSetChanged();


  }

    break;

}}

请建议如何从数据库中删除它。任何帮助将受到高度赞赏。

Please suggest how can I delete it from database as well. Any help would be highly appreciated.

编辑:
在取消注释db.deleteContact(posi)时,我收到以下错误:
DatabaseHandler类型中的deleteContact(Contact)方法不适用于参数(int)

On uncommenting db.deleteContact(posi), I get the following error: The method deleteContact(Contact) in the type DatabaseHandler is not applicable for the arguments (int)

请注意,函数deleteContact具有Contact类型的联系变量。

Note that the function deleteContact has contact variable of the type Contact.

推荐答案

删除时....尝试从数据库删除第一,然后从 ListView 删除..

例如:

When you delete.... Try Deleting first from database then from ListView..
example:

 db.deleteContact(list.get(posi));  // this will get string  
 list.remove(posi);  

DatabaseHandler类.......

public void deleteContact(String name){
    Log.d("Name:",""+ name);
    db.delete(TABLE_CONTACTS, KEY_NAME + " = ?", new String[] { name });

}                                                                  

这篇关于单击“删除”按钮,从数据库和列表视图中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆