Note that there are some explanatory texts on larger screens.

plurals
  1. POCaching images and displaying
    primarykey
    data
    text
    <p>Hello Am facing a particular problem in which I need to download images and display them onto a <code>ListView</code> corresponding to their particular <code>TextView's</code>. The code I have is successfully displaying the The <code>TextView's</code> I need to display but I don't know how to display all these different images next to my text views in my <code>ListView</code>.</p> <p>After going through many threads in SO. The top answers are to solve this by 1. Lazy List 2. Universal Image Loader</p> <p>I have gone through both the solutions. I downloaded Lazy List codes in which the URL's are hardcoded strings stored in an Array. What I would like to do is create my own Strings dynamically. Store them onto cache and display all the corresponding images.</p> <p>Here is my code:</p> <pre><code>public class Tools_ListItemActivity extends ListActivity { private Context context; String s; private static final String TAG_POSTS = "posts"; private static final String TAG_MDNAME = "mdname"; private static final String TAG_UTCOST = "utcost"; private static final String TAG_IIMG= "iimg"; JSONArray posts = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); s=getIntent().getExtras().getString("url"); new ProgressTask(Tools_ListItemActivity.this).execute(); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); } ArrayList&lt;HashMap&lt;String, String&gt;&gt; jsonlist = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); ListView lv ; private class ProgressTask extends AsyncTask&lt;String, Void, Boolean&gt; { private ProgressDialog dialog; public ProgressTask(ListActivity activity) { Log.i("1", "Called"); context = activity; dialog = new ProgressDialog(context); } /** progress dialog to show user that the backup is processing. */ /** application context. */ private Context context; protected void onPreExecute() { this.dialog.setMessage("Progress start"); this.dialog.show(); } @Override protected void onPostExecute(final Boolean success) { if (dialog.isShowing()) { dialog.dismiss(); } ListAdapter adapter = new SimpleAdapter(context, jsonlist, R.layout.activity_toolsitem, new String[] { TAG_IIMG, TAG_MDNAME, TAG_UTCOST }, new int[] { R.id.imageViewUrl, R.id.mdname, R.id.utcost }); setListAdapter(adapter); // selecting single ListView item lv = getListView(); } protected Boolean doInBackground(final String... args) { JSONParser jParser = new JSONParser(); // getting JSON string from URL JSONObject json = jParser.getJSONFromUrl(s); try { posts = json.getJSONArray(TAG_POSTS); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { // looping through All Contacts for(int i = 0; i &lt; posts.length(); i++){ JSONObject c = posts.getJSONObject(i); // Storing each json item in variable String mdname = c.getString(TAG_MDNAME); String utcost= c.getString(TAG_UTCOST); String iimg=c.getString(TAG_IIMG); //Forming the Url of the image to be shown in the list view String imageUrl="My_App_URL"+iimg; /* try { String imageUrl="My_App_URL"+iimg; ImageView imageView = (ImageView)findViewById(R.id.imageViewUrl); Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent()); imageView.setImageBitmap(bitmap); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } */ // creating new HashMap HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); // adding each child node to HashMap key =&gt; value map.put(TAG_MDNAME, mdname); map.put(TAG_UTCOST, utcost); map.put(TAG_IIMG, iimg); jsonlist.add(map); } }catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } } } </code></pre> <p>Here i have debugged and found out that <code>imageUrl</code> gets formed correctly. <code>Json</code> only returns the name of the <code>jpg</code> so i append that to the URL and Store it in <code>imageUrl</code>. The other two textviews are getting properly parsed and displayed. If any one can help me out in displaying the images in the image view too then it would be great. Thanks. </p> <p><strong>Update:</strong> I was able to solve my particular problem with the help of the link shared by Pankaj(<a href="https://github.com/AndroidBegin/Android-JSON-Parse-Images-and-Texts-Tutorial">https://github.com/AndroidBegin/Android-JSON-Parse-Images-and-Texts-Tutorial</a>) and the concept knowledge shared by Raghunandan down below. Please go through these answers for a detailed explanation as well as implementation of Lazy loading. My apologies for not being able to reward an answer with bounty as i was away for two days.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload