我正在尝试将适配器设置为 ListFragment,但无论我做什么,它总是显示为“空列表”。我在设置适配器之前实例化 studentList 。一定有一些非常简单的东西我错过了。我怎样才能写这个,以便我的代码显示给定的学生列表?
主屏幕.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
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);
}
}