我正在制作一个应用程序,它将从SD卡中获取歌曲 填写listview。但由于某种原因,它不会填充和显示 空白片段。你能帮忙解决造成这种情况的原因吗? 问题。谢谢。
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;
}
}
答案 0 :(得分:1)
我认为您的身份证明不正确
Bellasongviews = (ListView)rootview.findViewById(android.R.id.list);
请确保此android.R.id.list
ID正确无误,应从R.id.yourId
此处不要导入android.R
此处导入您的项目R
。
检查您的XML布局文件的ID,或者在您的问题中将其粘贴到此处。
答案 1 :(得分:1)
查看你的代码,我看到方法getBellaSingList()。这个方法正在检索数据,甚至反复调用自己,但它不会在onCreateView()
内调用。从技术上讲 - 您没有填充数据集。您需要在初始化视图时调用它 - 并且请验证此方法的逻辑。