0

In my database I am storing data on various tourist attractions. I'm also storing the name of an image for each attraction, e.g. caste.jpg. The following method adds all written data to the text fields but I don't know how to add the image to the ImageView. Any help?

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

    }
Kelvin Schoofs
  • 8,323
  • 1
  • 12
  • 31
mmrrtt
  • 11
  • 1
  • 4

2 Answers2

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)
Axifive
  • 1,159
  • 2
  • 19
  • 31
Raghul
  • 46
  • 7
0

You could refer to the image with a path (local or remote)

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

And then, instead of using ther built in adapter, make a customer adapter and load the image from the path in the adapter's getView() method. For example, the below code is a custom adapter I use in one of my apps:

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;

    }
}
Martin
  • 4,711
  • 4
  • 29
  • 37