ListViewのプログレスバーを更新するにはどうすればよいですか。各プログレスバーがファイルのダウンロードに関連付けられている場合、これはAsyncTaskを介して実行されます。
In the View Inflation process of ListView. (This is also not working)
基本的に、ブラウザで.mp4のようなファイルを取得するたびに、これをasynctaskと呼ぶので、asynctaskのインスタンスがn個存在する可能性があります。しかし、特定のプログレスバーを特定のAysnctaskタスクで更新する方法。以下の私のコード>>>>
package com.example.testhopelist;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
public class TestHopeList extends Activity {
ListView lv;
// ArrayList<String> list = new ArrayList<String>();
ArrayList<Url_Dto> list = new ArrayList<Url_Dto>();
MyListAdapter adtf;
public static String prf_date_view = "";
String str_start;
Button all_detail;
Button accepted_all;
Button not_shown;
public static SQLiteDatabase db;
String name;
File download;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_hope_list);
list = DBAdapter.getUrl_Detail();
Log.v("log_tag","list :: "+list.size());
lv = (ListView) findViewById(R.id.main_list_meet);
adtf = new MyListAdapter(this);
lv.setAdapter(adtf);
SqliteAdpter dba = SqliteAdpter.getAdapterInstance(getApplicationContext());
dba.createdatabase();
db = dba.openDataBase();
}
public class MyListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public MyListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return list.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = mInflater
.inflate(R.layout.custome_list_view, null);
Button cl = (Button) convertView.findViewById(R.id.cancle_sedual);
final ProgressBar pr=(ProgressBar)convertView.findViewById(R.id.listprogressbar);
//pr.setProgress(getItem(position));
cl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
str_start=list.get(position).url_video;
Log.v("log_tag","str_start "+ str_start);
//
//new DownloadFileFromURL().execute(str_start);
new DownloadFileFromURL().execute(pr,str_start);
}
});
return convertView;
}
}
class DownloadFileFromURL extends AsyncTask<Object, String, String> {
int count = 0;
ProgressDialog dialog;
ProgressBar progressBar;
int myProgress;
/**
* Before starting background thread Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
ProgressBar progressBar;
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(Object... params) {
int count;
progressBar= (ProgressBar) params[0];
try {
//URL url = new URL(f_url[0]);
URL url = new URL((String)params[1]);
Log.v("log_tag","name ::: "+url);
name = ((String) params[1]).substring(((String) params[1]).lastIndexOf("/") + 1);
Log.v("log_tag","name Substring ::: "+name);
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);
download = new File(Environment
.getExternalStorageDirectory()
+ "/download/");
if (!download.exists()) {
download.mkdir();
}
String strDownloaDuRL = download+"/" + name;
Log.v("log_tag"," down url " + strDownloaDuRL);
FileOutputStream output = new FileOutputStream(strDownloaDuRL);
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... values) {
progressBar.setProgress(0);
super.onProgressUpdate(values);
Log.v("log_tag","progress :: "+values);
// setting progress percentage
// pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
Log.v("log", "login ::: 4::: "
+ download);
String videoPath =download +"/"+ name;
String chpName = name;
Log.v("log_tag","chpName ::::"+ chpName + " videoPath " +videoPath);
db.execSQL("insert into videoStatus (chapterNo,videoPath) values(\"" + chpName + "\",\"" + videoPath + "\" )");
}
}
}