2

这是我的代码。我The constructor ArrayAdapter<String>(MainFragment, int, String[]) is undefined在 ArrayAdapter 上遇到错误。

如何填充listView1数组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>
4

1 回答 1

8

片段不是上下文对象。您需要将 Activity 对象 (Fragment.getActivity()) 作为第一个参数传递。

于 2012-07-11T18:51:05.527 回答