Android Custom ListView Example

0
827

Introduction

In this article, we learn Android Custom ListView Example. If you have not yet checked it out, I would highly recommend you to read a complete practical work Android ListView and Customized ListView.

Android Customized ListView Example

You will learn in this section

  1. Simple Listview
  2. Customized listview

Prerequisite

It will good to cover my previous articles before this. Its help to learn more about Android development. In which I explained. How can create checkbox and spinner in android?

What is a list view

Whenever you have to show a great deal of information and make it simple to explore. Android  ListView, which is helpful makes scrollable records. The list view can store a large number of the items. The items are displaying as the user scrolling it down.

When a user clicks on a particular item it displays or performing the action on it. The display complex and a large amount of data it manages in two main classes.

  1. Adapter (A class that manages the data is called an adapter).
  2. AdapterView (A class that manages the appearance is called Adapter View).

In listView, there are three core elements. The number is one data source means which type of data that are shown on the listview. The number two is list Adapter that creates a view of each row and number three is listview on which data appear.

Working of Adapter

Android ListView and Customized Example

The adapter creates the view of each row and displays data from the data source in list view. A simple view controls and display data repeatedly. The adapter access the data from data source data display in the list view. There are different types of adapter first is list view which shows the data vertically and horizontally with the user can interact. The second is the grid view the user also interact it. The grid view more beautiful than the list view.

They are different types of data source which are <Simple array Adapter> <Cursor array Adapter> and<Based Adapter>. The Based adapter is also called a custom adapter which is used in a customized list view. The cursor adapter is used when taking data from the database.

Create a new Layout
->Right click> the ->layout folder-> select ->New option->and ->click ><layout resource folder-> and<after the layout is created>

<Paste the following code>

File:activity_main.xml

Create a new Layout 
<Paste the following code>

File:listviewitems_xml

Open <res> click on <values>  than open <string.xml>

<Paste the following code>

File:string.xml

The Layout inflator

The layout inflator converts the XML appearance into Java objects. It works the very simple way to take an XML view and create a java object and set the value of all attributes and repeat it’s on all children nodes. The are two methods are used getSystemServices() and get Layout Inflator().

<Paste the following code on an activity that hosts the layout>

File:MainActivity.java

The following code contains three main elements. The number one element is arrays. The number two layout inflator. The number three adapter. The first array you will define string.xml and second arrays of the image you will define in the drawable folder.

Let you ran the application, just click on toolbar find the option <Run>click on it. The following is beautiful output.

Android Customized ListView Example

 

What is the custom listview

Android structure, of course, gives us the capacity to make list items which incorporates just a single view. In any case, we generally run over applications that demonstrate different data in a single ListItem, for example, Whatsapp and some more. Executing your own custom adapter for customized listview.

How to create custom listView

  1. Find the Data source.
  2. Define the appearance of the single row.
  3. Create the custom adapter that maps the data source.
  4. A custom adapter or similar to adapter really connects between user interface segments and the information source that fill information into user interface Component.
  5. The custom adapter holds the information and sends the information customerAdapter view can take the information from the BasedAdapter or customAdapter view and demonstrates the information on various perspectives.
Create a new Layout
->Right click> the ->layout folder-> select ->New option->and ->click ><layout resource folder-> and<after the layout is created>

<Paste the following code>

The following layout consists of the listview and set height and width and also define the _id for unique identified. This is listview on which data map.

File:activity_main.xml

Create a new Layout 
<Paste the following code>

The following is the Row_xml contain two images and two text view. This layout is used in layout inflator to show each row. There is one property is different which is image view. This simple way to use it.<android:src=”@drawable/bhh is image name” >  android: src define the source of the drawable folder where the image is saved.

File:Row_xml

Open <res> click on <values>  than open <string.xml>

<Paste the following code>

The following two <titles><placess>array are define in string .xml.

File:string.xml

The main activity code three class. The ZUBAdapter class used for BasedAdapter. This class extend BasedAdapter and implementing all the method of BasedAdapter. The following is the detail of these methods.

Custom Adapter method () Used for what
public int getCount()  It returns all the elements in an array.
public Object getItem (int position) These returns object at the position.
public long getItemId (int position) That returns the id of each row.
public View getView(int position, View convertView, ViewGroup parent) Show the appearance of each row.
<Paste the following code on an activity that hosts the layout>

File:MainActivity.java

Ley runs the application here see the amazing output. The following output contains four arrays the two arrays are defined in the string.xl and two are defined in the main_activity also define the resource in the drawable folder.

Android Customized ListView Example

Conclusion

I wish I could tell you that a great site. You just understand the key element above post-?. More, detail about Android ListView Example. Attempt to make it editable, change to literary style shading, content style family, width, content size thus fo content sizes the result. You can in like manner endeavor above case with various Android Custom ListView Example in a solitary action.  Thank you for perusing this address. Expectation you got the thought.