简体   繁体   English

在Android上的ListFragment中使用ArrayAdapter获取构造函数错误

[英]Getting a constructor error with ArrayAdapter in a ListFragment on Android

Here's my code. 这是我的代码。 I'm getting the error The constructor ArrayAdapter<String>(MainFragment, int, String[]) is undefined on ArraryAdapter. 我收到错误The constructor ArrayAdapter<String>(MainFragment, int, String[]) is undefined

How do I populate the listView1 with the array items ? 如何使用数组items填充listView1

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:

<?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. 您需要将Activity对象(Fragment.getActivity())作为第一个参数传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM