How to use ListView in android.

List views are views that enable you to display a long list of items. In Android, there are two types of list views: ListView and SpinnerView. Both are useful for displaying long lists of items. The following Try It Outs show them in action.
The ListView displays a list of items in a vertically scrolling list. The following Try It Out demonstrates how to display a list of items using the ListView.

In ListView  You need to use ArrayAdapter class to store the Arraylist.

 

//Simple ListView Example in android

Step 1: Firstly, open your activity_main.xml file and add following code ( Open res -> layout -> activity_main.xml and add following code:)
// activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.ravi.listview.MainActivity">

    <ListView
        android:id="@+id/simcards"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
</RelativeLayout>

Step 2: Let’s  open .Java file and and add following code (Open src -package -> MainActivity.java and add following code: )
Code//

package com.example.ravi.listview;

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

public class MainActivity extends Activity implements AdapterView.OnItemClickListener {
    ListView lv;

    String[] sm = {"Airtel","Aircel","Vodafone","Idea","Reliance Jio","Reliance","BSNL","Tata DoCoMo","MTNL","Telenor (Uninor)"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = (ListView) findViewById(R.id.simcards);

        ArrayAdapter<String>ad = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,sm);
        lv.setAdapter(ad);
        lv.setOnItemClickListener(this);


    }

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

        int itemPosition     = position;
        String  itemValue    = (String) lv.getItemAtPosition(position);

        Toast.makeText(getApplicationContext(),
                "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG).show();


        //Toast.makeText(this, sm[position], Toast.LENGTH_LONG).show();
    }
}

Step 3: Open AndroidManifest.xml and add following code :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ravi.listview">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Step 4 : Our output will be like this :

1

2

I hope you like this article.

Tags: ,