Android 带有自定义 ListView 的 DialogFragment
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20356038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DialogFragment with custom ListView
提问by TrtG
I'm trying to create a DialogFragment that shows a dialog with a custom ListView inside.
我正在尝试创建一个 DialogFragment 来显示一个带有自定义 ListView 的对话框。
public class MultiSelectDialogCustom extends DialogFragment {
ListView mLocationList;
private ArrayList<String> mOfficeListItems = new ArrayList<String>();
public static MultiSelectDialogCustom newInstance(int title) {
MultiSelectDialogCustom dialog = new MultiSelectDialogCustom();
Bundle args = new Bundle();
args.putInt("title", title);
dialog.setArguments(args);
return dialog;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Collections.addAll(mOfficeListItems, getResources().getStringArray(R.array.offices));
View v = inflater.inflate(R.layout.fragment_choice_list, container,
true);
mLocationList = (ListView)v.findViewById(R.id.location_criteria_list);
final FunctionListArrayAdapter adapter = new FunctionListArrayAdapter(
this, android.R.layout.simple_list_item_1, mOfficeListItems);
mLocationList.setAdapter(adapter);
getDialog().setTitle(getArguments().getInt("title"));
return v;
}
}
When calling it from a fragment :
从片段调用它时:
MultiSelectDialogCustom dialogFrag = MultiSelectDialogCustom_.newInstance(R.string.dialog_title);
dialogFrag.show(getActivity().getSupportFragmentManager(), null);
It only shows a blank dialog with the title... why my isn't my list displayed?
它只显示一个带有标题的空白对话框......为什么我的列表没有显示?
回答by Kasra Rahjerdi
Instead of using onCreateView
you should be overriding onCreateDialog
and inside of it, it'll look something like:
而不是使用onCreateView
你应该覆盖onCreateDialog
并在它内部,它看起来像:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Collections.addAll(mOfficeListItems, getResources().getStringArray(R.array.offices));
View v = getActivity().getLayoutInflater().inflate(R.layout.fragment_choice_list, null);
mLocationList = (ListView)v.findViewById(R.id.location_criteria_list);
final FunctionListArrayAdapter adapter = new FunctionListArrayAdapter(
this, android.R.layout.simple_list_item_1, mOfficeListItems);
mLocationList.setAdapter(adapter);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(getArguments().getInt("title")).setView(v);
return builder.create();
}
This quote from the DialogFragment
documentation pagedescribes what you're trying to do:
DialogFragment
文档页面中的引用描述了您要执行的操作:
Implementations should override this class and implement
onCreateView(LayoutInflater, ViewGroup, Bundle)
to supply the content of the dialog. Alternatively, they can overrideonCreateDialog(Bundle)
to create an entirely custom dialog, such as anAlertDialog
, with its own content.
实现应覆盖此类并实现
onCreateView(LayoutInflater, ViewGroup, Bundle)
以提供对话框的内容。或者,他们可以覆盖onCreateDialog(Bundle)
以创建一个完全自定义的对话框,例如AlertDialog
具有自己内容的 。
In your case, it seems like onCreateDialog
is the way to go since you want to do a custom inner view.
在您的情况下,这似乎onCreateDialog
是要走的路,因为您想进行自定义内部视图。
回答by Kanak Sony
May be you are missing something very small but important. Are you missing notifyDataSetChanged()
in your adapter?
可能你错过了一些很小但很重要的东西。你notifyDataSetChanged()
的适配器丢失了吗?
"Once you have added the new item to the adapter you have to call notifyDataSetChanged()
so that the listview
refreshes itself with the new set of data found in the adapter."
“将新项目添加到适配器后,您必须调用,notifyDataSetChanged()
以便listview
使用适配器中找到的新数据集刷新自身。”
回答by Jim Turner
what I forgot was:
我忘记的是:
view.setAdapter(adapter);
after I added that the code worked
在我补充说代码有效之后