Hello, welcome to bunkerspoint ,this topic is based on list view in android with example. So what’s list view in android? In android developers official website it says,“A list view is an adapter view  that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter  as needed, such as to display new views as the user scrolls up or down”.

In simple, a list view is a view that contains list items and display them in vertical scrollable, which allows user to scroll the list items up and down vertically. ListView uses adapter to bind data from it to display in ListView. Think of an adapter as a middleman between Adapter view and any other view such as ListView , GridView etc.

USING ADAPTER IN LISTVIEW

ListView is a subclass of AdadpterView and they can be populated by binding to an Adapter. An Adapter retrieves data and represents those data. There are some common adapters which are frequently used such as ArrayAdapter, BaseAdapter, CursorAdapter etc..


In this tutorial, we are going to make use of ArrayAdapter. We are going to see how to implement listView in android studio using ArrayAdapter.
After following this tutorial, you would have the following output.

LISTVIEW IN ANDROID 1

Lets begin with creating activity_main.xml

Create a lisView inside the activity_main.xml and give it a id of listView.

<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for the main screen -->
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp”
    android:paddingRight="16dp"
    android:paddingTop="16dp" />

Create a new layout resource file and name it as list_items

<!--  Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16dp"
    android:textStyle="bold" >
</TextView>

In mainActivity.java add this code snippet.

package com.bunkerspoint.android.listView;

import android.os.Bundle;
 import android.support.v7.app.AppCompatActivity;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;
 import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
    ArrayList<String> arrayList = new ArrayList<>();
    arrayList.add("Manuj");
    arrayList.add("Pratik");
    arrayList.add("Yash");
    arrayList.add("Yatendra");
    arrayList.add("Yagnesh");
    arrayList.add("Mayur");
    arrayList.add("Manuj");
    arrayList.add("Raju");
    arrayList.add("Yadav");
    arrayList.add("Dhoni");


    ArrayAdapter<String>arrayAdapter = new      ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,arrayList);
    // Get a reference to the ListView, and attach the adapter to the listView.
    ListView listView = (ListView) findViewById(R.id.listview_flavor);
    listView.setAdapter(arrayAdapter);
 }
}

The above code snippet creates a listView with list items as shown below.

HANDLE CLICK LISTENERS IN LISTVIEW

The above code snippet only shows the list item but listview are useless when we are not able to click those list items. So below code snippet will let you handle click events in listView so that you could be able to use it in your projects.

listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

        String selectedItem=arrayList.get(position);
        Toast.makeText(getApplicationContext(), "Person Selected : "+selectedItem,   Toast.LENGTH_LONG).show();
    }
});

Place this code snippet in MainActivity.java inside the onCreate()methd.

Let me explain whats happening in the MainActivity.java.
So, we instantiated ArrayList of type String and named it as arrayList and then added the items in it.

Then we created a ListView and ArrayAdapter which taken in as String and named it as arrayAdapter and we set the adapter to listView by listView.setAdapter()method.

To handle click events in our listView, we used the onItemClickListener and said it what we want to do when a user clicks on any item. In this case we toast the items upon click.


In this post, you learnt about how to create a listView and use it to populate list items in it by using ArrayAdapters. The ArrayAdapter in this tutorial uses a string type and not custom implementation and also the ArrayAdapter uses Android Studio’s default (provided layout of simple_list_item_1) adapter. So if you want to learn more about how to implement a CustomAdapter then check it out at our website
http://bunkerspoint.com/category/android/

LEAVE A REPLY

Please enter your comment!
Please enter your name here