Tuesday, December 13, 2011

Android ListView Simple Adapter(custom style) example

This is an example of a very simple list view with custom style/layout.
ListView itemListView = (ListView)findViewById(R.id.itemChoseList);
List docList = new ArrayList();
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("docTitile1", "My Title");
temp.put("docTitile2", "Sub title");
docList.add(temp);
simpleAdapter = new SimpleAdapter(
this,
docList,
R.layout.doc_row_lite,
new String[] {"docTitile1","docTitile2"},
new int[] {R.id.doc_lite_title1, R.id.doc_lite_title2});
itemListView.setAdapter(simpleAdapter);

doc_row_lite.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="wrap_content"
android:padding="2dip">

<TextView
android:id="@+id/doc_lite_title1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:textColor="#000000"
android:textSize="18dip"
android:gravity="center_vertical"
/>
<TextView
android:id="@+id/doc_lite_title2"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:textSize="13dip"
android:text="shamim"
android:textColor="#111133"
android:singleLine="true"/>
</LinearLayout>

1 comment:

fawwad said...

it was showing some warning so i changed some code.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class FourthScreenActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen4);
ListView itemListView = (ListView)findViewById(R.id.itemChoseList);
List> docList = new ArrayList>();
HashMap temp = new HashMap();
temp.put("docTitile1", "Fawwad is Title");
temp.put("docTitile2", " Khan is Sub title");
docList.add(temp);
SimpleAdapter simpleAdapter = new SimpleAdapter(
this,
docList,
R.layout.doc_row_lite,
new String[] {"docTitile1","docTitile2"},
new int[] {R.id.doc_lite_title1, R.id.doc_lite_title2});
itemListView.setAdapter(simpleAdapter);
}
}