0

我正在尝试开发一个可以根据用户选择更改 ImageView 图像的微调器。我已经成功开发了如下代码但是我想问一下是否可以以更容易管理的方式开发它

我的想法是将它开发成这样的格式:

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

我应该使用 hashmap 还是 arraylist?你能帮我一些关于如何改进它的建议吗?非常感谢提前

我当前的代码[更新]

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;}



}

当前问题

目前,我在查看几张图像后面临内存不足的问题,您能给我一些解决问题的建议吗?

4

1 回答 1

1

我认为您应该使用它,HashMap因为它为您提供了键值对功能来容纳地图名称及其图像资源

于 2012-05-23T08:08:07.650 回答