Listview In Android Part 1

android_learners_hub
android_learners_hub

        ListView In Android using Array Adapter

Android ListView
ListView is a viewgroup(Build your First App Part 2 (onlyjavaforall.blogspot.com)) which groups several items from a data source like array or database and displays them in a scroll-able list. This is vertical scrollable list in which each view is present exact below the previous view (best example of this is your call logs, What's App).Listview is also the subclass of Adapter View. Data are bound with listview using an Adapter class, so, you will have your first glimpse of using adapter in this post.

To use ListView you have to Import class android.widget.ListView. This can also be done automatically or by using shortcut key Alt+enter.
Here, we will create a listview using Array Adapter(We can create our own adapter by extending Base Adapter). So, lets have a short look on "What is Array Adapter?".

What is Array Adapter?
As the name suggest that this adapter use array (homogeneous collection of data) as a data source. It creates a view by calling Objects#toString (here Object is class and tostring() is its method) from the collection of array that we provide and place them in the textview. It can be easily used with list based user interface widgets such as Spinner and Listview.

How to initalise an Array Adapter :-
ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.ListView,StringArray);

To set Adapter:-
ListView listView ;          // make variable of ListView that is listView 
listView = (ListView) findViewById(R.id.listview);      // then, find the View by using ID from XML.
listView.setAdapter(adapter);         // set Adapter to listview by passing the variable of array adapter in setAdapter() 

Now, lets start coding for creating ListView by using the Array Adapter:-

Step 1:
To display a list, you can include a list view in your layout XML file:-
First use <Listview> tag and give it ID as shown below, you can also give width and height if you want.

<ListView
      android:id="@+id/list_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />

Other attributes used in XML are:-
1.android:divider :-Drawable or color to draw between list items.
2.android:dividerHeight :-To set Height of the divider.
3.android:entries :-It refer to an array resource that will populate the ListView.
4.android:footerDividersEnabled :-When set to false, the ListView will not draw the divider before each footer view.
5.android:headerDividersEnabled :-When set to false, the ListView will not draw the divider after each header view.

Step 2:
Write all the data in Strings.xml file that you want to show in listview. You have to write this in String-array shown as following:-
        <string-array name="listItem">
        <item>1.Introduction</item>
        <item>2.Components</item>
        <item>3.Build your First App Part-1</item>
        <item>4.Build your First App Part-2</item>
        <item>5.View and ViewGroup</item>
        <item>6.Challenges in App development</item>
        <item>7.Adapter</item>
        <item>8.ListView</item>
        <item>9.Layout</item>
        </string-array>

Step 3:
Set array adapter in MainActivity.java file.

Source Code

Strings.xml 
<resources>
<string name="app_name">ListView</string>
<string-array name="listItem">
<item>1.Introduction</item>
<item>2.Components</item>
<item>3.Build your First App Part-1</item>
<item>4.Build your First App Part-2</item>
<item>5.View and ViewGroup</item>
<item>6.Challenges in App development</item>
<item>7.Adapter</item>
<item>8.ListView</item>
<item>9.Layout</item>
</string-array>
</resources>


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/linear"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="android_learners_hub"
android:textAllCaps="true"
android:textSize="30dp"
android:layout_marginBottom="5dp"/>

<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EFE8E8"
android:cacheColorHint="@color/purple_700"
android:paddingStart="5dp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingEnd="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp" />

</LinearLayout>

MainActivity.java

package com.example.listview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text= findViewById(R.id.text);
ListView listView=findViewById(R.id.list);
String[] listitem=getResources().getStringArray(R.array.listItem);
ArrayAdapter adapter= new ArrayAdapter(this, android.R.layout.simple_list_item_1,android.R.id.text1,listitem);
listView.setAdapter(adapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// TODO Auto-generated method stub
String Item= (String) adapter.getItem(position);
Toast.makeText(getApplicationContext(),Item, Toast.LENGTH_SHORT).show();

}
});
}
}

Output

output of listview

output of listview with toast message

This post was on listview in which you learn to create a simple listview. In the second part ,we will learn to create custom listview. Hope you like this blog . Please follow , Subscribe and for any query feel free to comment.
-------------------------------------------------------------------------------------------------------------------------------
android_learners_hub




Comments

Post a Comment