Android: 无法设置 init listView 以在自定义模式中使用

Android: Cannot set init listView for using in custom modal

在 activity 我有以下内容:

ListView lv = (ListView) findViewById(R.id.list_view_meal_country);

ListView xml 不是实际可见的部分 activity。

ListView 应该显示在自定义模式中 window:

但如果我尝试检查

if(lv == null)
                Logger.e("LV IS NULL");

我总是得到空结果。

所以我无法将自定义适配器设置为 listView。

包含listVIew的Layout未初始化时如何避免ger listView null?

模式布局window:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content">

    <EditText
        android:id="@+id/username"
        android:inputType="textEmailAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="4dp"
        android:hint="TEST1" />
    <EditText
        android:id="@+id/password"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="16dp"
        android:fontFamily="sans-serif"
        android:hint="TEST"/>

    <!-- LIST VIEW -->
    <ListView
        android:id="@+id/list_view_meal_country"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

我尝试在将内容附加到模态后附加 Lv,但结果仍然相同:

boolean wrapInScrollView = true;
            new MaterialDialog.Builder(ctx)
                    .title("TEST")
                    .customView(R.layout.custom_modal_list, wrapInScrollView)
                    .show();

            ListView lv = (ListView)  ctx.findViewById(R.id.list_view_meal_country);
            if(lv == null)
                Logger.e("LV IS NULL");

请问如何解决?

您可以使用 getCustomView() 访问您的自定义视图,然后您可以使用 findViewById 访问列表视图

MaterialDialog.Builder dialog = new MaterialDialog.Builder(ctx)
                    .title("TEST")
                    .customView(R.layout.custom_modal_list, wrapInScrollView)
                    .show();

dialog.getCustomView().findViewById(R.id.list_view_meal_country);