自定义单选列表对话框单选后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,为什么呢?
答
最后我自己解决了。。还是谢谢大家了
答
选择的时候没有保存选择值?
答
没环境,没法帮你运行,你检查下你放到dialog的数据是不是都是对的(大概是List);
如果数据都是不一样的,那在选中的时候(一般选中之后窗口会关闭,数据会返回给你)你可以保存相关的值.
参考: