SimpleAdapter for ListView

Showing posts with label SimpleAdapter for ListView. Show all posts
Showing posts with label SimpleAdapter for ListView. Show all posts

Thursday, August 14, 2014

Creating SimpleAdapter for ListView in Android.....


This Article shows to create SimpleAdapter for ListView in Android.

1- Create row xml for ListView(row.xml).

<?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"
     android:orientation="vertical">
     <TextView android:id="@+id/text1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
     <TextView android:id="@+id/text2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1"/>
     <TextView android:id="@+id/text3"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_weight="1"/>
</LinearLayout>


2- Create ListActivity(MainActivity.java).

import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;

public class Abc extends ListActivity { 
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ArrayList<HashMap<String, String>> list =
                  new ArrayList<HashMap<String, String>>();

String[] from = new String[] {"Text1", "Text2", "Text3"};

int[] to = new int[] {R.id.text1, R.id.text2, R.id.text3};
for(int i=0; i<3; i++) {
HashMap<String,String> map = new HashMap<String, String>();
//initialize row data
map.put(from[i], "Text" + i);
list.add(map);
}
setListAdapter(new SimpleAdapter(this, list, R.layout.row, from, to));
}
}