简体   繁体   English

如何检索此片段中的歌曲列表?

[英]How do I retrieve the song list in this fragment?

There are two errors neither of which I can figure out why. 有两个错误,我都无法弄清楚为什么。

  1. Cannot resolve method " getContentResolver() " 无法解析方法“ getContentResolver()

2.Cannot infer arguments (on the new ArrayAdapter<>() ). 2.无法推断参数(在new ArrayAdapter<>() )。

I hope this is the right way to get the songs list from a fragment ? 我希望这是从fragment获取歌曲列表的正确方法吗? If there is some better alternative, please explain that. 如果有更好的选择,请解释一下。 Thank you 谢谢

public class SongsFragment extends Fragment {

    public SongsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_songs, container, false);
        ListView audioView = (ListView)view.findViewById(R.id.songView);
        ArrayList<String> audioList = new ArrayList<>();
        String[] proj = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME};
        final Cursor audioCursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);

        if (audioCursor != null) {
            if (audioCursor.moveToFirst()) {
                do {
                    int audioIndex = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);

                    audioList.add(audioCursor.getString(audioIndex));
                } while (audioCursor.moveToNext());
            }
        }
        audioCursor.close();

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, audioList);
        audioView.setAdapter(adapter);

        return view;
    }

}

In class which is extending Fragment Context is not available to get it need to use getActivity() method. 在正在扩展的类中,无法使用Fragment Context来获取它,需要使用getActivity()方法。

1. Cannot resolve method "getContentResolver()" 1. 无法解析方法“ getContentResolver()”

Because getContentResolver method is not available in Fragment class, need to user Context to access it: 由于Fragment类中没有getContentResolver方法,因此需要用户Context来访问它:

 final Cursor audioCursor = getActivity().getContentResolver().query(
                              MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
                              proj, null, null, null);

2. Cannot infer arguments (on the new ArrayAdapter<>()). 2. 无法推断参数(在新的ArrayAdapter <>()上)。

Because ArrayAdapter constructor require valid Context as first parameter : 因为ArrayAdapter构造函数需要有效的Context作为第一个参数:

ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), 
                                         android.R.layout.simple_list_item_1, 
                                         android.R.id.text1, audioList);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将自定义对象传递到其他片段中的列表? - How do I pass a Custom Object to a list in a different fragment? 如何更新片段中的 recyclerview 列表? (notifyDataSetChanged 不起作用) - How do I update the recyclerview list in the fragment? (notifyDataSetChanged not working) 如何在片段中创建自定义列表? - How to do a Custom List in Fragment? 如何在所有活动中播放同一首歌 - How do i keep same song playing in all activities 如何从 MongoDB 中检索特定的元素列表? - How do I retrieve particular List of element from MongoDB? 片段中的RecyclerView –如何从阵列列表中检索数据 - RecyclerView in Fragment – How to retrieve data from an Array List 我如何将共享首选项中的数据检索到片段中 - How can i retrieve data from sharedpreferences into fragment 如何在片段中单击按钮时检索 EditTexts 和 Spinners? - How can I retrieve EditTexts and Spinners on a button click in a fragment? 如何在列表中进行多次联接和检索 - how to do jooq multiple join and retrieve in a list 如何通过列表片段的选项菜单调用DialogFragment的ListFragment和DialogFragment之间进行通信? - How do I communicate between ListFragment and DialogFragment where the DialogFragment is invoked via the options menu of list fragment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM