您好我正在使用带有listview的Alert对话框它工作正常但是当我点击列表项时我想要关闭对话框,但它不起作用,以下是我的片段,任何人都可以帮我解决这个问题。
public void setdailog()
{
final AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
// dialog.setContentView(R.layout.alert_list_radio);
dialog.setTitle("Select Language");
View customView = LayoutInflater.from(getActivity()).inflate(
R.layout.alert_list, null, false);
ListView listView = (ListView) customView.findViewById(R.id.listiview_lang);
AlertListAdapter mAdapter = new AlertListAdapter(langList, getActivity());
listView.setAdapter(mAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SELECTED_LANGUAGE = langList.get(position).getLangid();
System.out.println("JO JO"+SELECTED_LANGUAGE);
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
dialog.dismiss();
}
});
}
});
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
dialog.setView(customView);
dialog.show();
}
答案 0 :(得分:1)
尝试类似
的内容public void setdailog()
{
final AlterDialog alert;
final AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
// dialog.setContentView(R.layout.alert_list_radio);
dialog.setTitle("Select Language");
View customView = LayoutInflater.from(getActivity()).inflate(
R.layout.alert_list, null, false);
ListView listView = (ListView) customView.findViewById(R.id.listiview_lang);
AlertListAdapter mAdapter = new AlertListAdapter(langList, getActivity());
listView.setAdapter(mAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SELECTED_LANGUAGE = langList.get(position).getLangid();
System.out.println("JO JO"+SELECTED_LANGUAGE);
// dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
// @Override
// public void onDismiss(DialogInterface dialog) {
// dialog.dismiss();
// }
if (alert != null && alert.isShowing()) {
alert.dismiss();
}
});
}
});
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
dialog.setView(customView);
alert = dialog.create();
alert.show();
}
或强>
您可以创建如下所示的自定义对话框:
public void setdailog()
{
Dialog cpDialog = new Dialog(getActivity());
cpDialog.setTitle("Select Language");
cpDialog.setContentView(R.layout.alert_list);
ListView listView = (ListView) cpDialog.findViewById(R.id.listiview_lang);
AlertListAdapter mAdapter = new AlertListAdapter(langList, getActivity());
listView.setAdapter(mAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
cpDialog.dismiss();
}
});
cpDialog.show();
}
希望这有助于你...如果你需要任何帮助,你可以问
答案 1 :(得分:0)
public void setdailog()
{
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
// dialog.setContentView(R.layout.alert_list_radio);
dialog.setTitle("Select Language");
dialog.setView(customView);
View customView = LayoutInflater.from(getActivity()).inflate(
R.layout.alert_list, null, false);
ListView listView = (ListView) customView.findViewById(R.id.listiview_lang);
AlertListAdapter mAdapter = new AlertListAdapter(langList, getActivity());
listView.setAdapter(mAdapter);
final AlertDialog dialogOne = dialog.create();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SELECTED_LANGUAGE = langList.get(position).getLangid();
System.out.println("JO JO"+SELECTED_LANGUAGE);
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
dialogOne.dismiss();
}
});
}
});
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
dialog.show();
}