0

我在创建 Listview 适配器时遇到问题,这是代码。

import com.actionbarsherlock.app.SherlockFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Recipes extends SherlockFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        ListView recipelist = (ListView) getView().findViewById(R.id.recipe_drawer);

        String[] items ={ getResources().getString(R.string.block),
        getResources().getString(R.string.tool),
        getResources().getString(R.string.support),
        getResources().getString(R.string.veggie)
        };

        ArrayAdapter<String> adapt = new ArrayAdapter<String>(Recipes.this, R.layout.recipes_list_item, items);
        recipelist.setAdapter(adapt);

        View rootView = inflater.inflate(R.layout.recipes, container, false);
        return rootView;


    }

}

我得到的错误是ArrayAdapter<String> adapt = new ArrayAdapter<String>(Recipes.this, R.layout.recipes_list_item, items);,它说

构造函数 ArrayAdapter(Recipes,int,String[]); 未定义。

我尝试将Recipes.this 更改为this,但仍然出现相同的错误。任何帮助,将不胜感激!

4

2 回答 2

2

更改Recipes.thisgetActivity()。你在 aFragment中,它不是 a ,它是构造函数族的Context第一个参数。ArrayAdapter

于 2013-08-13T17:56:35.897 回答
2

替换这个

  ArrayAdapter<String> adapt = new ArrayAdapter<String>(Recipes.this, R.layout.recipes_list_item, items);

经过

  ArrayAdapter<String> adapt = new ArrayAdapter<String>(getActivity(), R.layout.recipes_list_item, items);

用于getActivity获取托管活动的活动上下文

public final Activity getActivity ()

Added in API level 11
Return the Activity this fragment is currently associated with.
于 2013-08-13T17:57:23.587 回答