Android 具有自定义布局的ListFragment:始终显示空消息
我已经使用默认的Android 具有自定义布局的ListFragment:始终显示空消息,android,android-arrayadapter,android-listfragment,Android,Android Arrayadapter,Android Listfragment,我已经使用默认的ArrayAdapter为ListFragment创建了一个自定义布局。我的问题是,当列表不为空时,空消息显示在每一行上(当列表为空时,消息仅按预期显示一次) headline_list.xml(即我的自定义布局文件) HeadlineFragment.java public class HeadlineFragment extends ListFragment { @Override public View onCreateView(LayoutInfla
ArrayAdapter
为ListFragment
创建了一个自定义布局。我的问题是,当列表不为空时,空消息显示在每一行上(当列表为空时,消息仅按预期显示一次)
headline_list.xml(即我的自定义布局文件)
HeadlineFragment.java
public class HeadlineFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.headline_list, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<String> headlines = ...; // populate headlines list
setListAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.headline_list, R.id.my_text, headlines));
}
}
public类HeadlineFragment扩展ListFragment{
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
返回充气机。充气(R.layout.headline\u列表,容器,错误);
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
列表标题=…;//填充标题列表
setListAdapter(新的ArrayAdapter(getActivity()),
R.layout.headline_list,R.id.my_text,headline);
}
}
您的布局有点混乱
new ArrayAdapter<String>(getActivity(),
R.layout.headline_list, R.id.my_text, headlines)
在本例中,android.R.layout.simple\u list\u item\u 1
本身就是一个TextView
,因此我们不需要为一个项目提供ID
此外,您应该考虑保持对适配器的引用,如果您需要稍后修改它,
您的意思似乎是将
my_text
TextView
作为您的自定义行布局。如果是这样,请将其从片段
的布局、标题列表
中删除,并将其放入自己的布局文件中;e、 例如,list_row.xml
。您的布局文件将如下所示:
headline\u list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="There are no data" />
</LinearLayout>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
然后,您将实例化适配器,如下所示:
new ArrayAdapter<String>(getActivity(), R.layout.list_row, headlines)
newarrayadapter(getActivity(),R.layout.list_行,标题)
同样,由于行布局只是一个
TextView
,因此我们不需要在构造函数中传递一个ID。谢谢您的回答。内置的布局很好,但我想问一下自定义布局。看看你的答案,你写的第二个PAR是个人行布局的ID。这在实践中意味着什么?请参阅我发布的xml文件。注意API引用称第二PAR是一个布局文件的资源ID,它包含在实例化视图时使用的布局。您所发布的布局是片段的整个布局——包含ListVIEW和两个TeTeVIEW的线性布局。您提供给适配器构造函数的布局ID用于ListView中显示的各个行;“实例化视图时使用的布局”,即行。由于您已经为适配器提供了片段的布局,因此图像中的每一行实际上都是一个线性布局,带有一个空(不可见)列表视图和两个文本视图,其中一个是空
视图,这就是为什么它显示在每一行中。
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
new ArrayAdapter<String>(getActivity(), R.layout.list_row, headlines)