Search code examples
androidhashtablesimpleadapter

how to get bitmap image from hashtable into simple adapter to view in imageview for listactivity


Below is my code. I am getting url for an image through JSONArray. Then i converted the image to Bitmap image and stored in Hashtable.

I am using ImageView to show the images in listview. So i am using simple adapter for getting images. But image is not loading.

Please help me out of this, I am new to Android!!!!!!

ArrayList<Hashtable<String, Bitmap>> mylist1 = new ArrayList<Hashtable<String, Bitmap>>();

try{                
    JSONArray  earthquakes = json.getJSONArray("earthquakes");              
    for(int i=0;i<10;i++){                      
        imagemap = new Hashtable<String, Bitmap>();
        JSONObject e = earthquakes.getJSONObject(i);
        try
        {
            aURL = new URL(photo);
        }
        catch (MalformedURLException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        URLConnection conn = null;
        try
        {
            conn = aURL.openConnection();
        }
        catch (IOException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try
        {
            conn.connect();
        }
        catch (IOException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        InputStream is = null;
        try
        {
            is = conn.getInputStream();
        }
        catch (IOException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        BufferedInputStream bis = new

        BufferedInputStream(is,8*1024);

        Bitmap bm = BitmapFactory.decodeStream(bis);

        imagemap.put("im",bm);

        mylist1.add(imagemap);
        ListAdapter adapter = new SimpleAdapter(this,mylist1,R.layout.main,new String[] {"im"},new int[] {R.id.image});

        setListAdapter(adapter);

Update:

Below is the ArrayAdapter i used to load the string to text fields in the listview. I have one ImageView in it. How to override the listadapter to load the dynamic images into the listview.

ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main4, 
                       new String[] { "percent","propertyid",  "cityname", "statecode", "propertytype", "footage", "bathroom", "bedroom", "price", "estimated" }, 
                        new int[] { R.id.percent, R.id.property_id,  R.id.city_name, R.id.state_code, R.id.prop_type, R.id.foot, R.id.bath, R.id.bed, R.id.list, R.id.estimat});

setListAdapter(adapter);

Solution

  • This is how I put an image inside a listview

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder holder;
        if(convertView==null){
            vi = inflater.inflate(R.layout.highscore_item, null);
            holder=new ViewHolder();
            holder.Icon=(ImageView)vi.findViewById(R.id.icon);
            vi.setTag(holder);
        }
        else
            holder=(ViewHolder)vi.getTag();
        holder.Icon.setTag(mData[position].getIcon());
        imageLoader.DisplayImage(mData[position].getIcon(), activity, holder.Icon);
        return vi;
    }
    

    Take a look at Lazylist if you want more info.