0
所以我有两个片段在一个活动使用PageAdapter
。ListView没有出现在片段
其中一个片段是一个ListView,另一个是图像的GridView。
片段被成功创建,但ListView控件是空
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View view = inflater.inflate(R.layout.eventlist ,container, false);
String[] x = new String[]{"AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC"};
ListView listView = (ListView) view.findViewById(R.id.listView);
ArrayAdapter<String> test = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_1,x);
listView.setAdapter(test);
return view;
}
XML布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
与该检查,代码does not得到运行,我该如何解决它? – cxzp
Sry。我的错误,将其更改为listView.getChildCount()== 0。这意味着如果您的列表当前为空,请将这些项目添加到列表中。 – Joe138
不,因为当我将'if'语句设置为'listView.getChildCount()!= 0'时,代码永远不会运行,如果它保持为'listView!= null',它将会到达'setAdapter(test)'行,但是仍然没有任何反应 – cxzp