0

I am trying to develop a spinner that can change the image of ImageView according to the user selection. I have successfully developed the code like following But I would like to ask if it is possible to develop it in a manner that can easier for the purpose of management

My idea is to develop it into the format like that:

HK_map,R.drawable.map_101
UK_map,R.drawable.map_102
US_map,R.drawable.map_103

Shall I use hashmap or arraylist? May you help me some advice on how to improve it?Many Thanks in advance

My current Code[Updated]

package tool.mobile;

import java.util.ArrayList;
import java.util.List;   
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;

public class SpinnerActivity extends Activity implements OnItemSelectedListener{

private ImageView view2;
private Spinner spinner2;
private ArrayAdapter adapter2;
private List<HashMap<String, String>> items;
private Bitmap snoop;


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bible_help_1);

spinner2 = (Spinner) findViewById(R.id.spinner1);
view2 = (ImageView) findViewById(R.id.imageView1);

items = fillMaps();

SimpleAdapter adapter=new SimpleAdapter(this,items,R.layout.bible_help_spinner,
                new String[]{"name"},
                new int[]{R.id.title});


spinner2.setAdapter(adapter);

spinner2.setOnItemSelectedListener(this);

spinner2.setVisibility(View.VISIBLE);

}

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {

HashMap map = (HashMap)items.get(arg2);
String Drawing_1= map.get("Drawing").toString();
int resID = getResources().getIdentifier(Drawing_1, "raw", "tool.mobile"); 
Bitmap snoop= BitmapFactory.decodeStream(getResources().openRawResource(resID));
imageshow.setImageBitmap(snoop);
imageshow.setTag(resID);
imageshow.setTag(resID);

}

public void onNothingSelected(AdapterView<?> arg0) {  
}

private List<HashMap<String, String>> fillMaps()
    {
        List<HashMap<String, String>> items = new ArrayList<HashMap<String,String>>();

        HashMap<String,String> i = new HashMap<String,String>();
            i.put("name","HK_map");
            i.put("Drawing", "map_101");
            items.add(i);

            i = new HashMap<String,String>();
            i.put("name","US_map");
            i.put("Drawing", "map_102");
            items.add(i);

return items;}



}

Current Problem

Currently, I am facing the problem of out-of-memory after viewing several images, may you give me some advice on how to solve the problem?

Eric
  • 357
  • 1
  • 4
  • 14

1 Answers1

1

I think you should go with HashMap because it gives you key-value pair feature to accommodate both map name and its image resource value

waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • see the sample code in question from [this](http://stackoverflow.com/questions/3949041/android-arrayadapter-listview-slowness) link – waqaslam May 24 '12 at 07:35
  • I have tried to change the code and the code work successfully, thx for help. Can you have a look on my code and see if there is any way to improve it? Also when should I call .recycle() to save the memory? – Eric May 25 '12 at 03:43
  • your code is good enough... where do you want to call `.recycle()` because i think its called on `TypedArray` when you are using custom attributes, but in your case you don't. So i guess its all fine :) – waqaslam May 25 '12 at 05:56
  • When i try to add more and more maps into spinner and open them into imageview via spinner selection, the program eventually faces the problem of OUT OF MEMORY. Can you give me some advice on how to solve the problem? – Eric May 25 '12 at 07:25
  • how many items are you talking about? i think it could be something with the images (initialization or size) which might be causing this. For a quick check, dont involve images and then set the same quantity to see it crashes – waqaslam May 25 '12 at 08:04
  • around 20 items or more... Since only one item image will be shown at one time, I am wonder if it is possible to release the memory used by the old image after changing the image being showed via spinner so that the only image using the memory is the current image – Eric May 25 '12 at 09:21
  • it depends how you are dealing with `snoop` and `imageshow` objects. Try declaring `snoop` out of **onItemSelected** as a *global instance* – waqaslam May 25 '12 at 09:34
  • After declaring snoop in the first place private Bitmap snoop; and call snoop.recycle(); right after imageshow.setTag(resID); i get this error java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@408aedb0 – Eric May 25 '12 at 10:22
  • can you put this stuff in a project and send me the project? – waqaslam May 25 '12 at 11:09
  • I solve the problem by adding following code before calling the new image imageshow.setImageBitmap(null); snoop=null; System.gc(); – Eric May 26 '12 at 03:26