What is listview

Android ListView is used to display the the list of items in vertical manner with scrollbar.For example we can display the list of fruits or name of the persion etc.

The getItemAtPosition(position) method of ListView take a position as argument and return the clicked item from the listview.


ListView:.

In this section i am going to show the ListView with practical example.

First open the android studio and create a project and give the name of your project like ListViewDemo.

The screenshots of the program is as follows:

Smiley face Smiley face

Drag and drop the one ListView from pallet. Open the activity_main.xml file and code look like this


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.niitpc.myapplication.MainActivity">


    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="72dp" />
</RelativeLayout>


       

After that you open the app->res->layout->right click on layout and a pop up menu display click new->xml->layout XML file after that a dialog box will display wright the layout name as name_list and click on finish.

You need to replace LinearLayout with TextView and code look like this.

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TextView>
     

Open the MainActivity.java file and write code look like this.


package com.example.niitpc.myapplication;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

ListView listView;
String[] Names=new String[]{"Ram","Shyam","Amit","Amita"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.listView);
ArrayAdapter adapter=new ArrayAdapter(this,R.layout.name_list,Names);
listView.setAdapter(adapter);
listView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
 String value=(String) listView.getItemAtPosition(position);
 Toast.makeText(MainActivity.this,"Position"+position+"Value"+value,Toast.LENGTH_LONG).show();
        }
     }
   );
  }
}



       

Explanation:

ListView listView:This statement create the object listview of ListView.

String[] Names=new String[]{"Ram","Shyam","Amit","Amita"};:This statement create the String array name is Names store the name of person.

listView=(ListView)findViewById(R.id.listView)This statement initialized the listview object.

ArrayAdapter adapter=new ArrayAdapter(this,R.layout.name_list,Names):This statement create the object adapter of ArrayAdapter.It will take three argument first as this second as layout that you create like name_list and third is array Names that store the names of persion.

listView.setAdapter(adapter):The setAdapter() set the adpter object into listview.

listView.setOnItemClickListener:This statement create the listener of listview.

String value=(String) listView.getItemAtPosition(position):The getItemAtPosition() method take a position as argument and return the click item from the listview and after converting into String we store into variable value.

Toast.makeText(MainActivity.this,"Position"+position+"Value"+value,Toast.LENGTH_LONG).show():The Toast display the position and value onto the screen with touch the name in listview.

String url=editText.getText().toString():The getText() return the enter url after converting it into String value assign into the url variable.


WebView

What is ListView for video Click here

Smiley face                     Smiley face