Ticker

6/recent/ticker-posts

Custom ListView in android with item click


In this blog of Android UI, we have discussed How to implement the item click event on the ListView using Custom Adapter.




What is ListView?

In ListView, display the data in the columns list and it is scrollable. A list view is an adapter view that does not understand the features, such as type and contents, of the views it carries. For that, ListView requests a view from ListAdapter for displaying the different views as the user scrolls up and down


What is the Adapter? 

Adapters are like the bridge or string that attached between the Data Source and the UI components. It converts the data items into view items so that they can display them in the UI components. 

There are different types of Adapters available in Android. Base AdapterArray AdapterCursor Adapter, Custom Array AdapterSimple AdapterCustom Simple Adapter are the types of the Adapter.

Here we used the Custom Adapter that extends the Base Adapter.

There are some layouts like ListView and GridView for which you need to use an Adapter"Because these layouts are changing, you can't plan for these layouts." For that, you need to use a subclass of the AdapterView class an Adapter. The Adapter holds the data in the form of an array or database query and converts data into a view.



What is AdapterView?

 

AdapterView is the subclass of the Adapter. Adapter sets the value for the AdapterView. That means if Adapter is a parent then the AdapterView is like a child of the Adapter. And the Adapter also set the children of the AdapterView. It's like the Adapter is human and AdapterView is a robot what function we put on the robot it works like that only.

AdapterView is the ViewGroup that displays the items loaded into the adapter. We mostly use the array type adapter to load data.

And AddpterView handles the selection by using the class of AdapterView.OnItemClickListener and set it into a listener to invoke the selected item. And the listener that usually used is the setOnItemClickListener


What is setOnItemClickListener?

public void setOnItemClickListener (AdapterView.OnItemClickListener listener)

This is a callback method that invoked when the items of the AdapterView has been clicked.

Here the AdapterView.OnItemClickListener is a parameter that invoked when an item has been clicked

 

Creating ListView using Custom Adapter in Android

The following example shows the item click on the ListView.


We already discuss how to create a custom listView using an adapter in the previous blog of Android UI. Please go through it on how to create a custom listView using an adapter.

Here we are going to see how to set the item click event on an item. For that first set the adapter and impose the setOnItemClickListener


Here as you see, we're using the putExtra() method to send data from this activity to another activity. The putExtra() is a method that is used to set some extended data into the intent. It has two parameters. The first one is the name which we want to send to another activity and another one is the data itself.


File: MainActivity.java


Create another activity to display the data

Create an empty activity to display the clicked item.

File: activity_second.xml


File: SecondActivity.java


Here we first declare the views and set it with its IDs, then we code for the extras that are got from the main activity. In the getStringExtra() method we put the names that are set in the putExtra() method.

At last set the back pressed method.



Quick Recap:

  • A list view is an adapter view that does not understand the features, such as type and contents, of the views it carries.
  • Adapters are like the bridge or string that attached between the Data Source and the UI components. It converts the data items into view items so that they can display them in the UI components. 
  • AdapterView is the subclass of the Adapter. Adapter sets the value for the AdapterView.
  • setOnClickListener is a callback method that invoked when the items of the AdapteView has been clicked.
  • The putExtra() is a method that is used to set some extended data into the intent.


You can also see:



Post a Comment

0 Comments