我想在上传或下载图片或视频时在Whats应用中显示多个进度条。并且完成了进度隐藏进度条的进度。
我试图这样做使用异步任务但没有得到精确的解决方案,也被代表用来做。没有得到精确的解决方案。
这是我的代码
主要活动
public class MainActivity extends ActionBarActivity {
private ListView mListView;
private ArrayList<String> alst;
private int count = 0;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.listView1);
alst = new ArrayList<String>();
adapter = new MyAdapter(this, R.layout.activity_main, alst);
mListView.setAdapter(adapter);
}
public void onClick(View v) {
count++;
alst.add("" + count);
adapter.notifyDataSetChanged();
}
}
这是我的适配器
public class MyAdapter extends ArrayAdapter<String> {
private Context mContext;
public ViewHolder viewHolder;
public MyAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
mContext = context;
}
private class ViewHolder {
ProgressBar progressBar;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.adapter, parent, false);
viewHolder = new ViewHolder();
viewHolder.progressBar = (ProgressBar) convertView
.findViewById(R.id.progressBar1);
viewHolder.progressBar.getIndeterminateDrawable().setColorFilter(
mContext.getResources().getColor(R.color.actionBar),
android.graphics.PorterDuff.Mode.MULTIPLY);
viewHolder.progressBar.setVisibility(View.VISIBLE);
convertView.setTag(viewHolder);
}
viewHolder = (ViewHolder) convertView.getTag();
TextView textView = (TextView) convertView.findViewById(R.id.textView1);
textView.setText(getItem(position));
new DownloadAsyncTask().execute(viewHolder);
return convertView;
}
public class DownloadAsyncTask extends
AsyncTask<ViewHolder, Void, ViewHolder> {
@Override
protected ViewHolder doInBackground(ViewHolder... params) {
ViewHolder viewHolder = params[0];
for (int i = 0; i < 2500; i++) {
System.out.println(i);
}
return viewHolder;
}
@Override
protected void onPostExecute(ViewHolder result) {
result.progressBar.setVisibility(View.GONE);
}
}
}
以下是进展
请告诉我有价值的解决方案,或者是否有任何图书馆受益
答案 0 :(得分:1)
我不确定“精确解决方案”是什么意思,但查看代码我发现你正在尝试从AsyncTask设置进度。但是,您只能从UI线程更新视图,而不能直接从AsyncTask(一个单独的线程)中更新。
但是,当doInBackground在单独的线程中工作时,Asynctask提供了一个方法publishProcess(),您可以使用该方法在UI线程上执行此类操作(如调用更新进度条的函数)。查看this example.
您可能需要一个接口来从AsyncTask中调用主线程中的函数。希望这有帮助!