ListView is easier to use and straight forward if we extend ListActivity. But what happens if the activity already extends some other activity. Unfortunately , we do not have an option to extend two activities.
No issues, we could still create our customized list without extending ListActivity.
In the below example 'selectprofile.xml', a list called profilelist is declared as below.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="@color/bgcolor"> <TextView android:id="@+id/header" android:layout_width="fill_parent" android:layout_height="50dp" android:textColor="@color/headtxtcolor" android:text="Select Profile" android:textSize="20dp" android:textStyle="bold" android:gravity="center" android:layout_gravity="center_horizontal" android:background="@drawable/button_image" /> <ListView android:id="@+id/profilelist" android:layout_width="fill_parent" android:layout_height="fill_parent" android:drawSelectorOnTop="false" android:layout_weight="1" android:dividerHeight="1dp"> </ListView> </LinearLayout> |
In the code
static final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selectprofile);
...
...
initializeList();
getProfData();
lv.setAdapter(adapter);
}
protected void initializeList(){
lv = (ListView)findViewById(R.id.profilelist);
adapter = new SimpleAdapter(this,list,
R.layout.selectprofile_row,
new String[] {"id","name","sex","year","date"},
new int[]{R.id.profid,R.id.name,R.id.sex,R.id.year,R.id.crdatevalue}
);
list.clear();
}
|
Here 'selectprofile_row' defines the layout of the list row.
After populating the array list in getProfData() , set adapter to the list view. You can also add listener to the list item , if needed.
No comments:
Post a Comment