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;
}
   
    }
}





UPDATING THE SDK




In Eclipsc Window > Select Android Sdk and Avd manager

Then a window will open



In that select Available Packages then it will list all the available packages select the things what ever you want and click on install selected then sdk will updated.

Note :  

For updating sdk also we need INTERNET.

INSTALLING ANDROID SDK ON YOUR SYSTEM

Step 1:

Downloading the Eclipsc from the following link.


The "Eclipse Classic" version is recommended.

Step 2:

Downloading the "Software Development Kit"

The SDK starter package is not a full development environment—it includes only the core SDK Tools, which you can use to download the rest of the SDK components (such as the latest Android platform).


Step3:

Installing ADT (Android Development Tool) for the Eclipsc.



Step 4:

Adding ADT to Eclipsc.

 Help > Install New Software.... > add the downloaded ADT and click "OK".

Step 5:

Adding the Downloaded SDK

Window  > Preferences >  Then a window will open...




In that select Android. Then click on browse and target the downloaded sdk and click on ok.


That's it. Installation completed.

Note :  

For the Step 5 we need INTERNET.



Sax Parsing


try
{
        //url for parsing
        URL url = new URL("Your URL");        

          //reading the data at the specified url
        
          //For the local file
          //input = getClass().getResourceAsStream("/res/raw/your.xml");
        input = url.openStream();
         
          //Creating object for SAXPARSER FACTORY
        SAXParserFactory spf = SAXParserFactory.newInstance();

        //CREATION OF OBJECT FOR SAXPARSER
        SAXParser sp = spf.newSAXParser();

        //READING AND PARSING
        XMLReader xr = sp.getXMLReader();
SaxHandler myXMLHandler = new SaxHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(input));
}
catch(Exception e)
{
       e.printStackTrace();
 }





SaxHandler.java

public class SaxHandler extends DefaultHandler
{

Boolean currentElement = false;
String currentValue = null;
NewsItem objNewsItem;
boolean test_Des = false;

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
currentValue = "";
currentElement = true;

if (localName.equalsIgnoreCase("START TAB"))
{
//VECTOR CREATION
                        //OBJECT CREATION
}

}

@Override
public void endElement(String uri, String localName, String qName)throws SAXException
{
                //HERE YOU HAVE TO INSERT TAG NAMES THAT YOU HAVE TO READ
currentElement = false;
if (localName.equalsIgnoreCase("TAG NAME"))
{
VAR = currentValue;
}
else if(localName.equalsIgnoreCase("TAG NAME"))
{
VAR = currentValue;
}
else if(localName.equalsIgnoreCase("TAG NAME"))
{
        YOURVECTOR.add(objNewsItem);
}

}

@Override
public void characters(char[] ch, int start, int length)throws SAXException
{
if (currentElement)
{
currentValue += new String(ch, start, length);

}
}

}