2011-10-13 59 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是最好的。 –