我的 android listview 有一个非常奇怪的问题。列表视图位于片段内,一切都在编译,我不再收到空指针错误,但列表视图显示为空。即使它看起来是空的,日志也说明列表视图有 385 个对象。我不明白为什么它是空的。我确实得到了一个蓝色片段,并且填充了列表视图。有任何想法吗?
我如何设置适配器:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs_layout);
initialiseTabHost(savedInstanceState);
if (savedInstanceState != null) {
mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
}
ActivePackages = getList();
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout mContainer = (LinearLayout) inflater.inflate(R.layout.tab_frag1_layout, null);
ListView activeList = (ListView) mContainer.findViewById(R.id.activelist);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, ActivePackages);
Log.i("valueof activeList",String.valueOf(activeList.getCount())); //returns 0
activeList.setAdapter(adapter);
adapter.notifyDataSetChanged();
Log.i("valueof activeList",String.valueOf(activeList.getCount())); //returns 385.
}
这是片段的 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/activelist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0073fd">
</ListView>
</LinearLayout>