android - 在android中自定义对话框列表

标签 android android-dialog

我正在尝试使用以下代码创建列表对话框

new AlertDialog.Builder(ContactActivity.this)
    .setTitle(contactStore.getContactName())
    .setItems(contactStore.getContactNumber(), 
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        })
    .create();

但是它以下面的形式显示数据

enter image description here

但是我想创建这样的东西

enter image description here

因此,要实现此目的,我是否需要创建自定义对话框并在其中加载 ListView ?

最佳答案

不使用 setItems(),而是使用 setAdapter()。示例:

return new AlertDialog.Builder(getActivity())
     .setTitle(getArguments().getString("titulo"))
     .setCancelable(false)
     .setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.listrowlay, lista), 
                 new DialogInterface.OnClickListener(){
                     @Override
                     public void onClick(DialogInterface dialog, int item){

您可以使用您的布局代替 R.layout.listrowlay。

关于android - 在android中自定义对话框列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9329156/

相关文章:

java - onConfigurationChanged 不会避免方向更改时 Activity 重新启动

java - setOnGroupExpandListener 未解决,android

Android ListView ItemClickListener 无法与 Horizo​​ntalScrollView 一起正常工作

Android:在容器中心显示自定义对话框

android - 如何在其 onCreate 方法中取消或关闭自定义对话框?

android - 在不新建项目的情况下在Android Studio中查看logcat

android - 让两个应用程序使用相同的签名相互通信

java - GC 期间释放了什么?

android - 将 ConstraintLayout 与自定义 Android 对话框一起使用

安卓 1.6 : "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"