代码之家  ›  专栏  ›  技术社区  ›  no_profile

如何使用OnitemLongClick删除ListView中的项目?

  •  0
  • no_profile  · 技术社区  · 5 年前

    我已经创建了一个 onitemLongClick which will display the following alertDialog when listView is long clicked.我的代码的问题是,即使我从列表中选择了“编辑”作为选项,该项仍将被删除。

    我想在单击“编辑”时表达一个意图,在单击“删除”时删除该项,但我不知道如何创建条件语句来执行该操作。

    这是我的代码:

    MaultActudio.Java

    <--start of snippet-->
    
    @覆盖
    公共布尔型OnitemLongClick(adapterView<?>视图,最终内部位置,长ID){
    persons selectedPersons=this.list.get(职位);
    string name=selectedPersons.getname();
    
    final charsequence[]选项=“编辑”,“删除”
    
    builder.items(选项,新对话框interference.onclickListener()。{
    
    @覆盖
    public void onclick(dialoginterface dialog,int which){
    列出。删除(位置);
    adapter.notifyDataSetChanged();
    toast.maketext(mainactivity.this,“项目已删除!”,toast.length_long.show();
    
    }
    (});
    alertDialog dialog=builder.create();
    dialog.show();
    
    返回真值;
    
    }
    
    <--代码段结尾-->
    
    
    

    我想在单击“编辑”时表达一个意图,在单击“删除”时删除该项,但我对如何创建条件语句没有任何线索。

    enter image description here

    这是我的代码:

    mainactivity.java(主活动.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 -->
    
    2 回复  |  直到 5 年前
        1
  •  0
  •   shb    5 年前

    把一个if条件放在你的onclick中,就像这样-

    @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
               }
    
          }
    });
    
        2
  •  0
  •   SaadAAkash    5 年前

    自从你提到 wanted to put an intent when I click "Edit" and delete the item when I click Delete ,我认为你可以通过使用 which 从您的 DialogInterference.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;
    }
    
    
        3
  •  0
  •   Rajan Girsa    5 年前

    添加列表的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();
    }