gpt4 book ai didi

java - Fragment 中的自定义 listView。未找到布局

转载 作者:行者123 更新时间:2023-12-01 16:33:47 25 4
gpt4 key购买 nike

我在 Fragment 中有一个 listView 并且工作正常。现在我正在尝试做一个自定义 listView 但我找不到。simple_list_item_1 工作正常,但列表项不起作用。

    @Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);

ListView mListView = (ListView)getActivity().findViewById(R.id.list);
//mArrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, android.R.id.text1, songs);
mArrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.list_item, android.R.id.text1, songs);
mListView.setAdapter(mArrayAdapter);

}

错误

error: cannot find symbol
mArrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.list_item, android.R.id.text1, songs);
^

符号:变量list_item

最佳答案

try this if there is no crash and your list view in not creating,

重写onViewCreated()并在其中声明listview并使用view而不是getActivity()像这样

ListView mListView = (ListView)view.findViewById(R.id.list);

 mArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.list_item, android.R.id.text1, songs);

将代码复制并粘贴到此处

  @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ListView mListView = (ListView)view.findViewById(R.id.list);
//mArrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, android.R.id.text1, songs);
mArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.list_item, android.R.id.text1, songs);
mListView.setAdapter(mArrayAdapter);
}

关于java - Fragment 中的自定义 listView。未找到布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62002171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com