如何从 AsyncTask 更改或设置列表视图的子项?

How to change or set subitems of a listview from AsyncTask?

在我的 MainActivity 中,我有一个列表视图,每行有两行文本。应用启动时只写第一行,第二行必须根据AsyncTask处理的东西写class。当我尝试这样做时,列表视图的第一项获取 AsyncTask 提供的所有值,因为我不明白如何更新 only 列表视图的第二行 each 项,无需重写整个列表视图。 每个 textView 在 xml 布局文件中都有自己的 ID。我怎样才能在 onPostExecute() 中做到这一点? 这是代码:

在 MainActivity onCreate() 中:

    final ListView listview = (ListView) findViewById(R.id.listview);

    final String[] values = new String[10];
    for(int i=0;i<10;i++){
        //do stuff to write the values[] elements

    }

    MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, values);
    listview.setAdapter(adapter);

MySimpleArrayAdapter class:

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
  private final Context context;
  private final String[] values;
  int position=0;

  public MySimpleArrayAdapter(Context context, String[] values) {
    super(context, R.layout.list_layout, values);
    this.context = context;
    this.values = values;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.list_layout, parent, false);
    TextView firstLine = (TextView) rowView.findViewById(R.id.label);
    TextView secondLine = (TextView) rowView.findViewById(R.id.sublabel);

    firstLine.setText(values[position]);
    secondLine.setText(""); //I prefer to write an empty string for the secondLine

    return rowView;
  }
}

异步任务:

private class Process extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        //do stuff
    }

    @Override
    protected void onPostExecute(String message) {
        TextView secondLine = (TextView) findViewById(R.id.sublabel);

        //This of course udpdates only the secondLine of the first item of listview
        secondLine.setText(processedValues);
    }
}

将 TextView 作为参数传递给 AsyncTask,如下所示

private class Process extends AsyncTask<Void, Void, String> {
private WeakReference<TextView> weakTextView;
public Process(TextView textView){
       weakTextView = new WeakReference<TextView>(textView);
}
@Override
protected String doInBackground(Void... params) {
    //do stuff
}

@Override
protected void onPostExecute(String message) {
    TextView tv = weakTextView.get();
   if(tv!=null){
       tv.setText(message);
 }
}

}

您在 getView 中将其称为

Process p = new Process(yourTextView);
p.execute();

//方法二

private class Process extends AsyncTask<TextView, Void, String> {

private WeakReference<TextView> weakTextView;

@Override
protected String doInBackground(TextView... params) {
    if(params == null||params.length=0){
         return null;
    }
    TextView tv = (TextView)params[0];
    weakTextView = new WeakReference<TextView>(tv);

    //do stuff
}

@Override
protected void onPostExecute(String message) {
    TextView tv = weakTextView.get();
   if(tv!=null){
       tv.setText(message);
 }
}

}

您在 getView 中将其称为

Process p = new Process();
p.execute(yourTextView);  

//方法三

只需更新此列表适配器的下划线数据对象 并致电 adapter.notifyDataSetChanged();
这将再次调用 getView
在大多数情况下,这种方法比上面的其他 2 种方法更可取,除非您显示的是来自互联网的图像