如何从数据库中更改android中imageview的背景图像

时间:2015-03-11 18:43:56

标签: android image imageview

在我的数据库中,我将数据存储在各个旅游景点。我还存储了每个景点的图像名称。 (示例caste.jpg)以下方法将所有写入的数据添加到texfields,但我不知道如何将图像添加到imageview标题imageView。有什么帮助吗?

public void updateDisplay()
    {
        // get the comments
        attractions = db.getAttractions();

        // create a List of Map<String, ?> objects
        ArrayList<HashMap<String, String>> data =
                new ArrayList<HashMap<String, String>>();
        for (Attraction attraction : attractions) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("name", attraction.getName());
            map.put("times", attraction.getOpeningTimes());
            map.put("descr", attraction.getDesc());
            map.put("price", attraction.getPrice());
            data.add(map);
        }

        // create the resource, from, and to variables
        int resource = R.layout.listview_attraction;

        String[] from = {"name", "times", "descr",  "price",  "web"};
        int[] to = {R.id.name, R.id.times, R.id.descr, R.id.price, R.id.web};

        // create and set the adapter
        SimpleAdapter adapter =
                new SimpleAdapter(this, data, resource, from, to);
        attractionListListView.setAdapter(adapter);

    }

2 个答案:

答案 0 :(得分:1)

    String name = "your_drawable";
int id = getResources().getIdentifier(name, "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(id);

// Then use this for setting the drawable in either case:



Imageview.setBackground(drawable) 

答案 1 :(得分:0)

您可以使用路径(本地或远程)引用图像

for (Attraction attraction : attractions) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("name", attraction.getName());
        map.put("times", attraction.getOpeningTimes());
        map.put("descr", attraction.getDesc());
        map.put("price", attraction.getPrice());
     -> map.put("imagePath", attraction.getPath());
        data.add(map);
    }

然后,不要使用内置适配器,而是使用客户适配器 并从适配器的getView()方法中的路径加载图像。例如,下面的代码是我在我的一个应用程序中使用的自定义适配器:

private class ViewHolder {

    TextView mItem;
    ImageView mStore;

}

public class MyDataAdapter extends SimpleCursorAdapter {
    private Cursor c;
    private Context context;
    int layout;

    public MyDataAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        this.c = c;
        this.layout = layout;
        this.context = context;
    }

    public View getView(final int pos, View inView, ViewGroup parent) {

        ViewHolder holder = null;

        if (inView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inView = inflater.inflate(layout, null);

            holder = new ViewHolder();
            holder.mItem = (TextView) inView.findViewById(R.id.item);
            holder.mStore = (ImageView) 
            inView.setTag(holder);
        } else {
            holder = (ViewHolder) inView.getTag();
        }

        final VisitProfile visit = VisitProvider.getProfile(c, pos);
        if (visit != null) {
            final int store = visit.getStore();

            RetailStoreProfile rsp = RetailStoreProvider.fetchStore(ShowEventDetail.this, store);
            if (rsp != null) {


                holder.mStore.setDrawable(rsp.getPath());





            } else {
                Log.d(TAG, "Bogus Store in adapter " + store);
            }
        } else {
            Log.d(TAG, "Bogus visit in adapter " + visit);
        }

        return inView;

    }
}