Friday 23 January 2015

ANDROID LISTVIEW TUTORIALS FOR BEGINNERS


Android listview is a group view that displays a list of scrollable items. A simple example is phone Contact book, where you have a list of your contacts displayed in a listview.
To use a listview in android, drag and drop the listview control from the pallet to your user interface (UI).
LISTITEM.
List items are individual rows in a listview where data for each individual item is displayed. For example in the phone contact book mentioned earlier, each person’s contact details (could be their photo, number, name & email) are displayed in a single row (list item).
ADAPTER:-
Acts as the bridge between the view (e.g. Listview or gridview) and the underlying data for the view.
It just manages the adapter with the data and adapts the data to the individual rows (list item) of the view.
-        setAdapter() method is used to bind the adapter with the listview.
  Some common adapters provided by android include:-
a.      Simple cursor adapter.
b.      Array adapter.
c.      Cursor adapter.
But for the most part you would like to make your own adapter, and to do that you extent BaseAdapter.


Alright, enough basics let’s try a simple listview application now using Arrayadapter.

-        This kind of adapter is mostly used if you have a list of single items backed by an array. e.g. List of fruits names, list of cities, list of planets etc.
Let’s create a string array of fruits;
    String [] fruits= new String[] {“Orange”,”Apple”,”Mangoes”,”pineapple”,”Bananas”}; // this is our source data.

Next is to create the arrayadapater;
ArrayAdapter<String> fruitAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fruits);

fruitAdapter is an object of ArrayAdapter.
The parameters include:-
a.      Context… this
b.      Layout , which the adapter will use to bind the data from fruit array ; here we use _simple_list_item_1, displays a simple text view(you can use simple_list_item_multiple_choice to add check box after each textview)
c.      The 3rd is the data ……. fruits.

The last thing is to reference our listview and bind it to our adapter.
ListView fruitList= (ListView)findViewById(R.id.lst);
 fruitList.setAdapter(fruitAdapter);

Here is the code:


<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"
    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=".SharedPrefActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>



package com.example.listviewtutorial;

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

public class ListViewActivity extends Activity {

ListView fruitList;
String[]fruits= new String[]{"orange","apple","Bananas","Mangoes","Carrots","Tomatoes","onions","potatoes",};// make it longer if you wish
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        
 ArrayAdapter<String> fruitsAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,fruits);
        
        
        fruitList=(ListView)findViewById(R.id.listView1);
        
        fruitList.setAdapter(fruitsAdapter);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.shared_pref, menu);
        return true;
    }
    
}