You've hit the problem exactly YuvieDroid. Boy do I feel dumb. I thought if the view was being re-used then the layout items on that view were preserved as well. Moving the bracket on the if statement fixed the issue immediately. Thanks!
On Nov 29, 1:44 pm, YuviDroid <[email protected]> wrote: > You should always set the whole content of the layout in getView(). When > "v" is not null right now you are returning that view without setting your > text. > Something like this: > > public View getView(int position, View convertView, ViewGroup parent) { > View v = convertView; > if (v == null) { > LayoutInflater li = (LayoutInflater) > mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); > v = li.inflate(R.layout.grid_item, null); > } > > TextView tv = (TextView)v.findViewById(R.id.grid_item_text); > tv.setText("Item #"+mItems.get(position)); > > return v; > > > > > > > > > > } > On Tue, Nov 29, 2011 at 5:17 PM, Kumar Bibek <[email protected]> wrote: > > What exactly is the problem. "Bonkers" doesn't really tell us much. > > > On Nov 29, 8:12 pm, authorwjf <[email protected]> wrote: > > > Hi Android Framework Gurus! > > > > I am hoping someone out there can answer a question for me about re- > > > using views in my adapters. My understanding, is that for performance > > > reasons this is something I should always always always do. Yet in my > > > experience, doing so always results in the view, whether a grid or > > > list, going wonky once I get very many children in the view. I > > > suspect, I'm just doing something flat out wrong. So I put together a > > > simple straight-forward project to demonstrate both how I attempt to > > > re-use the views in a grid view, and how it goes bonkers if you add a > > > few hundred entries and scroll the list. I've tested on both the > > > cupcake emulator as well as my gingerbread device with the same > > > results. > > > > Thanks in advance to anyone who takes the time to respond! > > > > ==Layouts== > > > main.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="fill_parent" > > > android:orientation="vertical" > > > > <TextView > > > android:layout_width="fill_parent" > > > android:layout_height="wrap_content" > > > android:text="Reuse Views Demo" /> > > > <GridView > > > android:id="@+id/gridview" > > > android:layout_width="fill_parent" > > > android:layout_height="fill_parent" > > > android:padding="5dp" > > > android:verticalSpacing="5dp" > > > android:horizontalSpacing="10dp" > > > android:numColumns="auto_fit" > > > android:columnWidth="60dp" > > > android:stretchMode="columnWidth" > > > android:gravity="center_horizontal"/> > > > </LinearLayout> > > > > grid_item.xml > > > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ > > > android" > > > android:id="@+id/GridItem" > > > android:layout_width="fill_parent" > > > android:layout_height="wrap_content" > > > android:orientation="vertical" > > > android:gravity="center_horizontal"> > > > <TextView android:id="@+id/grid_item_text" > > > android:layout_width="wrap_content" > > > android:layout_height="wrap_content" > > > android:gravity="center_horizontal" > > > android:layout_marginBottom="4dip"/> > > > </LinearLayout> > > > > ==source== > > > Main.java > > > package com.authorwjf.reuseviews; > > > > import java.util.ArrayList; > > > > import android.app.Activity; > > > import android.os.Bundle; > > > import android.widget.GridView; > > > > public class Main extends Activity { > > > > private CustomAdapter mAdapter; > > > private ArrayList<String> mItems = new ArrayList<String>(); > > > > @Override > > > public void onCreate(Bundle savedInstanceState) { > > > super.onCreate(savedInstanceState); > > > setContentView(R.layout.main); > > > for (int i = 0; i<200; i++) { > > > mItems.add(Integer.toString(i)); > > > } > > > mAdapter = new CustomAdapter(this, mItems); > > > GridView g = (GridView) findViewById(R.id.gridview); > > > g.setAdapter(mAdapter); > > > } > > > > } > > > > CustomAdapter.java > > > package com.authorwjf.reuseviews; > > > import java.util.ArrayList; > > > import com.authorwjf.reuseviews.R; > > > import android.content.Context; > > > import android.view.LayoutInflater; > > > import android.view.View; > > > import android.view.ViewGroup; > > > import android.widget.BaseAdapter; > > > import android.widget.TextView; > > > > public class CustomAdapter extends BaseAdapter { > > > > private Context mContext; > > > private ArrayList<String> mItems; > > > > public CustomAdapter(Context c, ArrayList<String> items) { > > > mContext = c; > > > mItems = items; > > > } > > > > @Override > > > public int getCount() { > > > return mItems.size(); > > > } > > > > @Override > > > public Object getItem(int position) { > > > return mItems.get(position); > > > } > > > > @Override > > > public long getItemId(int position) { > > > return position; > > > } > > > > @Override > > > public View getView(int position, View convertView, ViewGroup > > parent) > > > { > > > View v = convertView; > > > if (v == null) { > > > LayoutInflater li = (LayoutInflater) > > > mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); > > > v = li.inflate(R.layout.grid_item, null); > > > TextView tv = > > (TextView)v.findViewById(R.id.grid_item_text); > > > tv.setText("Item #"+mItems.get(position)); > > > } > > > return v; > > > } > > > > } > > > -- > > You received this message because you are subscribed to the Google > > Groups "Android Developers" group. > > To post to this group, send email to [email protected] > > To unsubscribe from this group, send email to > > [email protected] > > For more options, visit this group at > >http://groups.google.com/group/android-developers?hl=en > > -- > YuviDroid > Check out Launch-X <http://android.yuvalsharon.net/launchx.php> (a widget > to quickly access your favorite apps and > contacts!)http://android.yuvalsharon.net -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

