Reputation: 9
This code is getting the albums of all the songs, but it is if there are 5 songs in a particular album, the album name is appearing 5 times, simultaneously. How to avoid this?
public class AlbumListFragment extends Fragment {
private ListView lv;
public AlbumListFragment() {}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.list_view_album, container, false);
final ArrayList<String> arr=new ArrayList<>();
lv=(ListView)v.findViewById(R.id.lvalbum);
String[] proj={MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ALBUM};
final Cursor c=getActivity().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
if(c!=null) {
if(c.moveToFirst()) {
do{
int index=c.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM);
arr.add(c.getString(index));
}while(c.moveToNext());
}
} c.close();
ArrayAdapter<String> adapter=new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, arr);
lv.setAdapter(adapter);
return v;
}
}
Upvotes: 0
Views: 200
Reputation: 804
You should use the MediaStore.Audio.Albums class like so:
String[] proj={MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM};
final Cursor c=getActivity().getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, proj, null, null, null);
You just have to replace MediaStore.Audio.Media with MediaStore.Audio.Albums and your code should work.
Don't forget to accept it as the answer to your question to get more reputation.
Upvotes: 1