I'm have a custom GridView Array Adapter.  The problem for me is that
when the grid list gets large enough to scroll off the screen the
gridview and arraylist get out of sync.  For instance in my case I
have code that checks if an actor is of type director the text color
should be red.  if you scroll up and down my list enough times all the
actors text in my gridview will become red.  The thing is that the
Gridview appearance actually looks fine its the backing data.  Here is
the complete class.  In the code you can see where I tried to override
getItem and getItemId but that didn't fix anything.


=============================================================================
package com.goldendoodlegames.android.hollywoodempire;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import android.app.*;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.*;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ActorsView extends Activity {

        private static ArrayList<Actor> ActorList;
        ActorRowAdapter ard;
        GridView g1;

        //
        private static class ActorRowAdapter extends ArrayAdapter<Actor> {
                private LayoutInflater mInflater;
                private ArrayList<Actor> items;

                public ActorRowAdapter(Context context, int resource,
                                int textViewResourceId, ArrayList<Actor> items) 
{
                        super(context, resource, textViewResourceId, items);
                        this.items = items;
                        mInflater = LayoutInflater.from(context);
                }

                // public int getCount() {
                // return ActorList.size();
                // }
                //
                // public Actor getItem(int position) {
                // return ActorList.get(position);
                // }
                //
                // public long getItemId(int position) {
                // return position;
                // }
                @Override
                public View getView(int position, View convertView, ViewGroup
parent) {
                        super.getView(position, convertView, parent);
                        View v = convertView;
                        if (v == null) {
                                v = mInflater.inflate(R.layout.actorgridrow, 
null);
                        }
                        //Actor a = items.get(position);
                         GridView gv = (GridView) parent;
                         Actor a = (Actor) gv.getAdapter().getItem(position);
                        if (a != null) {
                                TextView Actor = (TextView) 
v.findViewById(R.id.TextViewActor);
                                CheckBox SelectActor = (CheckBox) v
                                                
.findViewById(R.id.CheckBoxActor);
                                if (Actor != null) {
                                        
Actor.setText(a.ActorImageView.getText());
                                        if 
(a.Type.toLowerCase().equals("director"))
                                                Actor.setTextColor(Color.RED);
                                }
                                if (SelectActor != null) {
                                        SelectActor.setChecked(a.Visibility);
                                        Drawable[] actDraw = a.ActorImageView
                                                        .getCompoundDrawables();
                                        
SelectActor.setBackgroundDrawable(actDraw[1]);
                                }
                        }
                        return v;
                }
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                try {
                        setContentView(R.layout.actorslayout);
                } catch (Exception ex) {
                        ex.printStackTrace();
                }
                ActorList = (ArrayList<Actor>)
HollyWoodEmpire.extGM.CurrentPlayer.ActorInventory;

                Button btnSelectAllActors = (Button)
findViewById(R.id.ButtonSelectAllActors);
                btnSelectAllActors.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                                int lc = g1.getChildCount();

                                for (int i = 0; i < lc; i++) {
                                        View gvw = (View) g1.getChildAt(i);
                                        // View vw = g1.getAdapter().getView(i, 
null, null);
                                        CheckBox SelectActor = (CheckBox) gvw
                                                        
.findViewById(R.id.CheckBoxActor);
                                        SelectActor.setChecked(true);

                                }

                        }
                });

                Button btnClearAllActors = (Button)
findViewById(R.id.ButtonClearAllActors);
                btnClearAllActors.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                                int lc = g1.getChildCount();

                                for (int i = 0; i < lc; i++) {
                                        View gvw = (View) g1.getChildAt(i);
                                        // View vw = g1.getAdapter().getView(i, 
null, null);
                                        CheckBox SelectActor = (CheckBox) gvw
                                                        
.findViewById(R.id.CheckBoxActor);
                                        SelectActor.setChecked(false);

                                }

                        }
                });

                Button btnAddActors = (Button) 
findViewById(R.id.ButtonAddActors);
                btnAddActors.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {

                                int lc = g1.getChildCount();

                                for (int i = 0; i < lc; i++) {
                                        View gvw = (View) g1.getChildAt(i);
                                        // View vw = g1.getAdapter().getView(i, 
null, null);
                                        CheckBox SelectActor = (CheckBox) gvw
                                                        
.findViewById(R.id.CheckBoxActor);

                                        ActorList.get(i).Visibility = 
SelectActor.isChecked();

                                }

                                Uri data = Uri.parse("content://actorlist/");
                                Intent result = new Intent(null, data);
                                getParent().setResult(RESULT_OK, result);
                                finish();
                        }
                });

                EditText edtSort = (EditText) findViewById(R.id.EditTextSearch);
                edtSort.addTextChangedListener(filterTextWatcher);
                g1 = (GridView) findViewById(R.id.GridViewActors);
                g1.setTextFilterEnabled(true);

                ard = new ActorRowAdapter(this, R.layout.actorgridrow,
                                R.id.TextViewActor, ActorList);

                g1.setAdapter(ard);

        }

        private TextWatcher filterTextWatcher = new TextWatcher() {

                public void afterTextChanged(Editable s) {
                }

                public void beforeTextChanged(CharSequence s, int start, int 
count,
                                int after) {
                }

                public void onTextChanged(CharSequence s, int start, int before,
                                int count) {
                        ard.getFilter().filter(s);
                }

        };

}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to