简一_hz 2016-12-22 05:10 采纳率: 48.6%
浏览 1860
已采纳

自定义单选列表对话框单选后item值传递问题

我自定义了一个单选列表的对话框,想要选择一行数据后点击确定按钮再对这行数据进行相应操作,
但是我发现我现在的实现是不论点击哪行传进去的itemPosition都是最初默认的0;
相应部分代码见下:

//自定义Dialog
public class CustomDialog extends Dialog {

    public CustomDialog(Context context) {
        super(context);
    }

    public CustomDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private Context context;
        private String title;
        private String positiveButtonText;
        private String negativeButtonText;
        private View contentView;
        private ArrayList<String> mListItem;
        private BaseAdapter mAdapter;
        private int mClickedDialogEntryIndex;
        private DialogInterface.OnClickListener positiveButtonClickListener;
        private DialogInterface.OnClickListener negativeButtonClickListener;

        public Builder(Context context) {
            this.context = context;
        }


        /**
         * Set the Dialog title from String
         * 
         * @param title
         * @return
         */

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }


        public Builder setPositiveButton(String positiveButtonText,
                DialogInterface.OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText,
                DialogInterface.OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText,
                DialogInterface.OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public ArrayList<String> getItems() {
            return mListItem;
        }

        public Builder setItems(ArrayList<String> mListItem) {
            this.mListItem = mListItem;
            return this;
        }


        public Builder setSingleChoiceItems(ArrayList<String> mPairedDevicesList, int checkedItem, final OnClickListener listener) {
            this.mListItem = mPairedDevicesList;
            this.positiveButtonClickListener = listener;
            this.mClickedDialogEntryIndex = checkedItem;
            return this;
        } 

        public Builder setSingleChoiceItems(BaseAdapter adapter, int checkedItem, final OnClickListener listener) {
            this.mAdapter = adapter;
            this.positiveButtonClickListener = listener;
            this.mClickedDialogEntryIndex = checkedItem;
            return this;
        } 

        public CustomDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final CustomDialog dialog = new CustomDialog(context,R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialog_normal_layout, null);
            dialog.addContentView(layout, new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            /*if(mListItem == null){
                throw new RuntimeException("Entries should not be empty");
            }*/
            if(mAdapter == null){
                throw new RuntimeException("Entries should not be empty");
            }
            ListView lvListItem = (ListView) layout.findViewById(R.id.lvListItem);
            lvListItem.setAdapter(mAdapter);
            //lvListItem.setAdapter(new ArrayAdapter(context, R.layout.simple_list_item_single_choice, R.id.text1, mListItem));
            lvListItem.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            lvListItem.setItemChecked(mClickedDialogEntryIndex, true);
            lvListItem.setSelection(mClickedDialogEntryIndex);
            // set the cancel button
            if (negativeButtonText != null) {
                ((Button) layout.findViewById(R.id.cancelBtn))
                        .setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.cancelBtn))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    negativeButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }else{
                    ((Button) layout.findViewById(R.id.cancelBtn)).setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            dialog.dismiss();
                        }
                    });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.cancelBtn).setVisibility(
                        View.GONE);
            }

            if(positiveButtonText != null){
                ((Button) layout.findViewById(R.id.connectBtn))
                .setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.connectBtn))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(dialog,
                                            mClickedDialogEntryIndex);
                                }
                            });
                }else{
                    ((Button) layout.findViewById(R.id.connectBtn)).setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            dialog.dismiss();
                        }
                    });
        }
            }else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.connectBtn).setVisibility(
                        View.GONE);
            }


            if (contentView != null) {
                // if no message set
                // add the contentView to the dialog body
                ((LinearLayout) layout.findViewById(R.id.content))
                        .removeAllViews();
                ((LinearLayout) layout.findViewById(R.id.content)).addView(
                        contentView, new LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.FILL_PARENT));
            }
            dialog.setContentView(layout);
            return dialog;
        }

    }


}

 //调用自定义dialog
 final CustomDialog myDialog = new CustomDialog.Builder(getActivity())
        .setTitle(getString(R.string.btdialog_title))
        .setPositiveButton(getString(R.string.connect),  btnListener)
        .setNegativeButton(getString(R.string.cancel), null)
        .setSingleChoiceItems(btAdapter, -1, choiceListener)
        .create();
        myDialog.show();

其中choiceListener的实现

 private class ChoiceOnClickListener implements DialogInterface.OnClickListener{
        private int which = 0;

        public void onClick(DialogInterface dialogInteface, int which){
            this.which = which;
        }
        public int getWhich(){
            return which;
        }
    }

    final ChoiceOnClickListener choiceListener = new ChoiceOnClickListener();

这里就是不管选择列表中的哪一行,那个我想要的which值一直都是初始的0,为什么呢?

  • 写回答

3条回答 默认 最新

  • 简一_hz 2016-12-22 10:03
    关注

    最后我自己解决了。。还是谢谢大家了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝