简体   繁体   English

单击删除按钮从数据库和列表视图中删除项目

[英]Delete item from both, database and listview on clicking Delete Button

I need to delete an item permanently from ListView and then from database. 我需要从ListView然后从数据库中永久删除一个项目。 I have a DatabaseHandler.java class, which has the delete function as: 我有一个DatabaseHandler.java类,其删除功能为:

// 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();
}

Then I have a FriendList.java class, when the user's friends are displayed as an item in ListView. 然后,当用户的朋友在ListView中显示为项目时,我有了FriendList.java类。 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. 现在,当我单击删除时,该项目将从ListView中删除,但不会从数据库中删除。 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;


      }
      }); 

The corresponding code for onActivityResult is as follows: onActivityResult的相应代码如下:

@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. 任何帮助将不胜感激。

EDIT: 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) 编辑:在取消注释db.deleteContact(posi)时,出现以下错误:DatabaseHandler类型的方法deleteContact(Contact)不适用于参数(int)

Note that the function deleteContact has contact variable of the type Contact. 请注意,函数deleteContact具有类型为Contact的contact变量。

Its a compilation error. 它是编译错误。 You need to pass a Contact object to the method, not an integer. 您需要将Contact对象传递给方法,而不是整数。

When you delete.... Try Deleting first from database then from ListView .. 当您删除....尝试从数据库中删除第一然后从ListView中 ..
example: 例:

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

DatabaseHandler class....... DatabaseHandler类.......

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

}                                                                  

I think the problem is that position is not the corresponding id in the database. 我认为问题在于位置不是数据库中的相应ID。 A possible solution would be to add a tag with the database id of the contact you have in this listitem. 一种可能的解决方案是在此列表项中添加带有您的联系人的数据库ID的标签。 And when you remove it you get the tag with the id and delete the item from the database. 而当您删除它时,您将获得带有id的标签,并从数据库中删除该项目。

Edit (added clarification): 编辑(添加说明):

I would add in your listviewadapter something like: 我会在您的listviewadapter中添加以下内容:

yourcontactview.setTag(contact.getId());

in which you add to your view a tag with the database id of the corresponding contact. 在其中将带有相应联系人数据库ID的标签添加到视图中。

Then where you delete the contact I would get the contact you want to delete with something like this: 然后在删除联系人的位置,我将得到您想要删除的联系人,如下所示:

Contact deletecontact = db.getContact(view.getTag());
db.deleteContact(deletecontact);

Of course you could change your deleteContact(Contact contact) to a method in which you give the id instead of the contactobject. 当然,您可以将deleteContact(Contact contact)更改为提供ID而不是contactobject的方法。

This should hopefully work. 希望这可以工作。

You can delete from database first then from ListView. 您可以先从数据库中删除,然后再从ListView中删除。 And suggest Iterator to remove list element. 并建议Iterator删除列表元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM