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

Reply via email to