0

我目前正在尝试将数据从 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();
    }


}
4

2 回答 2

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


     }


 }
于 2012-05-27T04:04:56.910 回答
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();
    }


}
于 2012-05-27T04:07:21.367 回答