Java ListFragment显示“空列表”,但它肯定不是空列表
我试图将适配器设置为ListFragment,但无论我做什么,它始终显示为空列表。我正在设置适配器之前实例化studentList。我一定错过了一些很简单的东西。如何编写此代码以使代码显示给定的studentList Main Screen.xmlJava ListFragment显示“空列表”,但它肯定不是空列表,java,android,android-listfragment,Java,Android,Android Listfragment,我试图将适配器设置为ListFragment,但无论我做什么,它始终显示为空列表。我正在设置适配器之前实例化studentList。我一定错过了一些很简单的东西。如何编写此代码以使代码显示给定的studentList Main Screen.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
<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);
}
}这主要是因为您没有为列表适配器提供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);
虽然该方法似乎是多余的,但它应该可以工作。您没有调用它
super.onViewCreated(view, savedInstanceState);
在您的onViewCreated中
假设tryListFragment自动查找id为/list的ListView;你不需要显式地设置它。尝试使用id没有坏处。它对我有效很多次。Eclipse给出了如此荒谬的回答。我编译的东西没有错误或警告。不幸的是,它没有显示我的列表!不过,它会显示文本空列表,它会工作-我说它是多余的。ListFragment会为您执行完全相同的查找,当您调用setAdapter时,它会将其委托给该ListView。