Monday 5 March 2018

Simple ListView Tutorial

Simple ListView Android

Today we will learn how to create a Simple ListView in Android. The ListView is a view group that displays a list of vertical scrollable items. We will create a
ListView that allows users to scroll the list, and on ListView item click will show results on a new activity using an intent method.


Android Simple Listview
Android Simple Listview



ListView is a groups of several items and display them in vertical / Horizontal scrollable list.List items inserted to the list using an Adapter that gets the data from a source such as an array or database.


First, Create a new project in Android Studio, select FileNew > New Project. Fill in the details and name your project SimpleListView.

Application Name : SimpleListView
Project Name : SimpleListView
Package Name : com.stacklearning.simplelistview

Open layout file :


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.stacklearning.simplelistview.MainActivity">

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

</android.support.constraint.ConstraintLayout>




Open your MainActivity.java and paste the following code.



package com.stacklearning.simplelistview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    ListView listView;
    List<String> listValue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.list);
        listValue = new ArrayList<>();
        listValue.add("First Row List View");
        listValue.add("Second Row List View");
        listValue.add("Third Row List View");
        listValue.add("Forth Row List View");
        listValue.add("Fifth Row List View");
        listValue.add("Sixth Row List View");
        listValue.add("Seventh Row List View");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, listValue);
        listView.setAdapter(adapter);
        // ListView Item Click Listener
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                int itemPosition = position;
                String itemValue = (String) listView.getItemAtPosition(position);
                Toast.makeText(getApplicationContext(),
                        "Position clicked:" + itemPosition + "  List Item is : " + itemValue, Toast.LENGTH_LONG)
                        .show();
            }
        });
    }
}


                                                     DOWNLOAD CODE

0 comments:

Post a Comment