获取构造误差ArrayAdapter在Android上ListFragment [英] Getting a constructor error with ArrayAdapter in a ListFragment on Android
本文介绍了获取构造误差ArrayAdapter在Android上ListFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的code。我得到的误差构造ArrayAdapter<字符串>(MainFragment,INT,字符串[])未定义
上ArraryAdapter
我如何填充是ListView1
与数组项目
?
公共类MainFragment扩展ListFragment {
的String []项目= {12只猴子,和莎莫的500天,烈火战车};
@覆盖
公共无效的onCreate(包savedInstanceState)
{
setListAdapter(新ArrayAdapter<字符串>(这一点,android.R.layout.simple_list_item_1,项目));
}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
// TODO自动生成方法存根
返回super.onCreateView(充气,容器,savedInstanceState);
}
}
和这里是我的XML:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<的ListView
机器人:ID =@ + ID / ListView1的
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT>
< / ListView控件>
< / LinearLayout中>
解决方案
片段不是上下文对象。你需要传递活动对象(Fragment.getActivity())作为第一个参数来代替。
Here's my code. I'm getting the error The constructor ArrayAdapter<String>(MainFragment, int, String[]) is undefined
on ArraryAdapter.
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);
}
}
and here's my 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>
解决方案
Fragments are not context objects. You need to pass the Activity object (Fragment.getActivity()) as first argument instead.
这篇关于获取构造误差ArrayAdapter在Android上ListFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文