Android:如何将String变量传递给Dialog Options Menu

时间:2013-10-19 15:26:40

标签: java android android-dialog

我有一个ListView,它显示了几个String和一个setOnItemLongClick的方法,它将调用并弹出一个对话框选项菜单,其中包含Add,View和Delete选项。当用户LongClick ListView中的一个项目(如“Dictionary”,“Book”或“Journal”)时,将触发setOnItemLongCLick。

我想在这里做的是,在用户长按“日记”并选择删除选项后,我想要Toast消息“删除日记”。如果长按“书”,那么它将是“删除书”,依此类推。所以我需要从setOnItemLongClick中选择String并将String传递给Dialog Options Menu。

我想知道这是否可能以及如何?在此先感谢您的帮助。

这是我设置对话框菜单选项的代码

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

这是我在长项目上设置的代码

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

3 个答案:

答案 0 :(得分:0)

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

可以在

中处理从上下文菜单中选择的项目的操作
public boolean onContextItemSelected(final MenuItem item)

有关上下文菜单的详细信息,请参阅here

有关分步教程,请访问here

Here

答案 1 :(得分: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;
     }

答案 2 :(得分:0)

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

为AlertDalog创建一个方法:

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

非常感谢你帮助我!