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

このようにできると思います。それがあなたを助けることができることを願っています

public class 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 に答える