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

时间:2015-01-04 15:38:57

标签: java android listview android-listview android-asynctask

在我的MainActivity中,我有一个listview,每行有两行文本。应用程序启动时只写第一行,第二行必须根据AsyncTask类处理的内容编写。当我尝试这样做时,listview的第一项获取AsyncTask提供的所有值,因为我不明白如何更新列表视图的第二行每个项目,无需重写整个列表视图。 每个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类:

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

AsyncTask:

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

1 个答案:

答案 0 :(得分:0)

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

//方法3

只需更新此列表适配器的下划线数据对象 并致电adapter.notifyDataSetChanged();
这将再次致电getView 除非您正在显示来自互联网的图像

,否则在大多数情况下,此方法优于上述其他2种方法