android无法在片段中使用getLayoutInflater

时间:2016-04-12 11:30:59

标签: java android android-fragments layout-inflater

我正在使用片段,我试图建立一个从数据库中获取的列表。除了下面的代码之外,一切似乎都很好

LayoutInflater lf = getActivity().getLayoutInflater();

我已经在这里设置了布局inflater

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

        LayoutInflater lf = getActivity().getLayoutInflater();
        View view = lf.inflate(R.layout.activity_charity, container, false);

        listView = (ListView) view.findViewById(R.id.charityList);

        listadapter = new charitylist(this, getLayoutInflater());

        listView.setAdapter(listadapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                //String trackName = listadapter.getItem(position).optString("track_name", "");


            }

        });

        setType = "favourite";

        getlist(setUrl, setType);

        return view;
    }

但我不知道如何在listadapter中使用layoutinflater lf。

这是完整的代码

.sublist {
  max-height: auto;
  overflow: hidden;
  -webkit-transition: 0.5s ease-in all;
  -moz-transition: 0.5s ease-in all;
  transition: 0.5s ease-in all;
}

我不知道如何让这个工作。我尝试了几件事来让它发挥作用。但我需要启动适配器。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

LayoutInflater inflater onCreateView已提供

Fragment。为什么不这样使用:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.activity_charity, container, false);
    listView = (ListView) view.findViewById(R.id.charityList);
    listadapter = new charitylist(getActivity(), inflater);
 ...