[英]ListFragment shows “Empty List” when it most definitely is not
I'm trying to set an adapter to a ListFragment, but no matter what I do it always appears as an "Empty List". 我正在尝试将适配器设置为ListFragment,但是无论我做什么,它始终显示为“空列表”。 I'm instantiating studentList just before setting the adapter. 我在设置适配器之前实例化了StudentList。 There must be something very simple that I'm missing. 我必须缺少一些非常简单的东西。 How can I write this so that my code displays the given studentList? 如何编写此代码,以便我的代码显示给定的studentList?
Main Screen.xml 主Screen.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@android:id/empty"
android:layout_gravity="center"
android:text="@string/empty_list"
style="@style/font_large"/>
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MainScreenFrag.java MainScreenFrag.java
public class MainScreenFrag extends ListFragment {
public final static String TITLE = "Student List";
public final static String TAG = "MainScreenFrag";
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
String[] studentList = {"me", "you", "everybody", "nobody","some people", "most people", "just me", "ok you too"};
setListAdapter(new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
studentList));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_screen, container, false);
}
} }
You're not calling 你不打电话
super.onViewCreated(view, savedInstanceState);
in your onViewCreated
在你的onViewCreated
Give that a try 试试看
Its mostly because you have not given an id for your list Adapter. 这主要是因为您没有为列表适配器指定ID。
ListView lv = (ListView) findViewById(R.id.list);
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, studentList);
lv.setAdapter(arrayAdapter);
This should work, though the method seems redundant. 尽管该方法似乎是多余的,但这应该可行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.