在 Android 上的 ListFragment 中使用 ArrayAdapter 出现构造函数错误 [英] Getting a constructor error with ArrayAdapter in a ListFragment on Android
本文介绍了在 Android 上的 ListFragment 中使用 ArrayAdapter 出现构造函数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码.我收到错误 The constructor ArrayAdapter
on ArraryAdapter.
Here's my code. I'm getting the error The constructor ArrayAdapter<String>(MainFragment, int, String[]) is undefined
on ArraryAdapter.
如何用数组 items
填充 listView1
?
How do I populate the listView1
with the array items
?
public class MainFragment extends ListFragment {
String[] items = { "12 Monkeys", "(500) Days of Summer", "Chariots of Fire" };
@Override
public void onCreate(Bundle savedInstanceState)
{
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return super.onCreateView(inflater, container, savedInstanceState);
}
}
这是我的 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/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
推荐答案
片段不是上下文对象.您需要将 Activity 对象 (Fragment.getActivity()) 作为第一个参数传递.
Fragments are not context objects. You need to pass the Activity object (Fragment.getActivity()) as first argument instead.
这篇关于在 Android 上的 ListFragment 中使用 ArrayAdapter 出现构造函数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文