如何使用onItemLongClick删除列表视图中的项目?

时间:2019-06-30 06:23:43

标签: android android-listview alertdialog onlongclicklistener

我已经创建了一个onItemLongClick,当长按alertdialog时将显示以下listview。我的代码的问题在于,即使我从列表中选择“编辑”作为选项,该项目仍将被删除。

当我单击“编辑”时我想表达一个意图,而当我单击“删除”时我想删除该项目,但我不知道如何创建条件语句来做到这一点。

enter image description here

这是我的代码:

MainActivity.java

<-- start of snippet -->

@Override
public boolean onItemLongClick(AdapterView<?> View view, final int position, long id){
   Persons selectedPersons = this.list.get(position);
   String name = selectedPersons.getName();

   final CharSequence[] options = {"Edit", "Delete"};

   builder.Items(options, new DialogInterference.OnClickListener(){

      @Override
      public void onClick(DialogInterface dialog, int which){
          list.remove(position);
          adapter.notifyDataSetChanged();
          Toast.makeText(MainActivity.this, "Item deleted!", Toast.LENGTH_LONG).show();

}
});
AlertDialog dialog = builder.create();
dialog.show();

return true;

}

<-- end of snippet -->

3 个答案:

答案 0 :(得分:0)

像这样在您的onClick中放置一个if条件-

@Override
 public boolean onItemLongClick(AdapterView<?> View view, f final int position, long id){
    Persons selectedPersons = this.list.get(position);
    String name = selectedPersons.getName();

    final CharSequence[] options = {"Edit", "Delete"};

   builder.Items(options, new DialogInterference.OnClickListener(){

      @Override
      public void onClick(DialogInterface dialog, int which){
          if(options[which].equals("Delete") {
              list.remove(position);
              adapter.notifyDataSetChanged();
              Toast.makeText(MainActivity.this, "Item deleted!", Toast.LENGTH_LONG).show();
           } else if(options[which].equals("Edit") {
                 //Do edit
           }

      }
});

答案 1 :(得分:0)

自从您提到wanted to put an intent when I click "Edit" and delete the item when I click Delete以来,我认为您可以通过使用whichDialogInterference.OnClickListener()的值来做到这一点:

@Override
public boolean onItemLongClick(AdapterView<?> View view, final int position, long id){
   Persons selectedPersons = this.list.get(position);
   String name = selectedPersons.getName();
   final CharSequence[] options = {"Edit", "Delete"};
   builder.Items(options, new DialogInterference.OnClickListener(){
      @Override
      public void onClick(DialogInterface dialog, int which){
          if (which == 0) { //put the edit codes like call an intent
            Intent i = new Intent(getApplicationContext(), ActivityEdit.class);  
            startActivity(i); 
          } else {
             list.remove(position);
             adapter.notifyDataSetChanged();
             Toast.makeText(MainActivity.this, "Item deleted!", Toast.LENGTH_LONG).show();
          }
      }
   });
   AlertDialog dialog = builder.create();
   dialog.show();
   return true;
}

答案 2 :(得分:0)

添加列表的alertdialog onItemLongClick()事件。 像这样的东西:

new AlertDialog.Builder(this)
    .setTitle("Are you sure?")
    .setMessage("Do you want to delete this MemberClass?")
    .setIcon(android.R.drawable.ic_dialog_alert)
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
        list.remove(position);
        Toast.makeText(DeleteMember.this, "Deleted Member", Toast.LENGTH_SHORT).show(); 
    }})
    .setNegativeButton(android.R.string.no, null).show();
}