AsyncTask change ViewGroup properties in BaseAdapter (ListAdapter)

54 views Asked by At

What is the best way to update properties of certain elements in a ViewGroup whilst running in an AsyncTask. E.g: update download progress of a file in a listview's TextView item. I've provided my code with only the most relevant parts remaining. The FetchDataParams class only provides a basic blueprint, nothing interesting for this problem.

ViewHolder class:

static class ViewHolder {
   ProgressBar progressBar;
   TextView progress;
   TextView size;
   ToggleButton toggleBtn;
   int position;
}

BaseAdapter/ListAdapter class:

public class RouteItemAdapter extends BaseAdapter implements ListAdapter {
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      view = inflater.inflate(R.layout.routeitem, null);
    }

    routeItem = (TextView)view.findViewById(R.id.routeItem);
    routeItem.setText(list.get(position).nameShort);

    toggleBtn = (ToggleButton)view.findViewById(R.id.toggleButton);

    ViewHolder holder = new ViewHolder();
    holder.progressBar = (ProgressBar)view.findViewById(R.id.progressBar);
    holder.progress = (TextView)view.findViewById(R.id.progress);
    holder.size = (TextView)view.findViewById(R.id.size);
    holder.toggleBtn = toggleBtn;
    view.setTag(R.id.routeItem, holder);

    toggleBtn.setTag(R.id.toggleButton, this.getItem(position));
    toggleBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//Download offline route data in AsyncTask
        if (isChecked) {
          RouteItem rItem = (RouteItem)buttonView.getTag(R.id.toggleButton);
          String routeSelected = rItem.name;
          FetchData FetchData = new FetchData();
          FetchData.execute(routeSelected);
        } else {

        }
      }
    });

    return view;
  }

  public void setFileSize(long fileSize) {
    String sizeOfFile = readableFileSize(fileSize);
    size.setText("Size: " + sizeOfFile);
  }

  public void setProgressBar(int a) {
    progressBar.setProgress(a);
  }

  public void setTextProgress(String a) {
    progress.setText(a);
  }

Here comes the issue. How do I call setFileSize, setProgressBar and SetTextProgress through an AsyncTask?

AsyncTask code

private class FetchData extends AsyncTask<String, FetchDataParams, String> {

    @Override
    protected String doInBackground(String... routeSelected) {
      //code removed here...

      int progress = (int)((i / (float)totalpoints) * 100);
      String progressString = progress + "/100%";
      publishProgress(new FetchDataParams(outputFile.length(), progress, progressString));

      //code removed here...
      return filePath;
    }
    //This is what I figured it should be doing, this does not work inside a listAdapter
    protected void onProgressUpdate(FetchDataParams... params) {
      setFileSize(params[0].fileLength);
      setProgressBar(params[0].progress);
      setTextProgress(params[0].progressString);
    }

    @Override
    protected void onPostExecute(String routeSelected) {
      super.onPostExecute(routeSelected);      
    }
  }
1

There are 1 answers

0
NehaK On BEST ANSWER

You should send your view's object to your AsyncTask to update it on onPostExecute. Like :

private class FetchData extends AsyncTask<String, FetchDataParams, String> {

    View parentView;

    public FetchData(View parentView){
      this.parentView = parentView;
    }

    @Override
    protected String doInBackground(String... routeSelected) {
      //code removed here...

      int progress = (int)((i / (float)totalpoints) * 100);
      String progressString = progress + "/100%";
      publishProgress(new FetchDataParams(outputFile.length(), progress, progressString));

      //code removed here...
      return filePath;
    }
    //This is what I figured it should be doing, this does not work inside a listAdapter
    protected void onProgressUpdate(FetchDataParams... params) {
      setFileSize(parentView, params[0].fileLength);
      setProgressBar(parentView, params[0].progress);
      setTextProgress(parentView, params[0].progressString);
    }

    @Override
    protected void onPostExecute(String routeSelected) {
      super.onPostExecute(routeSelected);      
    }
  }

Other Methods :

  public void setFileSize(View view, long fileSize) {
    String sizeOfFile = readableFileSize(fileSize);

    size = (TextView)view.findViewById(R.id.size);

    size.setText("Size: " + sizeOfFile);
  }

  public void setProgressBar(View view,int a) {
    holder.progressBar = (ProgressBar)view.findViewById(R.id.progressBar);
    progressBar.setProgress(a);
  }

  public void setTextProgress(View view,String a) {
    progress = (TextView)view.findViewById(R.id.progress);
    progress.setText(a);
  }