vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Thursday, July 19, 2012

How to show two items in a List View in android application


Already I have written articles about ListView, it’s about to context menu using in listview.


Here I would share the method to use display two items in List View.
For display Single item in ListView we use array adapter, to display multiple items in List View, we use simple adapter.
Below I clearly depict to use the step by step method to use multiple items in ListView.
Step 1:
First create a layout to use in list view, with respect to the display items must add the textview,Here I would display two items and one image in list view, so I added two TextView and image .
rowlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingTop="20dp">          
<ImageView
        android:id="@+id/icon"
        android:layout_width="22dp"
        android:layout_height="22dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="4dp"
        android:src="@drawable/man" >
    </ImageView>
    <LinearLayout
       android:layout_width="wrap_content"
    android:layout_height="wrap_content"
          android:orientation="vertical">
    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
       android:textSize="12sp"        
       android:textStyle="bold"
        android:textColor="#000000">       
    </TextView> 
     <TextView
        android:id="@+id/label1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:textStyle="italic"
        android:typeface="sans"
        android:textColor="#000000">       
    </TextView>
    </LinearLayout> 
</LinearLayout>

Step 2:
Add ListView in activity layout
 List.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
<ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        >
    </ListView>
</LinearLayout>

Step 3:
Here use the java code to set the data in ListView.By using Hashmap string we set two arrays string str and country in List View.
package de.dotnetcode.android.list;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.widget.SimpleAdapter;

public class Mylist extends Activity {
String[] str,country;
       ListView lst;
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
setContentView(R.layout.list);

str=new String[] {"Kumar","Rajesh","Daliel","Salim","Mohammed"};
country=new String[] {"India","Japan","Singapore","USA","UK"};
lst = (ListView) findViewById(R.id.listView1);
ArrayList<HashMap<String, String>> mylistData =
                          new ArrayList<HashMap<String, String>>();

String[] row = new String[] {"row1", "row2"};

for(int i=0;i < str.length; i++)
                     {
                      HashMap<String,String> map = new HashMap<String, String>();
                      //initialize row data
                           map.put("row1",str[i]);
                         map.put("row2", country[i]);
                      mylistData.add(map);
                     }
 SimpleAdapter arrayAdapter =
                             new SimpleAdapter(this, mylistData, R.layout.rowlayout,
                                           row , new int[] {R.id.label,R.id.label1});
                      lst.setAdapter(arrayAdapter);
}
}

I hope the example would help you to understand the ListView process..
Incase if you have any doubts or any issues to implement the method. Please post your comments here…










0 comments:

Post a Comment