My Android Experience
ArrayAdapter
Let’s say you need a simple ArrayAdapter but it might not be as simple as you think if you want to use List of complex objects in that adapter.
Here is my solution…
ArrayAdapter<Greeting> greetingAdapter = new ArrayAdapter<Greeting>(this, android.R.layout.simple_list_item_1,greetings) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getView(position, convertView, parent);
view.setText(greetings.get(position).getName());
return view;
}
};
How about you want to use your own layout rather than the built-in one. It’s possible but with a limitation. You can not simple call getView method of the super class as it requires only TextView to be present in your layout. If it’s what you need then no problem but if you want to display more than one column in your ArrayAdapter then you should create your own view using the inflater. Here is my sample…
ArrayAdapter arrayAdapter=new ArrayAdapter<ForwardingPhone>(this, R.layout.forwardingphonelist_item, forwardingPhones) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout view = (convertView != null) ? (LinearLayout) convertView : createView(parent);
ForwardingPhone forwardingPhone = forwardingPhones.get(position);
if (forwardingPhone.getPhoneEnabled()) {
view.setBackgroundColor(Color.parseColor("#99CCFF"));
}
TextView nameView = (TextView) view.findViewById(R.id.phoneName);
nameView.setText(forwardingPhone.getPhoneName());
TextView type = (TextView) view.findViewById(R.id.phoneType);
type.setText(forwardingPhone.getPhoneType());
TextView phoneNumber = (TextView) view.findViewById(R.id.phoneNumber);
phoneNumber.setText(forwardingPhone.getPhoneNumber());
return view;
}
private LinearLayout createView(ViewGroup parent) {
LinearLayout item = (LinearLayout) getLayoutInflater().inflate(R.layout.forwardingphonglist_item,
parent, false);
return item;
}
};
the layout xml file for R.layout.forwardingphonelist_item is as follows :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="?android:attr/listPreferredItemHeight" android:layout_width="fill_parent" android:orientation="horizontal" android:background="#FFFFFF" > <TextView android:id="@+id/phoneName" android:text="Name" android:layout_height="fill_parent" android:layout_width="wrap_content" android:layout_margin="5dip" android:layout_weight="3" android:textColor="#000000" android:textStyle="bold" android:textSize="20px" android:gravity="center_vertical" android:singleLine="true" /> <TextView android:id="@+id/phoneType" android:layout_height="fill_parent" android:layout_width="wrap_content" android:layout_margin="5dip" android:text="Type" android:layout_weight="1" android:gravity="center_vertical" android:singleLine="true" /> <TextView android:id="@+id/phoneNumber" android:text="Number" android:layout_height="fill_parent" android:layout_width="wrap_content" android:layout_margin="5dip" android:layout_weight="1" android:gravity="center_vertical" android:singleLine="true" /> </LinearLayout>
Nathan said
This was very handy, thank you!
Thomas Ellis said
Thanks for this post. I am interested with android tablet i write some android review at http://cheap-android.blogspoot.com I have bookmarked this page for my future rference. thanks