繁体   English   中英

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

[英]Correct way to pass data to showOptionsDialog

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

尽管目前我正在做的事情感觉不错,但我希望这里的人能够为我提供正确的解决方案,并解释为什么它是正确的解决方案。

我的代码如下,此刻在长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();
    }


}
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():


     }


 }

我想你可以做到这一点。 希望它可以帮助你

公共类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();
    }


}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM