所以我不确定是否可以在使用构建器的方法在构建器SetEmptyView
中设置适配器之后进行设置。AlertDialog
setAdapter
但是,我通过在同一个 xml 文件中定义一个ListView
和我的空视图(例如TextView
,可见性设置为消失)来解决这个问题,例如,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<ListView
android:id="@+id/my_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="multipleChoice"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"/>
<TextView
android:id="@+id/my_empty_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Searching for devices..."
android:layout_gravity="center"
android:textSize="16sp"
android:paddingTop="50dp"
android:paddingBottom="50dp"
android:visibility="gone"/>
</LinearLayout>
OnCreateDialog
我在扩展类的方法中扩展了这个视图组DialogFragment
(如创建自定义布局下的文档中所述),即
View myDialogView = inflater.inflate(R.layout.my_dialog_view, null);
从膨胀视图中检索到我的ListView
和空视图的引用,即
ListView myListView = (ListView) myDialogView.findViewById(R.id.my_list_view);
TextView myEmptyView = (TextView) myDialogView.findViewById(R.id.my_empty_view);
使用这些设置列表视图setemptyView
,即
myListView.setEmptyView(myEmptyView);
设置我的适配器
myListView.setAdapter(yourAdapter);
然后在构建器中简单地使用了该setView()
方法
.setView(myDialogView)
这可能是最好的方法。我被android文档抛出,它建议使用构建器setAdapter
中的方法AlertDialog
。就个人而言,我认为坚持这种setView
方法提供了更大的灵活性。