Custom ArrayAdapter

ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that’s placed into the list.-http://developer.android.com/

So we will start with making a simple listview and move towards using CustomAdapter which provide a custom way of representing our own XML .

Lets get started:

To show a vertical row of scrollable item in our application.

First we need is a ListView

ListView is then populated with Adapter

Simplest Adapter used is ArrayAdapter which converts a list of array (ArrayList) to views in ListView

ArrayAdapter acts as a mediator between Datasource(ArrayList,Cursor) and AdapterView(ListView,GridView,Spinner)

Using Basic ArrayAdapter

To use basic ArrayAdapter, we just need to initialize the array adapter and attach them to ListView

ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);

ArrayAdapter Uses three argument:

1st Argument:takes a context or a Activity Instance

2nd Argument:it takes the layout of a xml file

3rd Argument:its a array of data

Next we just need to connect adapter to the listview

ListView listView = (ListView) findViewById(R.id.lvItems);
listView.setAdapter(itemsAdapter);

Using Custom Adapter

When we want to use our own custom XML layout for each item in the list we use custom adapter.To do this we need to create our own custom ArrayAdapter.
 For creating custom Adapter , let us think what are we keeping in the list .so i am going to create a list of my friend with their photos and phone number.
 First ,So lets create a class named ‘Friend’ which contains name.phone number and their image location.
 Now we define the xml layout of the ListView

Now we just convert the java object to view .Here what we need to do is extends ArrayAdapter

For Enhanced performance , ViewHolder is used to make the system smooth. Since every time the item in the list is loaded it has to perform TextView task . getView Method : Called each time for data size. LayoutInflater : Inflate (Add) view/xml file for ListView Row. Holder : Contain Inflated / added xml Elaments. Now lets attach adapter to listview

// Construct the data source
ArrayList arrayOffriends = new ArrayList();
// Create the adapter to convert the array to views
CustomAdapter adapter = new CustomAdapter(this, arrayOffriends);
// Attach the adapter to a ListView
ListView listView = (ListView) findViewById(R.id.lvItems);
listView.setAdapter(adapter);

Populating Data into ListView

now since the adapter is attach we can attach item anytime on the list

// Add item to adapter

Friend rohit = new Friend(“github.com/rohitsthaa”, “rohit”,”9845422250");
Friend hilson = new Friend(“hilson”, “hilson”,”98011000");
Friend aatish = new Friend(“aatish”, “aatish”,”980000125");
adapter.add(rohit);
adapter.add(hilson);
adapter.add(aatish);

// Or even append an entire new collection

// Fetching some data, data has now returned

// If data was JSON, convert to ArrayList of User objects.

JSONArray jsonArray = …;
ArrayList<Friend> newfriend = User.fromJson(jsonArray)
adapter.addAll(newfriend);

also can clear at anytime

You can find the project at :https://github.com/rohitsthaa/CustomListView


Originally published at shrestharohit.com.np on October 26, 2015.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.