3

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:

  1. What is the usefulness of the pojo class, if i can achieve the whole operation with 2d arraylist.
  2. 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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Tobi Oyelekan
  • 362
  • 5
  • 19

1 Answers1

1

What is the usefulness of the pojo class

  • The term "POJO" initially denoted a Java object which does not follow any of the major Java object models, conventions, or frameworks; nowadays "POJO" may be used as an acronym for "Plain Old JavaScript Object" as well, in which case the term denotes a JavaScript object of similar pedigree

  • A POJO is usually simple so won't depend on other libraries, interfaces or annotations. This increases the chance that this can be reused in multiple project types

Read more about Plain old Java object (POJO) and from here Advantage of POJO

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

Serializable

  • Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

Read serialization - advantages and disadvantages

Goku
  • 9,102
  • 8
  • 50
  • 81