列表视图未显示在片段中

时间:2017-02-15 16:49:23

标签: android listview android-viewpager audio-player android-music-player

playlists.java

public class playlists extends Fragment {
private ArrayList<musics> songList;
private ListView songView;
public playlists() {
    // Required empty public constructor
}

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

    songList = new ArrayList<musics>();
    getSongList();
    Collections.sort(songList, new Comparator<musics>(){
        public int compare(musics a, musics b){
            return a.getTitle().compareTo(b.getTitle());
        }
    });
    Adapter songAdt = new Adapter(getActivity(), songList);
    songView.setAdapter(songAdt);

}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    songView = (ListView) view.findViewById(R.id.song_list);
    // Inflate the layout for this fragment

}
public void getSongList() {
    ContentResolver musicResolver =  getActivity().getContentResolver();
    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
    //retrieve song info
    if(musicCursor!=null && musicCursor.moveToFirst()){
        //get columns
        int titleColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.TITLE);
        int idColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media._ID);
        int artistColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.ARTIST);
        //add songs to list
        do {
            long thisId = musicCursor.getLong(idColumn);
            String thisTitle = musicCursor.getString(titleColumn);
            String thisArtist = musicCursor.getString(artistColumn);
            songList.add(new musics(thisId, thisTitle, thisArtist));
        }
        while (musicCursor.moveToNext());
    }
}}

playlist.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".playlists">

<ListView
    android:id="@+id/song_list"
    android:layout_width="fill_parent"
    android:layout_marginTop="102dp"
    android:layout_height="wrap_content" >
</ListView></RelativeLayout>

所以我试图在listview中显示片段中的歌曲,但它对我不起作用请帮助我。我正在尝试用它制作音乐播放器应用程序。

当应用程序启动时,listview未显示其仅显示空白屏幕。

1 个答案:

答案 0 :(得分:1)

好像你错过了onCreateView方法。你需要实现类似的东西:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.playlist, container, false);
}