Android Listview From Array

July 25, 2017

Android Listview ia group of several item. It display of  scrollable list. This list automatically add in the list item form array. We use locally array or use database like json. Here we data insert form arraylist and sent to data adapter view. This data  can shows different whay like list view, grid view and spinner etc. This Listview and Gridview are subclasses for Adapter, Which save data from another source and view in list.

Android  array listview

Android array listview

It is a simple android listview from array. We can customize any time from arraylist.

 

Example:

First Create Project in android studio

Android XML CODE:

<?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”

  android:background=“@drawable/bg_appslayout”

  tools:context=“com.layout.apps.listviewfromarray.MainActivity”>

""</span

  <ListView

      android:id=“@+id/listView”

      android:background=“#64ffffff”

      android:layout_width=“fill_parent”

      android:layout_height=“wrap_content”

      android:layout_alignParentTop=“true”

      android:layout_centerHorizontal=“true”

      tools:layout_editor_absoluteY=“0dp”

      tools:layout_editor_absoluteX=“8dp” />
</android.support.constraint.ConstraintLayout>

 

 

 

 

 

Java Code:

package com.layout.apps.listviewfromarray;

 

//————SM—————–

import android.content.DialogInterface;

import android.os.Bundle;

import android.support.v7.app.AlertDialog;

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;

public class MainActivity extends AppCompatActivity {

  ListView listView;

  @Override

  protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

 

      // Define string array.

      final String[] listValue = new String[] {“One”,“Two”,“Three”,“Four”,“Five”,“Six”,“Seven”,“Eight”,“Nine”,“Ten”,“Eleven”,“Twelve”,“Thirteen”,“Fourteen”,“Fifteen”,

              “Sixteen”,“Seventeen”,“Eighteen”,“Nineteen”,“Twenty”}; // array list

     

          listView = (ListView)findViewById(R.id.listView);  //listView string show

          ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_item, android.R.id.text1, listValue);

          listView.setAdapter(adapter);

 

      listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { // listview onclick for chack string position

          @Override

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

 

           //—————–Alert Dialog ———————————————————

              AlertDialog.Builder sm = new AlertDialog.Builder(MainActivity.this);

              sm.setTitle(listValue [position].toString());

              sm.setMessage(“Are you sure?”);

              sm.setPositiveButton(“YES”, new DialogInterface.OnClickListener() {

                  public void onClick(DialogInterface dialog, int which) {

                      dialog.dismiss();

                  }

              });

              sm.setNegativeButton(“NO”, new DialogInterface.OnClickListener() {

                  @Override

                  public void onClick(DialogInterface dialog, int which) {

                      dialog.dismiss();

                  }

              });

 

              AlertDialog alert = sm.create();

              alert.show();

 

              //toast for onClick item show position wise

              Toast toast = Toast.makeText(getApplicationContext(),listValue [position].toString(),Toast.LENGTH_SHORT);

              toast.show();

          }

      });

  }

}
 

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *