2013-09-01 139 views
0

所以我有两个片段在一个活动使用PageAdapterListView没有出现在片段

其中一个片段是一个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> 

回答

0

你不应该检查空的列表视图。而是检查列表是否为空。

将其更改为listView.getChildCount()== 0。

但我认为你不需要检查,因为它会一直是空的,因为它是一个新创建的视图。列表中没有任何项目。如果您想正确检查,请在将适配器设置为列表后进行条件检查。

检查是否有效。

+0

与该检查,代码does not得到运行,我该如何解决它? – cxzp

+0

Sry。我的错误,将其更改为listView.getChildCount()== 0。这意味着如果您的列表当前为空,请将这些项目添加到列表中。 – Joe138

+0

不,因为当我将'if'语句设置为'listView.getChildCount()!= 0'时,代码永远不会运行,如果它保持为'listView!= null',它将会到达'setAdapter(test)'行,但是仍然没有任何反应 – cxzp