2017-01-22 2 views
-1

私は独自のベースアダプタを備えたカスタムリストビューを持っています。それは瞬間にタイトルとディスカッションを持っています。 idはURLから画像を追加するのが好きです。しかし、私はどのように追加された画像を追加するコードを調整するか分からない。既存のリストビューにURLイメージをダウンロードする方法

すでにリストビュー内の項目を押したときに非同期タスクを使用してファイルをダウンロードすると、このメソッドを再利用してイメージを追加できますか?

のみダウンロードするファイルのための私の非同期タスクが瞬間

class DownloadFileFromURL extends AsyncTask<String, String, String> { 

    /** 
    * Before starting background thread 
    * Show Progress Bar Dialog 
    * */ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     showDialog(progress_bar_type); 
    } 

    /** 
    * Downloading file in background thread 
    * */ 
    @Override 
    protected String doInBackground(String... f_url) { 
     int count; 
     try { 
      URL url = new URL(f_url[0]); 
      URLConnection conection = url.openConnection(); 
      conection.connect(); 
      // getting file length 
      int lenghtOfFile = conection.getContentLength(); 

      // input stream to read file - with 8k buffer 
      InputStream input = new BufferedInputStream(url.openStream(), 8192); 

      // Output stream to write file 
      OutputStream output = new FileOutputStream("/sdcard/Download/"+newnamestring); 

      byte data[] = new byte[1024]; 

      long total = 0; 

      while ((count = input.read(data)) != -1) { 
       total += count; 
       // publishing the progress.... 
       // After this onProgressUpdate will be called 
       publishProgress(""+(int)((total*100)/lenghtOfFile)); 

       // writing data to file 
       output.write(data, 0, count); 
      } 

      // flushing output 
      output.flush(); 

      // closing streams 
      output.close(); 
      input.close(); 

     } catch (Exception e) { 
      Log.e("Error: ", e.getMessage()); 
     } 

     return null; 
    } 

    /** 
    * Updating progress bar 
    * */ 
    protected void onProgressUpdate(String... progress) { 
     // setting progress percentage 
     pDialog.setProgress(Integer.parseInt(progress[0])); 
    } 

    /** 
    * After completing background task 
    * Dismiss the progress dialog 
    * **/ 
    @Override 
    protected void onPostExecute(String file_url) { 
     // dismiss the dialog after the file was downloaded 

     dismissDialog(progress_bar_type); 
     Intent intent = new Intent(Intent.ACTION_VIEW); 
     intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + newnamestring)), "application/vnd.android.package-archive"); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     startActivity(intent); 




    } 

} 

で私のカスタムベースアダプタ

class dataListAdapter extends BaseAdapter { 
    String[] Title, Detail; 


    dataListAdapter() { 
     Title = null; 
     Detail = null; 

     ; 

    } 

    public dataListAdapter(String[] text, String[] text1) { 
     Title = text; 
     Detail = text1; 





    } 

    public int getCount() { 
     // TODO Auto-generated method stub 
     return Title.length; 
    } 

    public Object getItem(int arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflater = getLayoutInflater(); 
     View row; 

     row = inflater.inflate(R.layout.custom, parent, false); 
     TextView title, detail; 
     title = (TextView) row.findViewById(R.id.titleapp); 
     detail = (TextView) row.findViewById(R.id.detail); 
     title.setText(Title[position]); 
     detail.setText(Detail[position]); 
     return (row); 
    } 


} 

答えて

1

あなたはできる最善のことをURLからイメージをダウンロードして、ImageViewのにそれを表示したい場合doはGlideライブラリを使用することです、このライブラリはあなたのimageViewにダウンロードして、別の時間を使用するためにそれをキャッシュすることができます そしてそれはあなたのプロジェクトに追加した後にそれを使う方法です。 Glide.with(mContext).load(imageUrl).into(yourImageView);

+0

#私はグライドライブラリにその歓声を忘れたかもしれないと思います –

+0

あなたの歓迎;) –

関連する問題