Search code examples
androidjsonandroid-fragmentshashmapsimpleadapter

how to load image and pass to Layout using JSON Data?


This is My code,I'm trying to pass the image to listview but i get error. how can i pass image ? should i download it into local memory first and then send it listview ??

  protected String doInBackground(String... args) {

    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    // getting JSON string from URL
    JSONObject json = jParser.makeHttpRequest(url_all, "GET", params);

    if(json == null)
        System.out.println("NULL");
    else{
    // Check your log cat for JSON reponse
    Log.d("All Developers: ", json.toString());

    try {
        // Checking for SUCCESS TAG
        int status = json.getInt(TAG_STATUS);

        if (status == 1) {
            // products found
            // Getting Array of Products
            developers = json.getJSONArray(TAG_all);

            // looping through All Products
            for (int i = 0; i < developers.length(); i++) {
                JSONObject c = developers.getJSONObject(i);

                // Storing each json item in variable
                String id    = c.getString(TAG_UID);
                String name  = c.getString(TAG_NAME);
                String image = c.getString(TAG_IMAGE_PATH);


                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_UID, id);
                map.put(TAG_NAME, name);
                //map.put(TAG_IMAGE_PATH, image);

                // adding HashList to ArrayList
                developersList.add(map);
                //Iamge Download
            }
        } else {
            System.out.println("No Data");
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    }

    return null;
}
protected void onPostExecute(String file_url) {
    // dismiss the dialog after getting all products
    pDialog.dismiss();
    // updating UI from Background Thread
    getActivity().runOnUiThread(new Runnable() {
        public void run() {
            /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(getActivity(),
                    developersList,R.layout.list_data, new String[] { TAG_UID,
                            TAG_NAME,TAG_IMAGE_PATH},
                    new int[] { R.id.uid, R.id.name,R.id.image });
            // updating listview
            lv.setAdapter(adapter);
        }
    });

 }
   }
 }

Errors

03-30 13:34:03.475: E/BitmapFactory(22326): Unable to decode stream: java.io.FileNotFoundException


Solution

  • You don't have to re-invent the wheel - assuming you have the url to the image you can use nostra13 library for image loading it'll do all the nasty staff for you (cache handling downloading\ saving thumbnail etc..) it also have an exmaple project you can look at to better unnderstand of how to use it.