将数据传递给showOptionDialog的正确方法

时间:2012-05-27 02:57:40

标签: java android android-alertdialog

我目前正在尝试从onItemLongClick方法将数据传递到AlertDialog中,并且我正在尝试找到关于我如何执行此操作的最佳/正确练习。

虽然我现在正在做的事情有效,但感觉不对,我希望有人能够为我提供正确的解决方案并解释为什么它是正确的解决方案。

我的代码如下,目前在long onItemLongClick中,我将属性设置为列表视图中已被单击的行项,然后从AlertDialog.Builder中访问该属性。

public class ListViewExample {

    private long clickedRowId;
    ....

    mListView.setOnItemLongClickListener(new ListView.OnItemLongClickListener () {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long rowId) {
            clickedRowId = rowId;
            /* here is my issue, surely it is better to pass rowId into showOptionsDialog as an argument? */
            showOptionsDialog();
            return true;
        }
    });


    private void showOptionsDialog() {

        new AlertDialog.Builder(this.context)
            .setTitle(R.string.stack_dialog_title)
            .setItems(R.array.stack_options, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int selected) {

                    switch (selected) {
                    case 0:
                        //perform selection #1
                        break;
                    case 1:
                        //perform selection #2
                        break;
                    case 2:
                        deleteRowItem(clickedRowId);
                        break;
                    }

                }

        }).show();
    }


}

2 个答案:

答案 0 :(得分:0)

public class ListViewExample {

....

mListView.setOnItemLongClickListener(new ListView.OnItemLongClickListener () {
    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long rowId) {
        /* here is my issue, surely it is better to pass rowId into showOptionsDialog as an argument? */
        showOptionsDialog(rowId);
        return true;
    }
});


private void showOptionsDialog(long rowId) {
   AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);
   builder.setTitle(R.string.stack_dialog_title);
   builder.setItems(R.array.stack_options, new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int selected) {
        //Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
        switch (selected) {
            case 0:
                //perform selection #1
                break;
            case 1:
                //perform selection #2
                break;
            case 2:
                deleteRowItem(rowId);
                break;
            }
         }
     });
      AlertDialog alert = builder.create();
       alert.show():


     }


 }

答案 1 :(得分:0)

我认为你可以这样做。希望它可以帮助你

公共类ListViewExample {

    private long clickedRowId;
    ....

    mListView.setOnItemLongClickListener(new ListView.OnItemLongClickListener () {
        @Override
        public boolean onItemLongClick(AdapterView<YourObj> list, View arg1, int position, long rowId) {
            clickedRowId = rowId;
            YourObj clickObj list.getItemAtPosition(position)
            /* here is my issue, surely it is better to pass rowId into showOptionsDialog as an argument? */

            showOptionsDialog(clickObj );
            return true;
        }
    });


    private void showOptionsDialog(YourObj clickedObj) {

        new AlertDialog.Builder(this.context)
            .setTitle(R.string.stack_dialog_title)
            .setItems(R.array.stack_options, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int selected) {

                    switch (selected) {
                    case 0:
                        //perform selection #1
                         //do some thing about clickedObj
                        break;
                    case 1:
                        //perform selection #2
                        //do some thing about clickedObj
                        break;
                    case 2:
                        deleteRowItem(clickedRowId);
                        break;
                    }

                }

        }).show();
    }


}