How to use POJO to fill ArrayList in Android

Hello friends…!!! Today’s blog post is coming up with some programming concepts which I have applied to develop a simple Android application. One concept is “POJO (Plain Old Java Object)” and the other concept is called “programming to the interface”. I am not going to explain those concepts in detail, but I will show you how to apply them in a basic level for a simple Android application.

First I will explain what I am going to implement today. Actually the final output is almost similar to my previous blog post. Difference is how we are going to create the data set and what is the Adapter that we are going to use. POJO relates with how we are going to implement the data set and “programming to the interface” relates with the Adapter that we are using to join the UI (User Interface) component and the data set. Following image show you the final out come of today’s implementation.

Use POJO to fill ArrayList in Android
Figure 1

Now I will show you the POJO class that I have created. Simply it is a class with some “private” attributes and “public” getters and setters to access those attributes. In this example Film.java class act as the POJO.

package com.anuja.android;

public class Film {

	private String filmName;
	private String mainStar;

	public String getFilmName() {
		return filmName;
	}
	public void setFilmName(String filmName) {
		this.filmName = filmName;
	}

	public String getMainStar() {
		return mainStar;
	}
	public void setMainStar(String mainStar) {
		this.mainStar = mainStar;
	}
}

The next concept that I want to emphasize is “programming to the interface”. If you can remember my previous blog post, I have used ArrayAdapter class as to make a link between the UI and the data set. In this post also I am using ArrayAdapter class. But my reference type is a ListAdapter which is an Interface. You can see the difference in the code.

package com.anuja.android;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

public class FilmListActivity extends Activity {

	// Declare UI components
	private ListView filmListView;
	private ListView starListView;

	private ArrayList filmArray; // Type safe with only Film objects
	private ArrayList filmNamesArray;
	private ArrayList starNamesArray;
	private ListAdapter filmListAdapter;
	private ListAdapter starListAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.film_list); // Load the film_list.xml file

        // Initialize UI components
        filmListView = (ListView) findViewById(R.id.filmListView);
        starListView = (ListView) findViewById(R.id.starListView);

        // Initialize an array to put film objects with film's all attributes which we have defined in Film.java
        filmArray = new ArrayList();

        // Initialize an array to put film names which we are going to show in the ListView
        filmNamesArray = new ArrayList();

        // Initialize an array to put film names which we are going to show in the second ListView
        starNamesArray = new ArrayList();

        // Plug the data set (filmNamesArray) to the adapter
        filmListAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, filmNamesArray);

        // Plug the adapter with the UI component (filmListView)
        filmListView.setAdapter(filmListAdapter);

        // Setting Film attributes
        for(int i=0 ; i<4 ; i++ ){
            // Initialize a Film object in order to set it's attributes.
        	// We have to create a Film object each time this loop traverse
            Film film = new Film();

            // Here we set the film name and star name attribute for a film in one loop
        	film.setFilmName("Film name " + i);
        	film.setMainStar("Star name " + i);

        	// Pass the Film object to the array of Film objects
        	filmArray.add(film);

        	// Add the film name to array of film names
        	filmNamesArray.add(film.getFilmName());

        	// Add the star name to array of film names
        	starNamesArray.add(film.getMainStar());
        }

        // Plug the data set (starNamesArray) to the adapter
        starListAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, starNamesArray);

        // Plug the adapter with the UI component (starListView)
        starListView.setAdapter(starListAdapter);
    }
}

I have explain the code as comments. You can either download the source from my GitHub profile or copy above code and try it out with an IDE.

Thank You for reading this ๐Ÿ˜‰

Advertisements

About AnujAroshA

Working as a Senior Software Engineer. Specialized in iOS application development. A simple person :)
This entry was posted in Android Examples. Bookmark the permalink.

9 Responses to How to use POJO to fill ArrayList in Android

  1. Chris Nevill says:

    Excellent post. It would be awesome if you could do an extended version of this example where you add a search.

  2. Ganesh says:

    Great tutorials….need tutorials on all basic concepts in android

  3. Divesh says:

    Nice tutorial
    Why you have used filmArray ArrayList.Without using filmArray the program is running well.

  4. Michael says:

    Hi, ๐Ÿ™‚
    Won’t you mind if you post a code here, where is your Button is disabled and about setting a database on edittext, its like when you type on edittext, and then type the correct answer from database you can unlock the disabled button and it is being saved on database. so in everytime you open the app, that button is enabled on the next level

  5. Ankit Baghel says:

    easy to understand good code…but i have a problem here:
    filmListAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, filmNamesArray);

    // Plug the adapter with the UI component (filmListView)
    filmListView.setAdapter(filmListAdapter);

    // Setting Film attributes
    for(int i=0 ; i<4 ; i++ ){
    .
    .
    .
    .
    starNamesArray.add(film.getMainStar());
    }

    // Plug the data set (starNamesArray) to the adapter
    starListAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, starNamesArray);

    // Plug the adapter with the UI component (starListView)
    starListView.setAdapter(starListAdapter);
    }
    -x-x-x-x-x
    why did you plug your filmlistadapter before even filling your arraylist??
    arraylist has no data before it encounters the loop right??

  6. Polash says:

    nice ๐Ÿ™‚

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s