2

I want to show Multiple Progress Bar Like in Whats App while Uploading or downloading Images or Videos. And the progress which is completed to bi hide progress bar.

I tried to do this Using Async Task But not getting the Exact Solution And Also used to Do by Delegates. not getting an Exact Solution.

Here is my code

Main Activity

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();
    }
}

Here is My Adapter

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);
        }
    }
}

And here are the progresses

enter image description here

let me know with the valuable solution or is there any Library Beneficial for it

Sandeep Singh
  • 1,099
  • 2
  • 10
  • 28
Rishi Paul
  • 936
  • 2
  • 8
  • 25
  • This is working, But this will call async task multiple times for each view. For example if list has has 4 elements it will called 4 times for 4th element. – Sandeep Singh Jun 03 '15 at 11:43

1 Answers1

1

I'n not really sure what you mean by "Exact Solution", but viewing your code I see you're trying to set the Progress from your AsyncTask. However, you can only update Views from the UI thread, not directly from within an AsyncTask, which is a separate thread.

However, Asynctask provides a method publishProcess() you can use to perform such actions on the UI thread (like calling a function that updates your progress bar) while your doInBackground is working in a separate thread. Check this example.

You might need an Interface to call a function in your main thread from your AsyncTask. Hope this helps!

Frank D.
  • 1,286
  • 1
  • 12
  • 23