Android GridView - How to change a bitmap dynamically?

Posted by Alborz on Stack Overflow See other posts from Stack Overflow or by Alborz
Published on 2011-01-15T11:25:54Z Indexed on 2011/01/15 13:54 UTC
Read the original article Hit count: 281

Filed under:
|
|
|

Hello I have a gridView which I use to show some pictures on (small thumb of diffrent levels). When the user finishes one level, I would like to change the thumb for that level. (Somehow show that it has been completed).

I created two thumbs for each level. One is the original and one that shows that the level is completed.

But how can i change the source of the images?

The code which I use to draw the images looks like this.

The main activity:

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.maps);

  GridView gridview = (GridView) findViewById(R.id.gridview);
  gridview.setAdapter(new ImageAdapter(this));

  gridview.setOnItemClickListener(new OnItemClickListener() {
    @Override
      public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        //Open the map which was clicked on, if there is one
        if(position+1 > 1){
            Toast.makeText(maps.this, "Level " + (position+1) + " is not yet available!", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(maps.this, "Opening Level " + (position+1), Toast.LENGTH_SHORT).show();
            Intent myIntent = new Intent(v.getContext(), Tutorial2D.class);
            startActivity(myIntent);
        }
      }
  });
}

The ImageAdapter Class:

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    //Changing
    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}



// references to our images
private Integer[] mThumbIds = {
        R.drawable.map1, R.drawable.map2, R.drawable.map3,
        R.drawable.map4, R.drawable.map5, R.drawable.map6,
        R.drawable.map7, R.drawable.map8, R.drawable.map9,
        R.drawable.map10, R.drawable.map11, R.drawable.map12,
        R.drawable.map13, R.drawable.map14, R.drawable.map15,
        R.drawable.map16, R.drawable.map17, R.drawable.map18,
        R.drawable.map19
};

}

© Stack Overflow or respective owner

Related posts about android

Related posts about gridview