Many custom Adapter tutorials contain subtle error which can be hard to find and fix. Even efficient list adapter sample from Android SDK contains this bug. If you compile and run the sample without any changes you should see something like this:
There’s nothing wrong with the list on this screenshot, as long as that’s the look you want. But what if you want the list items to be wider? Let’s change the list item layout:
<?xml version="1.0" encoding="utf-8"?><!-- Copyright (C) 2007 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.--><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="150dip"><ImageViewandroid:id="@+id/icon"android:layout_width="48dip"android:layout_height="48dip"/><TextViewandroid:id="@+id/text"android:layout_gravity="center_vertical"android:layout_width="0dip"android:layout_weight="1.0"android:layout_height="wrap_content"/></LinearLayout>
What’s changed? Nothing, nada, zilch, zip. No changes whatsoever:
You can use hierarchy viewer tool to verify that the list item height was not set correctly.
There’s obviously nothing wrong with the layout xml, so let’s take a look at the code:
/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */packagecom.example.android.apis.view;importandroid.app.ListActivity;importandroid.content.Context;importandroid.os.Bundle;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.BaseAdapter;importandroid.widget.TextView;importandroid.widget.ImageView;importandroid.graphics.BitmapFactory;importandroid.graphics.Bitmap;importcom.example.android.apis.R;/** * Demonstrates how to write an efficient list adapter. The adapter used in this example binds * to an ImageView and to a TextView for each row in the list. * * To work efficiently the adapter implemented here uses two techniques: * - It reuses the convertView passed to getView() to avoid inflating View when it is not necessary * - It uses the ViewHolder pattern to avoid calling findViewById() when it is not necessary * * The ViewHolder pattern consists in storing a data structure in the tag of the view returned by * getView(). This data structures contains references to the views we want to bind data to, thus * avoiding calls to findViewById() every time getView() is invoked. */publicclassList14extendsListActivity{privatestaticclassEfficientAdapterextendsBaseAdapter{privateLayoutInflatermInflater;privateBitmapmIcon1;privateBitmapmIcon2;publicEfficientAdapter(Contextcontext){// Cache the LayoutInflate to avoid asking for a new one each time.mInflater=LayoutInflater.from(context);// Icons bound to the rows.mIcon1=BitmapFactory.decodeResource(context.getResources(),R.drawable.icon48x48_1);mIcon2=BitmapFactory.decodeResource(context.getResources(),R.drawable.icon48x48_2);}/** * The number of items in the list is determined by the number of speeches * in our array. * * @see android.widget.ListAdapter#getCount() */publicintgetCount(){returnDATA.length;}/** * Since the data comes from an array, just returning the index is * sufficent to get at the data. If we were using a more complex data * structure, we would return whatever object represents one row in the * list. * * @see android.widget.ListAdapter#getItem(int) */publicObjectgetItem(intposition){returnposition;}/** * Use the array index as a unique id. * * @see android.widget.ListAdapter#getItemId(int) */publiclonggetItemId(intposition){returnposition;}/** * Make a view to hold each row. * * @see android.widget.ListAdapter#getView(int, android.view.View, * android.view.ViewGroup) */publicViewgetView(intposition,ViewconvertView,ViewGroupparent){// A ViewHolder keeps references to children views to avoid unneccessary calls// to findViewById() on each row.ViewHolderholder;// When convertView is not null, we can reuse it directly, there is no need// to reinflate it. We only inflate a new View when the convertView supplied// by ListView is null.if(convertView==null){convertView=mInflater.inflate(R.layout.list_item_icon_text,null);// Creates a ViewHolder and store references to the two children views// we want to bind data to.holder=newViewHolder();holder.text=(TextView)convertView.findViewById(R.id.text);holder.icon=(ImageView)convertView.findViewById(R.id.icon);convertView.setTag(holder);}else{// Get the ViewHolder back to get fast access to the TextView// and the ImageView.holder=(ViewHolder)convertView.getTag();}// Bind the data efficiently with the holder.holder.text.setText(DATA[position]);holder.icon.setImageBitmap((position&1)==1?mIcon1:mIcon2);returnconvertView;}staticclassViewHolder{TextViewtext;ImageViewicon;}}@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setListAdapter(newEfficientAdapter(this));}privatestaticfinalString[]DATA=Cheeses.sCheeseStrings;}
I’ve highlighted the problematic line. It turns out that you have to use another overload of LayoutInflater.inflate method:
We set the attachToRoot to false, because we just want to properly intialize LayoutParams for the LinearLayout of the item and let the ListView to add the inflated views wherever it needs. In fact, setting it to true causes exception to be thrown from AdapterView: