Sunday, 6 April 2014

Listview


What is ListView?

     A child control of View class that shows items in a vertically scrolling list. The items come from the adapter (ListAdapter, ArrayAdapter or CursorAdapter) associated with this view shows in a Listview.
       ListView is a control that can be used to show bulk of data in a form of list.the data can be items of String Array, number of records from Database ( SQLite ), JSON Array, ArrayList, Hash Map etc.The good thing about ListView is that it is faster, has its own Scroll view to scroll up or down, can have data in form of Strings,Images,videos almost everything.You can use it the way you want by customising a ListView, you can set custom layout for list view with any child view to create a row of ListView like TextView, Button, ImageView, Checkboxes,RadioButtons etc.

Simple Project on Listview (SimpleListView):

OutPut:
listview of weeksmonths in List
listview of landscape
listview of landscape week

Create new Android Project
Project Name: SimpleListView
//tested from 2.3.3 to current android sdk 
Build Target: Android 2.3.3   //or greater than that
Application Name: Simple List View
Package Name: com.shaikhhamadali.blogspot.simplelistview
Create Layout file: activity_simple_list_view

1. code of Layout:

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

    <Button
        android:id="@+id/btnArrayList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:onClick="fromArray"
        android:text="List from Array" />

    <Button
        android:id="@+id/btnArrayList2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/btnArrayList"
        android:onClick="fromStringsArray"
        android:text="List from Strings.xml" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/btnArrayList" >
    </ListView>

</RelativeLayout>

2. code of activity:

package com.shaikhhamadali.blogspot.simplelistview;



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


public class SimpleListView extends Activity {
 //Array of Week 
 String[] weeks={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_simple_list_view);

 }
 /**
  * This method will be called on List from Array button click
  * @param View
  * */
 public void fromArray(View v){
  //get reference of list view
  final ListView lv=(ListView)findViewById(R.id.listView1);
  /*create an array adapter and set simple layout
   * of single text view and pass an array of week described above
   */
  ArrayAdapter<String> adpt=new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1,weeks);
  //set this adapter to listview
  lv.setAdapter(adpt);
 }
 /**
  * This method will be called on List from Strings.xml button click
  * @param View
  * */

 public void fromStringsArray(View v){
  //get reference of list view
  final ListView lv=(ListView)findViewById(R.id.listView1);
  /*create an array adapter and set simple layout
   * of single text view and pass an array from strings.xml file
   */
  ArrayAdapter<String> adpt=new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.months));
  //set this adapter to listview
  lv.setAdapter(adpt);
 }


}

strings.xml:

     As you see above in activity i have created an array of Strings and here in strings.xml i have created an StringArray items, don't confuse to see this form of array because these both are the same thing.

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Simple List View</string>
    <string name="action_settings">Settings</string>

    <string-array name="months">
        <item>January</item>
        <item>February</item>
        <item>March</item>
        <item>April</item>
        <item>May</item>
        <item>June</item>
        <item>July</item>
        <item>August</item>
        <item>September</item>
        <item>October</item>
        <item>November</item>
        <item>December</item>
    </string-array>

</resources>

3. note that:

  • Good practice is to use Strings from strings.xml because strings.xml return Strings according to the Locale of device if you create an application for more than one countries.
  • You can use ListView for many purposes to provide list of options for user to select,to show settings menu items etc.
  • You can get all method details of Listview from developer.android.com.
  • Learn more about ListView, ArrayAdapter.

      4. conclusion:

      • Some information about how to use ListView.
      • know how to show Array of Strings in ListView.
      • Know how to load StringsArray from strings.xml in ListView.

        5. About the post:

        • The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
        •  Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
        •  Hope you enjoy it!
            6. Source Code:
                    you can download the source code from Gdrive,Github.

            Cheers,
            Hamad Ali Shaikh