I'm trying to show all songs that are on the SD card. The code works fine if there is at least one song, but I get a NullPointerException if there are no songs, so I wanted to replace the listview with a new layout in case there are no songs like "sorry, nothing to show here"
I tried several ways but I always get a NullPointerException on the method getCount() of my adapter
So...here is my code:
ListFragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mInflater = inflater;
rootView = inflater.inflate(R.layout.fragment_list, container, false);
mContext = container.getContext();
mContainer = container;
mAdapterListFile = new SongsAdapter(mContext, mSongList);
mListSongs = (ListView) rootView.findViewById(R.id.listView_songs);
mListSongs.setAdapter(mAdapterListFile);
mSongList = new ArrayList<Song>();
mSongList = listAllSongs(); //there will be added all songs found in SD card
return rootView;
}
private ArrayList<Song> listAllSongs() {
Cursor cursor;
ArrayList<Song> songList = new ArrayList<Song>();
Uri allSongsUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String orderBy = MediaStore.Audio.Media.TITLE + " ASC";
if (isSdPresent()) {
cursor = getActivity().getContentResolver().query(allSongsUri, STAR, selection, null, orderBy);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
Song song = new Song();
String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
String[] res = data.split("\\.");
song.setSongName(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE))); //song name song.setSongFullPath(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA))); //directory
song.setSongId(cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media._ID))); //id song
song.setSongAlbumName(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM))); //album
song.setSongArtistName(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST))); //artist song.setAlbumImage(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART)));
song.setSongUri(ContentUris.withAppendedId(
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media._ID))));
String duration = getDuration(Integer.parseInt(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION)))); //duration
song.setSongDuration(duration);
songList.add(song);
} while (cursor.moveToNext());
return songList;
}
cursor.close();
}
} return null;
}
private static boolean isSdPresent() {
return android.os.Environment.getExternalStorageState()
.equals(android.os.Environment.MEDIA_MOUNTED);
}
SongsAdapter.java
public class SongsAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Song> songList;//Data Source for ListView
public SongsAdapter(Context context, ArrayList<Song> list) {
this.mContext = context;
this.songList = list;
}
@Override
public int getCount() {
return songList.size();
}
@Override
public Song getItem(int position) {
return songList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
//Layout inflate for list item
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, null);
}
ImageView mImgSong = (ImageView) convertView.findViewById(R.id.copertina_album);
TextView mTxtSongName = (TextView) convertView.findViewById(R.id.titolo_brano);
TextView mTxtArtista = (TextView) convertView.findViewById(R.id.artista);
TextView mTxtSongDuration = (TextView) convertView.findViewById(R.id.durata);
mImgSong.setImageResource(R.drawable.vector_vinyl);
mTxtSongName.setText(songList.get(position).getSongName());
mTxtArtista.setText(songList.get(position).getArtistName());
mTxtSongDuration.setText(songList.get(position).getSongDuration());
return convertView;
}
public void setSongsList(ArrayList<Song> list) {
songList = list;
this.notifyDataSetChanged();
}
}
Any suggestions?