I see that in the onClick, you try to access the list's (GridView's)
items by accessing its child-views. This is not correct.
Child-views are re-used for when displaying items in the
listview(GridView). When a child-view (list-item view) scrolls off the
screen, it is re-used for ones that start scrolling into the visible
screen.
Also:
The call 'Actor a = (Actor) gv.getAdapter().getItem(position)' looks
incorrect.
'// Actor a = items.get(position);' should be un-commented. This is
the correct way of getting your Actor.
Add a field to Actor that check if it is selected:
public class Actor ... {
boolean isSelected;
...
}
In the onClick of your Select-All button, call your adapter, not your
GridView, to obtain all the Actors:
for (int i = 0; i < ard.getCount();; i++) {
Actor a = (Actor)arg.getItem(i);
a.isSelected = true;
...
}
ard.notifyDataSetChanged(); // this will call the adapter's
getView(...) to be called.
Then in your adapter, do this:
public int getCount() {
return items.size();
}
public Actor getItem(int position) {
return items.get(position);
}
public View getView(int position, View convertView, ViewGroup
parent) {
...
...
Actor a = getItem(position)
CheckBox cb = v.findViewById(R.id.CheckBoxActor);
cb.setChecked(a.isSelected);
TextView tv = v.findViewById(R.id.TextViewActor);
tv.setText(a.ActorImageView.getText());
... etc...
}
Never call your adapter's getView(....) method yourself. It will be
called by the framework. It's a call-*back* function.
If you want to change the items in the list-view, do *not* change the
list-item views (child-views) of the listview (GridView). Instead,
change the data in the adapter, then call the adapter's
'notifyDataSetChanged()' method.
On Oct 28, 10:35 am, stanchat <[email protected]> wrote:
> 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);
> }
>
> };
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -
--
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