【问题标题】:How could I change ListView in the AsyncTask?如何更改 AsyncTask 中的 ListView?
【发布时间】:2012-10-06 09:05:31
【问题描述】:

我有一些从服务执行的 AsyncTask 类:

public class DownloadTask extends AsyncTask<Void, Integer, Boolean {

    @Override
    protected Boolean doInBackground(Void... arg0) {
        // do something
        publishProgress(i);
    }

    @Override
    protected void onProgressUpdate(Integer ... progress) {
        if(textView()!=null) getTextView().setText(i + "%");
    }

    public void setTextView(TextView textView) {
        this.textView = textView;
    }

}

还有 ListAdapter,在方法 getView() 中,我使用 AsyncTask 的方法 setTextView() 在 AsyncTask 中设置 TextView:

public class DownloadTasksAdapter extends BaseAdapter {
    private final Activity context;
    private DownloadTask[] downloadTasks;

    public DownloadTasksAdapter(Activity context, DownloadTask[] downloadTasks) {
        this.context = context;
        this.downloadTasks = downloadTasks;
    }

    public int getCount() {
        return downloadTasks.length;
    }

    public DownloadTask getItem(int position) {
        return downloadTasks[position];
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {       
        LayoutInflater inflater = (LayoutInflater) context.getLayoutInflater();
        final View rowView = inflater.inflate(R.layout.downloads_listview_row, parent, false);


        final TextView progressTxt = (TextView) rowView.findViewById(R.id.downloads_listview_row_progress);


        switch (downloadTasks[position].getStatus()) {
        case RUNNING:
            downloadTasks[position].setTextView(progressTxt);

            break;
        }

        return rowView;
    }
}

AsyncTask 从服务执行,但是当我打开包含 ListView 的 Activity 时,AsyncTask 应该更新一些字段,我该怎么做? :(

【问题讨论】:

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


    【解决方案1】:

    完成任务调用后

    mAdapter.notifyDataSetChanged();
    

    【讨论】:

    • 天啊!!!!!!我想吻你 :) 非常感谢 VEEEEERYYYY !!我找不到答案!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多