1

In my custom ListView contains two textview and one Imageview im using AsyncTask to read the Text from Internet same with the imageView .As im reading and assigning all three view elemnt at the same time it takes to much time . In such case ineed to Convert url to Bitmap in another AsyncTask when the text part is done. As a logic it recquire some concept of updating my ImageView resource .But i do not know how to do it....

Thanks In Advance..

private class AsynchTask extends AsyncTask<Void, Integer, Void> {
        URLConnection tc;
        BufferedReader in;
        URL twitter;
        int num=0;
                    @Override
            protected void onPreExecute() {
                super.onPreExecute();
            try {

               mProgressBar.setVisibility(View.VISIBLE);
                } catch (Exception e) {
                Log.e(TAG,""+e.getMessage());
            }
            }
            @Override
            protected Void doInBackground(Void... params) {
            try{
                    twitter = new URL("https://twitter.com/statuses/public_timeline.json");
                 tc = twitter.openConnection();
                 my = new ArrayList<HashMap<String,Object>>();
                 in = new BufferedReader(new InputStreamReader(
                 tc.getInputStream()));
                 ImageList=new ArrayList<String>();
            while ((line = in.readLine()) != null) {

                 JSONArray ja = new JSONArray(line);

                  for (int i = 0; i < ja.length(); i++) {
                      JSONObject jo = (JSONObject) ja.get(i);
                      /**Data Insert into the HashMap Object*/
                     hm=new HashMap<String, Object>();
                    hm.put(TEXT,jo.getString("text"));
                    hm.put(USER,jo.getJSONObject("user").getString("name"));
                  //                    String str=jo.getJSONObject("user").getString("profile_image_url"); hm.put(URL,"http://twitter.com/#!/"+jo.getJSONObject("user").getString("screen_name"));
             //                     hm.put(IMAGEURL,getDrawable_from_url(str));
                ImageList.add(jo.getJSONObject("user").getString("profile_image_url"));
                  Log.e(TAG,""+num);
                    my.add(hm);
                    num++;
                    Log.e("Count",""+num);
                    publishProgress(num);
                       }
                      num++;
                      publishProgress(num);
                  }
                 } catch (Exception e) {

                   Log.e(TAG,""+e.getMessage());
                  }
            return null;
            }

            @Override
                protected void onProgressUpdate(Integer... values) {
            mProgressBar.setProgress(values[0]);
            super.onProgressUpdate(values);
                }
        @Override
        protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        mProgressBar.setProgress(0);
        mProgressBar.setVisibility(View.GONE);

          adapter = new Simpleadapter(HelloWorldActivity.this, my, R.layout.listcontent, 
                    new String[]{TEXT,USER}, new int[]{R.id.text2,R.id.text1});
             listView.setAdapter(adapter);
             new AsynchTaskForImageLoading().execute();
            }
         }


             /**Method to convert Url to the Bitmap*/


              private Bitmap getDrawable_from_url(String url) {


               try{
             Bitmap x;
           HttpURLConnection connection = (HttpURLConnection)new                URL(url).openConnection();
               connection.setRequestProperty("User-agent","Mozilla/4.0");
          connection.connect();
               InputStream input = connection.getInputStream();
              x = BitmapFactory.decodeStream(input);

             return x;
                }
            catch (Exception e) {
                Log.e(TAG,""+e.getMessage());
                return null;
            }   

            }
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
gauravD
  • 89
  • 11
  • as far as i understood you have to implement image lazy loading http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview – vokilam Oct 13 '11 at 09:47
  • yes this is somehow related with Lazy loading but here the task is to update image BitmaPDrawable from default icon to Images resource comming through internet..url if you have any idea please suggest me// – gauravD Oct 13 '11 at 11:23
  • please provide some code – vokilam Oct 13 '11 at 11:56

1 Answers1

2

I've used this LazyList with great success: https://github.com/thest1/LazyList

For your needs, you can swap out the supplied stub image with the one you'd like to use. I've also used a 1x1 blank png to show no image.

Also, one change that i've made in my code and that I strongly suggest when using this package is to change the code from using the SD card to use the built in cache. You do this by modifying the FileCache.java file from using .getExternalStorageDirectory() to .getCacheDir().

Hope this helps.

SBerg413
  • 14,515
  • 6
  • 62
  • 88