我创建其中包含一个ListView的片段unity_view
:Android的碎片和ListView适配器问题
<ListView
android:id="@+id/listViewUnity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="90dp"
android:layout_toRightOf="@+id/firstName" >
</ListView>
下面的代码(类扩展片段)生成一个空例外:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// If activity recreated (such as from screen rotate), restore
// the previous article selection set by onSaveInstanceState().
// This is primarily necessary when in the two-pane layout.
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
}
final ListView listview = (ListView) this.getActivity().findViewById(R.id.listViewUnity);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
"OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
"Android", "iPhone", "WindowsMobile" };
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(),
android.R.layout.simple_list_item_activated_1);
listview.setAdapter(adapter);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.unity_view, container, false);
}
两个问题:
- 你知道为什么
listview.setAdapter
失败吗? - 我不应该在
android.R.layout
中包含listViewUnity作为选项吗?
是的,这是一个自定义列表。谢谢! – 2014-09-04 17:56:54