AsyncTask 更改 BaseAdapter (ListAdapter) 中的 ViewGroup 属性

AsyncTask change ViewGroup properties in BaseAdapter (ListAdapter)

在 AsyncTask 中 运行 更新 ViewGroup 中某些元素的属性的最佳方法是什么。例如:更新列表视图的 TextView 项目中文件的下载进度。我提供的代码只剩下最相关的部分。 FetchDataParams class 只提供了一个基本的蓝图,对于这个问题没有什么有趣的。

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

问题来了。如何通过 AsyncTask 调用 setFileSize、setProgressBar 和 SetTextProgress?

AsyncTask 代码

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

您应该将 view's object 发送到 AsyncTask 以在 onPostExecute 上更新它。 喜欢:

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

其他方法:

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