片段中未显示列表视图
List view not shown in fragment
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>
所以我试图在片段列表视图中显示歌曲,但它对我不起作用请帮助我。我正在尝试用它制作音乐播放器应用程序。
应用程序启动时,列表视图未显示,仅显示空白屏幕。
您似乎缺少 onCreateView 方法。你需要实现类似的东西:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.playlist, container, false);
}
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>
所以我试图在片段列表视图中显示歌曲,但它对我不起作用请帮助我。我正在尝试用它制作音乐播放器应用程序。
应用程序启动时,列表视图未显示,仅显示空白屏幕。
您似乎缺少 onCreateView 方法。你需要实现类似的东西:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.playlist, container, false);
}