Wednesday, April 6, 2011

Listview in Android

The listview is the most important view of the Android. For the mobile applications memory is the main concern. So we will use listview to optimize memory. For example we want to show a list of records at that there are two possibilities are there.

1. using for loop we will add the views to a LINEAR LAYOUT.

2. using a LISTVIEW.

In both cases we will get the same output. But the second way is the optimized way.

If we implement first way it will create all views at a time only. So the memory will waste.

If we implement the second thing then listitems will create dynamically. It mean we are having 30 records. At a time we have to see only 5 items according to screen resolution. Then listview will create only 5 items at the first time and if we scroll down to view the other items at that time it will create the other views. It mean it will not create 30 records views at a time.

Example :

main.xml:



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
 
    <ListView
    android:layout_width="fill_parent"
    android:id="@+id/lvList"
    android:layout_height="wrap_content">
    </ListView>

</LinearLayout>

header.xml:

Xml for the listview header.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <TextView
  android:layout_width="fill_parent"
  android:text="Header"
  android:layout_height="wrap_content">
  </TextView>

</LinearLayout>
listlayout.xml
Xml for the listitem view.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">

  <TextView
  android:layout_width="fill_parent"
  android:id="@+id/tvTitle"
  android:text="listitem"
  android:gravity="center_vertical"
  android:layout_height="30dip">
  </TextView>

</LinearLayout>

Home.Java:
This is the java file . Here we are using listview with custom list item. In the list item at present i am using on textview. In that you have to put what ever you want.

package com.exlistview.activities;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

public class Home extends Activity 
{   
ListView lvList;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //GETTING THE ID OF THE LISTVIEW
        lvList = (ListView)findViewById(R.id.lvList);
       
        //Adding Header to ListView
        
        //INFLATING THE HEADER VIEW FROM THE XML
        LayoutInflater inflater = getLayoutInflater();
        View header = (View)inflater.inflate(R.layout.header, lvList, false);
        
        //ADDING TO LISTVIEW
        lvList.addHeaderView(header, null, false);
        
        //SETTING THE ADAPTER
        lvList.setAdapter(new listAdapter());      
        
    }
    
    public class listAdapter extends BaseAdapter
    {

@Override
public int getCount() 
{
//ITEM SIZE
return 20;
}

@Override
public Object getItem(int arg0) 
{
return arg0;
}

@Override
public long getItemId(int arg0) 
{
return arg0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) 
{
//INFLATING THE ITEM FOR THE LIST
if(arg1 == null)
arg1 = getLayoutInflater().inflate(R.layout.listlayout, null);
return arg1;
}
   
    }
}





No comments:

Post a Comment