我有一个显示数据库记录列表的活动。 在此活动中有一个自定义ListView。 在自定义ListView中,有一个Button,一个TextView和一个ProgressBar。 我调用AsyncTask的按钮侦听器位于CustomAdapter类中。
DownloadTask downloadTask = new DownloadTask(getContext());
downloadTask.execute(url[position].toString());
AsynkTask工作得很好。 但我想在下载过程中更新进度条。 我在互联网上搜索了三天但没有成功。 对不起我的英文
public class listanimals extends ActionBarActivity {
private MyDatabase MyDataBase;
String[] listurl;
ProgressBar progressBar;
TextView url2;
CustomList adapter;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class DownloadTask extends AsyncTask<String, Integer, String> {
private Context context;
int myProgress;
public DownloadTask(Context context) {
this.context = context;
}
@Override
protected String doInBackground(String... sUrl) {
InputStream input = null;
final GlobalClass caches = (GlobalClass) context.getApplicationContext();
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL(sUrl[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
}
input = connection.getInputStream();
output = new FileOutputStream("/sdcard/"+caches.getName_cach()+".mp3");
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
myProgress = (int)(total*100/connection.getContentLength());
output.write(data, 0, count);
}
} catch (Exception e) {
return e.toString();
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
} catch (IOException ignored) {
}
if (connection != null)
connection.disconnect();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
//MY PROBLEM MAY HERE
progressBar.setProgress(myProgress);
}
@Override
protected void onPostExecute(String result) {
final GlobalClass caches = (GlobalClass) context.getApplicationContext();
if (result != null)
Toast.makeText(context,"Download error: "+result, Toast.LENGTH_LONG).show();
else{
Toast.makeText(context,"File downloaded"+ "" + caches.getName_cach(), Toast.LENGTH_SHORT).show();
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////AsynkTask end
///////////////////////////////////////////////////////////////////////////////////////
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listanimals);
MyDataBase = new MyDatabase(this);
final GlobalClass caches = (GlobalClass) getApplicationContext();
final ListView listView=(ListView)findViewById(R.id.listView);
SQLiteDatabase mydb = MyDataBase.getReadableDatabase();
Cursor cursor = mydb.rawQuery("select * from animals", null);
ArrayList<String> myList = new ArrayList<String>();
final ArrayList<String> myListname = new ArrayList<String>();
ArrayList<String> myListurl = new ArrayList<String>();
try {
while(cursor.moveToNext()) {
myList.add(cursor.getString(cursor.getColumnIndex("_id")));
myListname.add(cursor.getString(cursor.getColumnIndex("name")).trim());
myListurl.add(cursor.getString(cursor.getColumnIndex("url")));
String[] listname = new String[myListname.size()];
listname = myListname.toArray(listname);
listurl = new String[myListurl.size()];
listurl = myListurl.toArray(listurl);
String[] listid = new String[myList.size()];
listid = myList.toArray(listid);
adapter = new
CustomList(listanimals.this, listname,listurl);
listView.setAdapter(adapter);
}} finally{
mydb.close();
}}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class CustomList extends ArrayAdapter<String> {
private MyDatabase MyDataBase;
private final Activity context;
private final String[] web;
private final String[] url;
public CustomList(Activity context,
String[] web,String[] url) {
super(context, R.layout.list_file, web);
this.context = context;
this.web = web;
this.url = url;
}
@Override
public View getView(final int position, View view, final ViewGroup parent) {
final GlobalClass caches = (GlobalClass) context.getApplicationContext();
MyDataBase = new MyDatabase(getContext());
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_file, null, true);
Typeface kamran= Typeface.createFromAsset(context.getAssets(), "IranNastaliq.ttf");
progressBar=(ProgressBar)rowView.findViewById(R.id.progressBar);
TextView txtTitle3 = (TextView) rowView.findViewById(R.id.textView);
txtTitle3.setTypeface(kamran);
url2=(TextView)rowView.findViewById(R.id.textView2);
url2.setText(url[position]);
txtTitle3.setText(web[position]);
Button download=(Button)rowView.findViewById(R.id.button);
download.setTag(position);// Any data associated with the button has to be added with setTag()
download.setOnClickListener(new View.OnClickListener() {/////////call AsynkTask
@Override
public void onClick(View arg0) {
caches.setName_cach(web[position]);
DownloadTask downloadTask = new DownloadTask(getContext());
downloadTask.execute(url[position].toString());
}
});
return rowView;
}
}
}
答案 0 :(得分:1)
您需要从doInBackground方法调用publishProgress来更新ProgressBar:publishProgress docs
这将调用onProgressUpdate(Integer ... values);
您不需要myProgress实例变量;
答案 1 :(得分:0)
请查看此链接https://www.youtube.com/watch?v=5HDr9FdGIVg&list=PLonJJ3BVjZW6hmkEaYIvLLm5IEGM0kpwU&index=17
你需要调用 - publishProgress((int)total * 100 / connection.getContentLength());
在while循环中更新你的进度条。希望它有所帮助。
答案 2 :(得分:0)
您在列表的每个项目中都有一个进度条,而您在活动中保留了一个实例。这意味着您的进度视图将指向系统要求您绘制的最后一个列表项中的进度视图。这可能不是您按下按钮的列表项中的那个。我建议你拆分课程(将它们从活动中取出),你会更好地看到你的问题。但这只是你的第一个问题 - 因为你启动了异步任务,同时你可以在屏幕上滚动该项目。