我正在尝试在Fragment中显示listView,但是当我运行应用程序时,它会给出一个空白页面。我正在使用数组适配器从片段中的字符串填充ListView。
这是我的代码:
public class CardContentFragment extends Fragment {
private String[] items;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater
.inflate(R.layout.item_card, container, false);
items = getActivity().getResources().getStringArray(R.array.list);
ListView lv = (ListView) view.findViewById(R.id.list);
ListAdapter myListAdapter = (new ArrayAdapter<String>(getActivity().getApplicationContext(),
android.R.layout.simple_list_item_1, list));
lv.setAdapter(myListAdapter);
return view;
}
}
array.xml
<string-array name="list">
<item>Argentina (54 11) 4863 3291</item>
<item>Aruba (297) 839 662</item>
<item>Australia (61 2) 9416 2373</item>
</string-array>
layout.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
</ListView>
</RelativeLayout>
没有错误,我认为这只是一个布局问题,但我无法解决。
答案 0 :(得分:4)
您需要将items
传递给适配器
ListAdapter myListAdapter = (new ArrayAdapter<String>(getActivity().getApplicationContext(),
android.R.layout.simple_list_item_1, items));
^^^^^ this has your array
答案 1 :(得分:1)
发现问题,这只是一个错字:
achar()
我只是把“项目”而不是“列表”
答案 2 :(得分:0)
试试这个
items = getActivity().getResources().getStringArray(R.array.list);
ListView lv = (ListView) view.findViewById(R.id.list);
ListAdapter myListAdapter = (new ArrayAdapter<String>(getActivity().getApplicationContext(),
android.R.layout.simple_list_item_1, items));
lv.setAdapter(myListAdapter);
并将你的文件放在你的string.xml中
<string-array name="list">
<item>Argentina (54 11) 4863 3291</item>
<item>Aruba (297) 839 662</item>
<item>Australia (61 2) 9416 2373</item>
</string-array>