2011-10-13 58 views
1

在我的自定义ListView中包含两个textview和一个Imageview我在读取和分配所有三个视图元素的同时需要很长时间。在这种情况下,我need 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不 知道如何做到这一点....在android中如何更新自定义列表视图的内容

在此先感谢..

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

      } 
+0

据我了解你必须实现图像懒加载http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview – vokilam

+0

是这是某种程度上与延迟加载有关,但在这里的任务是更新图像BitmaPDrawable从默认图标到图像资源通过互联网..如果您有任何想法,请建议我// –

+0

请提供一些代码 – vokilam

回答

2

我用这个LazyList取得了巨大成功:https://github.com/thest1/LazyList

为了您的需要,您可以将提供的存根图像替换为您想使用的图像。我还使用了1x1的空白PNG来显示没有图像。

此外,我在代码中做出的一个更改,我强烈建议使用此程序包时,将使用SD卡更改代码以使用内置高速缓存。您可以通过使用.getExternalStorageDirectory()将.FileCache.java文件修改为.getCacheDir()来完成此操作。

希望这会有所帮助。

+0

+1链接被用于这样的问题。 LazyList是最好的。 –