程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> android-自定義單選列表對話框單選後item值傳遞問題

android-自定義單選列表對話框單選後item值傳遞問題

編輯:編程解疑
自定義單選列表對話框單選後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,為什麼呢?

最佳回答:


最後我自己解決了。。還是謝謝大家了

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved