2015-11-20 24 views
0

我正在從服務器獲取數據的應用程序,我有一個列表查看包含標題視圖我正在下載圖像,並在其完成我想更新我的標題視圖,我怎麼能做到這一點?更新標題列表查看圖像下載完成

我也更新我的項目,這些項目成功更新,但我不知道如何在適配器我的頭查看和更新​​它...

+0

請將您的代碼發佈到目前爲止... –

回答

1

保持參照列表中使用的文本視圖查看標題。

我假設你添加列表視圖頭象下面這樣:

View header = getLayoutInflater().inflate(R.layout.list_header, null);  
TextView headerText = (TextView) header.findViewById(R.id.my_textview); 
headerText.setText("This is my header!"); 
myListView.addHeaderView(header); 

一旦圖像下載完成後,可以通知使用一些接口/接收器,然後更新文本視圖。

1
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { 
    protected Long doInBackground(URL... urls) { 
    int count = urls.length; 
    long totalSize = 0; 
    for (int i = 0; i < count; i++) { 
     totalSize += Downloader.downloadFile(urls[i]); 
     publishProgress((int) ((i/(float) count) * 100)); 
     // Escape early if cancel() is called 
     if (isCancelled()) break; 
    } 
    return totalSize; 
    } 

protected void onProgressUpdate(Integer... progress) { 
    setProgressPercent(progress[0]); 
    } 

protected void onPostExecute(Long result) { 
    ListView listView = (ListView) findViewById(R.id.list); 
    View header = getLayoutInflater().inflate(R.layout.header, null); 
    listView.addHeaderView(header); 
    } 
} 

我希望這可以幫助你:)

1

我剛剛得到一個解決方案,我在這裏張貼所以它可能是有用的,每一個

我只是單純地發送headerView的參考在列表適配器的構造函數中,並在那裏下載圖像,並將圖像設置爲其零索引項,即標題視圖

public class MainListViewAdapter extends ArrayAdapter<DataObjects>{ 

    private int layoutResourceID; 
    private ArrayList<DataObjects> dataObjects; 
    private Context context; 
    private ListHolder listHolder; 
    private View headerView; 
    private Bitmap bitmap; 
    private Boolean flagBitmap = false; 

    public MainListViewAdapter(Context context, int layoutResourceID, ArrayList<DataObjects> objects, View headerView) { 
     super(context, layoutResourceID, objects); 
     this.layoutResourceID = layoutResourceID; 
     this.dataObjects = objects; 
     this.context = context; 
     this.headerView = headerView; 

     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 
      new DownloadImageTask(this.dataObjects.get(0).getImageURL()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null); 
     else 
      new DownloadImageTask(this.dataObjects.get(0).getImageURL()).execute(); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 
     listHolder = new ListHolder(); 
     if(row == null) { 
      LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
      row = inflater.inflate(layoutResourceID, parent, false); 

      row.setTag(listHolder); 

     } else { 
      listHolder = (ListHolder) row.getTag(); 
     } 

     DataObjects list = dataObjects.get(position); 

     if(position == 0) { 
      ImageView imageViewHeader = (ImageView) headerView.findViewById(R.id.imageViewHeaderImage); 

      if(flagBitmap) 
       imageViewHeader.setImageBitmap(bitmap); 
      else 
       imageViewHeader.setImageResource(R.drawable.sample); 
     } 


     return row; 
    } 

    private class ListHolder { 
     TextView textViewDate; 
     ImageView imageViewMagazine; 
     Button buttonPreview, buttonDownload; 
    } 

    private class DownloadImageTask extends AsyncTask<Void, Void, Bitmap> { 

     private String URL; 

     public DownloadImageTask(String URL) { 
      this.URL = URL; 
     } 

     protected Bitmap doInBackground(Void... urls) { 
      Bitmap mIcon11 = null; 
      try { 
       InputStream in = new java.net.URL(URL).openStream(); 
       mIcon11 = BitmapFactory.decodeStream(in); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return mIcon11; 
     } 

     protected void onPostExecute(Bitmap result) { 
      if(result != null) { 
       setThumbBitmap(result); 
       setThumbAvailable(true); 
      } 
     } 

     public void setThumbBitmap(Bitmap thumbBitmap) { 
      bitmap = thumbBitmap; 
     } 

     public void setThumbAvailable(boolean thumbAvailable) { 
      flagBitmap = thumbAvailable; 
     } 
    } 
}