please i am new to android, and i need to fully understand the use of POJO
class for populating recyclerview
in android. The way i do it is fetch data from local/API put it in a 2D ArrayList
and pass it to the adapter class of recyclerview, for example the code below fetches music from device and add it to a musics 2D ArrayList
:
musics.clear();
musicResolver = getActivity().getContentResolver();
Uri musicuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicuri, null, null, null, "LOWER(" + MediaStore.Audio.Media.TITLE + ")ASC");
if (musicCursor != null) {
while (musicCursor.moveToNext()) {
ArrayList<String> tempmusic = new ArrayList<>();
tempmusic.add(0, musicCursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
long time = Integer.parseInt(musicCursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION)));
tempmusic.add(1, (new SimpleDateFormat("mm:ss", Locale.getDefault())).format(new Date(time)));
tempmusic.add(2, musicCursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));
tempmusic.add(3, musicCursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
musics.add(tempmusic); //2d array
}
musicCursor.close();
}
rvAdapter.notifyDataSetChanged();
I have no problem with the above approach, it works fine, but i came across a tutorial where a POJO
class was used which implements Serializable
, see below:
public class Audio implements Serializable {
private String data;
private String title;
private String size;
private String duration;
public Audio(String data, String title, String duration, String size) {
this.data = data;
this.title = title;
this.duration = duration;
this.size = size;
}
public String getData() {
return data;
}
public String getTitle() {
return title;
}
public String getDuration() {
return duration;
}
public String getSize() {
return size;
}
}
Method for retrieving using the POJO class:
//musics.clear();
musicResolver = getActivity().getContentResolver();
Uri musicuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicuri, null, null, null, "LOWER(" + MediaStore.Audio.Media.TITLE + ")ASC");
if (musicCursor != null) {
while (musicCursor.moveToNext()) {
ArrayList<String> tempmusic = new ArrayList<>();
String data =
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
String title =
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String album =
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
String artist =
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));
tempmusic.add(new Audio(data, title, album, artist));
}
musicCursor.close();
}
rvAdapter.notifyDataSetChanged();
Now my question is:
- What is the usefulness of the pojo class, if i can achieve the whole operation with 2d arraylist.
- If pojo class implements serializable, what does it mean and the advantage(s) it has over using an ordinary pojo class or just using a 2d arraylist
A clear explanation will be very much appreciated, thanks.