[android-developers] Custom GridView with ArrayAdapter out of sync with backing data

2011-07-07 Thread ragupathi ragupathi
Hi every one,
 im developing one program for android gridview and displaying
images from getting images in sdcard and displayed. when i click one
image need to add one images in that gridview child. its working
fine.. bit my problem is when im scrolling down the un selected images
was highlighted. here im post my code..


package com.SdcardIma;


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * Loads images from SD card.
 *
 * @author Mihai Fonoage
 *
 */
public class SdcardIma extends Activity implements
OnItemClickListener {

/**
 * Grid view holding the images.
 */
private List pathlist=new ArrayList();

private List viewList=new ArrayList();
private List checkList=new ArrayList();
private int count =0;
private GridView sdcardImages;
  //  private boolean isVisble = false;
/**
 * Image adapter for the grid view.
 */
private ImageAdapter imageAdapter;
/**
 * Display used for getting the width of the screen.
 */
private Display display;

/**
 * Creates the content view, sets up the grid, the adapter, and
the click listener.
 *
 * @see android.app.Activity#onCreate(android.os.Bundle)
 */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Request progress bar
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);

display = ((WindowManager)
getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

setupViews();
setProgressBarIndeterminateVisibility(true);
loadImages();
}

/**
 * Free up bitmap related resources.
 */
protected void onDestroy() {
super.onDestroy();
final GridView grid = sdcardImages;
final int count = grid.getChildCount();
ImageView v = null;
for (int i = 0; i < count; i++) {
v = (ImageView) grid.getChildAt(i);
((BitmapDrawable) v.getDrawable()).setCallback(null);
}
}
/**
 * Setup the grid view.
 */
private void setupViews() {
sdcardImages = (GridView) findViewById(R.id.sdcard);
sdcardImages.setNumColumns(display.getWidth()/95);
sdcardImages.setClipToPadding(false);
sdcardImages.setOnItemClickListener(SdcardIma.this);
imageAdapter = new ImageAdapter(getApplicationContext());
sdcardImages.setAdapter(imageAdapter);
}
/**
 * Load images.
 */
private void loadImages() {
final Object data = getLastNonConfigurationInstance();
if (data == null) {
new LoadImagesFromSDCard().execute();
} else {
final LoadedImage[] photos = (LoadedImage[]) data;
if (photos.length == 0) {
new LoadImagesFromSDCard().execute();
}
for (LoadedImage photo : photos) {
addImage(photo);
}
}
}
/**
 * Add image(s) to the grid view adapter.
 *
 * @param value Array of LoadedImages references
 */
private void addImage(LoadedImage... value) {
for (LoadedImage image : value) {
imageAdapter.addPhoto(image);
imageAdapter.notifyDataSetChanged();
}
}

/**
 * Save bitmap images into a list and return that list.
 *
 * @see android.app.Activity#onRetainNonConfigurationInstance()
 */
@Override
public Object onRetainNonConfigurationInstance() {
final GridView grid = sdcardImages;
final int count = grid.getChildCount();
final LoadedImage[] list = new LoadedImage[count];

for (int i = 0; i < count; i++) {
final ImageView v = (ImageView) grid.getChildAt(i);
list[i] = new LoadedImage(((BitmapDrawable)
v.getDrawable()).getBitmap());
}

return list;
}
/**
 * Async task for loading the images from the SD card.
 *
 * @author Mihai Fonoage
 *
 */
class LoadImagesFromSDCard extends AsyncTask {

/**
 * Load images from SD Card in the background, and display
each image on the screen.
 *
 * @see android.os.AsyncTask#doInBackground(Params[])

[android-developers] Custom GridView with ArrayAdapter out of sync with backing data

2010-10-28 Thread stanchat
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 ActorList;
ActorRowAdapter ard;
GridView g1;

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

public ActorRowAdapter(Context context, int resource,
int textViewResourceId, ArrayList 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)
HollyWoodEmpire.extGM.CurrentPlayer.ActorInventory;

Button btnSelectAllActors = (Button)
findViewById(R.id.ButtonSelectAllActors);
btnSelectAllActors.setOnClickListener(