setAdapter(new ArrayAdapter ....不行

时间:2014-03-17 15:12:05

标签: android android-fragments

Hello i get构造函数ArrayAdapter(FragmentBlue,int,String [])未定义

在此特定代码中

package com.example.babycare;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.support.v4.app.Fragment;


public class FragmentBlue extends Fragment implements FragmentLifecycle {

    private String[] drawerListViewItems;
    private ListView drawerListView;

    private static final String TAG = FragmentBlue.class.getSimpleName();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_blue, container, false);
        return view;

        drawerListViewItems = getResources().getStringArray(R.array.items);

        View rootView = inflater.inflate(R.layout.fragment_blue, container, false);
        drawerListView = (ListView)rootView.findViewById(R.id.drawer);

        drawerListView.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_listview_item, drawerListViewItems));
    }

    @Override
    public void onPauseFragment() {
        Log.i(TAG, "onPauseFragment()"); 
    }

    @Override
    public void onResumeFragment() {
        Log.i(TAG, "onResumeFragment()");

    }
}

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

因为您正在使用片段,所以您必须获取活动上下文:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_blue, container, false);

    drawerListViewItems = getResources().getStringArray(R.array.items);

    View rootView = inflater.inflate(R.layout.fragment_blue, container, false);
    drawerListView = (ListView)rootView.findViewById(R.id.drawer);

    drawerListView.setAdapter(new ArrayAdapter<String>(getActivity(),
            R.layout.drawer_listview_item, drawerListViewItems));


    return view;
}

答案 1 :(得分:0)

我认为你需要

drawerListView.setAdapter(new ArrayAdapter<String>(getActivity,
                R.layout.drawer_listview_item, drawerListViewItems));

而不是

drawerListView.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_listview_item, drawerListViewItems));

this替换为getActivity