0

I have a ListView which is display several String and a method of setOnItemLongClick which will call and pop out a dialog options menu which has the options of Add, View and Delete. The setOnItemLongCLick will be triggered when the user LongClick one of the item from the Listview such as "Dictionary", "Book" or "Journal".

What I want to do here is, after the user has long pressed "Journal" and choose delete option, I want to Toast message "Delete Journal". If "Book" has been long pressed so it will be "Delete Book" and so on. So I need to get the String selected from setOnItemLongClick and pass the String into the Dialog Options Menu.

I'm wonder if this is possible and how? Thanks in advance for your help.

This is my code for Set Dialog Menu Options

   final String[] option = new String[] { "Add", "View", "Delete" }; 
    ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, option);
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("Select Option"); 
    builder.setAdapter(adapter2, new DialogInterface.OnClickListener() 
    { 
        public void onClick(DialogInterface dialog, int which) 
        { // TODO Auto-generated method stub 
            switch(which){
            case 0: 
                Toast.makeText(getApplicationContext(),  "Add", Toast.LENGTH_SHORT).show();
                break;
            case 1: 
                Toast.makeText(getApplicationContext(),  "View", Toast.LENGTH_SHORT).show();
                break;
            case 2: 
                String delete="";
                Toast.makeText(getApplicationContext(),  "Delete " + delete, Toast.LENGTH_SHORT).show();
                break;
            default:
                // nothing
                break;
            }
        } 

    });

    final AlertDialog dialog = builder.create();

This is my code for Set On Item Long Click

    mylist.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() 
    {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
        {
            //show dialog menu options box

            dialog.show(); 
            return true;
        }
    });
4

3 回答 3

0

onItemLongClic您可以使用public void onCreateContextMenu(final ContextMenu menu, final View v, final ContextMenuInfo menuInfo)设置编辑和删除选项或任何您需要的选项的位置,而不是使用k。

从上下文菜单中选择的项目的动作可以在

public boolean onContextItemSelected(final MenuItem item)

有关上下文菜单的更多信息,请参见此处

有关分步教程,请访问此处

这里

于 2013-10-19T15:32:42.810 回答
0

而不是调用 dialog.show();onItemLongClick您可以将所有与对话框相关的代码包装在具有选定索引参数的方法中,以获取对话框中按下的项目位置,如下所示:

private AlertDialog showOpetionMenu(int selected_index){

  // your code here...

AlertDialog dialog = builder.create();

return dialog;
}

并且onItemLongClick您可以通过传递选定的索引来获取对话框实例:

 @Override
   public boolean onItemLongClick(AdapterView<?> arg0, View arg1, 
                                        int arg2, long arg3) 
      {
         //show dialog menu options box
         AlertDialog dialog=dialogshowOpetionMenu(arg2);//<<pass selected index
         dialog.show(); 
         return true;
     }
于 2013-10-19T15:34:00.157 回答
0

在几个人的帮助下,这就是我得到的。

为 AlertDIalog 创建一个方法:

    private AlertDialog showOpetionMenu(final String selected_index){

    final String[] option = new String[] { "Add", "View", "Delete" }; 
    ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, option);
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle("Select Option"); 
    builder.setAdapter(adapter2, new DialogInterface.OnClickListener() 
    { 
        public void onClick(DialogInterface dialog, int which) 
        { // TODO Auto-generated method stub 
            switch(which){
            case 0: 
                Toast.makeText(getApplicationContext(),  "Add", Toast.LENGTH_SHORT).show();
                break;
            case 1: 
                Toast.makeText(getApplicationContext(),  "View", Toast.LENGTH_SHORT).show();
                break;
            case 2: 
                delete="";
                Toast.makeText(getApplicationContext(),  "Delete" + selected_index, Toast.LENGTH_SHORT).show();
                break;
            default:
                // nothing
                break;
            }
        } 

    }); 

    final AlertDialog dialog = builder.create(); 

    return dialog;
    }

然后只需在 onItemLongClick 中执行此操作

    mylist.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() 
    {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
        {
            //show dialog menu options box
            String selectedType=listType.get(arg2);
            AlertDialog dialog=showOpetionMenu(selectedType);//<<pass selected index
            dialog.show(); 
            return true;
        }
    });

非常感谢你帮助我!

于 2013-10-20T04:56:51.453 回答