Hi everyone, we are going to talk about how can we create our custom Listview in android with Java. We will build our own ListIems in XML and integrate it to ListView.

Firstly I want to talk about what ListView is?

Android ListView is a view that groups various items and displays them in a vertically scrollable list. List items are automatically added to the list using an Adapter that pulls content from a source, such as an array or database.

An adapter bridges the interface components and the data source that fills the data into the interface Components. The adapter store and sends the data to the ListView component.

Now lets start to build our own Custom Listview Item.

I generally design the XML UI of these type of views. So now I will design the UI of ListView Item firstly.

Here is the my design :

In this ListItem I have a NetworkImageView provided by Volley Library and TextViews. I will show the content Image with NetworkImageView. Also I will show the name and content counter with the text views. I will send the datas of these items from the Java My custom Java class.

Also I need to insert a ListView to the activity to show these items.

After created XML designs for user interfaces now we are ready to ceate our java class. First I will share the code and then I will explain what the code does.

Now lets start to explain what we have done.

First we created some ArrayLists to use in our listview.

In onCreate section we filled them from the data received from last activity with putStringArrayListExtra and getStringArrayListExtra. Below code block shows how to get ArrayLists from the last activity. I do not need to initialize before this. We are already initializing while declaring these array lists. Now our datas of listview is ready to use.

Since our datas are ready now we have to call our Custom DataAdapter and load datas into it. After loaded datas we will bound this adapter and ListView in activity. Below code block is doing what I explained :

I have sended just one array. Make sure the all arrays have same quantity items. If it does not program will have NullPointer Exception. Also make sure an item has the datas in same indexes in arrays. Let me explain what I tried to tell.

If you are using seperate ArrayLists like this, all contents informations need to be same locations in all arrays in this method.

With another method: we can send the datas in One ArrayList as serialized.

Both of methods has one important state. We must be careful of data Serialization.

Now you know how to make your own listview custom items. You can create very colorful items and can show your featured datas in your listview directly.

It was the end of the article.

Keep following my articles.

Have a nice coding.

Burak Hamdi TUFAN…


Please enter your comment!
Please enter your name here