我正在製作一個應用程序,它將從SD卡中獲取歌曲,並在列表視圖中填充 。但由於某些原因,它不會填充並顯示空白片段。你可以請幫忙,可能是造成這個問題的原因是 。謝謝。Listview不填充從SD卡片段歌曲
bellasongs.java
public class BellaSongs extends ListFragment {
private ArrayList<ModelBellaSongs> BellaSongList;
private ListView Bellasongviews;
Context context;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.bella_songs, container, false);
Bellasongviews = (ListView)rootview.findViewById(android.R.id.list);
BellaSongList = new ArrayList<ModelBellaSongs>();
return rootview;
}
public void getBellaSongList() {
ContentResolver musicResolver = context.getContentResolver();
Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
if (musicCursor != null && musicCursor.moveToFirst()) {
int titlecolumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int idcolumn = musicCursor.getColumnIndex(MediaStore.Audio.Media._ID);
int artistcolumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
do {
long thisId = musicCursor.getLong(idcolumn);
String thistitle = musicCursor.getString(titlecolumn);
String thisartist = musicCursor.getString(artistcolumn);
BellaSongList.add(new ModelBellaSongs(thisId, thistitle, thisartist));
} while (musicCursor.moveToNext());
}
getBellaSongList();
Collections.sort(BellaSongList, new Comparator<ModelBellaSongs>() {
@Override
public int compare(ModelBellaSongs a, ModelBellaSongs b) {
return a.getTitle().compareTo(b.getTitle());
}
});
BellaSongsAdapter bellasongsadapter = new BellaSongsAdapter(getActivity(),BellaSongList);
Bellasongviews.setAdapter(bellasongsadapter);
}
}
adapter.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/list"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
</LinearLayout>
bellasongsadapter.java
public class BellaSongsAdapter extends BaseAdapter {
private ArrayList<ModelBellaSongs> bellasongs;
private LayoutInflater songdetails;
public BellaSongsAdapter(Context c, ArrayList<ModelBellaSongs>thesongs){
bellasongs = thesongs;
songdetails = LayoutInflater.from(c);
}
@Override
public int getCount() {
return bellasongs.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout songLayout=(LinearLayout)songdetails.inflate(R.layout.singlelist_item,parent,false);
TextView titleview=(TextView)songLayout.findViewById(R.id.text_song_name);
TextView artistview=(TextView)songLayout.findViewById(R.id.text_artist_name);
ModelBellaSongs currentsongs = bellasongs.get(position);
titleview.setText(currentsongs.getTitle());
artistview.setText(currentsongs.getArtist());
songLayout.setTag(position);
return songLayout;
}
}
返回的遊標不是空的?請附上BellaSongsAdapter.class –
@YuriiTsap這裏我添加了適配器類。這是對的嗎。 – Sam