gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:02:55 25 4
gpt4 key购买 nike

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

ViewHolder 类:

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

BaseAdapter/ListAdapter类:

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 的对象发送到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);
}

关于java - AsyncTask 更改 BaseAdapter (ListAdapter) 中的 ViewGroup 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41532472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com