Getting a constructor error with ArrayAdapter in a ListFragment on Android

无人久伴 提交于 2019-12-29 08:14:06

问题


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>

回答1:


Fragments are not context objects. You need to pass the Activity object (Fragment.getActivity()) as first argument instead.



来源:https://stackoverflow.com/questions/11439392/getting-a-constructor-error-with-arrayadapter-in-a-listfragment-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!