AsyncTask 执行完成后 ExpandableListView 未更新

发布于 2024-11-09 15:37:49 字数 2091 浏览 3 评论 0原文

我有一个 ExpandableListView,它是作为活动的一部分创建的:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.news);

    // create the exandable list view widget
    newsListAdapter = new NewsListAdapter(this.getApplicationContext());    

    newsListView = (ExpandableListView) findViewById(R.id.news_list_view);

    newsListView.setAdapter(newsListAdapter);

    final Button homeButton = (Button) findViewById(R.id.home_btn);

    // go back to the home page
    homeButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {                
            startActivity(new Intent(view.getContext(), HomeActivity.class));
        }
    });   

    newsListView.setOnGroupClickListener(new OnGroupClickListener() {
        public boolean onGroupClick(ExpandableListView parent, View view, int groupPosition, long arg3) {
            return false;
        }
    });

    newsListView.setOnChildClickListener(new OnChildClickListener() {
        public boolean onChildClick(ExpandableListView parent, View view, 
                                    int groupPosition, int childPosition, long id) {
            return false;
        }
    });

    showLoadingProgressDialog();

    downloadRssFeedTask = new DownloadRssFeedTask();

    downloadRssFeedTask.execute();
}  

正如您从代码中看到的,它会关闭并从 AsyncTask 中的 RSS feed 中进行拉取。

AsyncTask 完成,进度对话框消失,并且 Activity 被告知任务已完成:

   private void responseReceived(Channel newsResponse) {
    if (newsResponse != null)           
        newsListAdapter.buildView(newsResponse);
    }
    else {
    Toast toast = Toast.makeText(this.getApplicationContext(), 
                           "There is no news available at this time.", 
                           Toast.LENGTH_LONG);

        toast.show();
    }
}

但是,ExpandableListView 在加载数据后不会重绘/刷新自身,它是一个空白屏幕。如何让 ExpandableListView 使用 RSS 数据刷新自身?我尝试过 invalidaterefreshDrawableState 但没有任何乐趣。

想法?

I have an ExpandableListView that is created as part of an activity:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.news);

    // create the exandable list view widget
    newsListAdapter = new NewsListAdapter(this.getApplicationContext());    

    newsListView = (ExpandableListView) findViewById(R.id.news_list_view);

    newsListView.setAdapter(newsListAdapter);

    final Button homeButton = (Button) findViewById(R.id.home_btn);

    // go back to the home page
    homeButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {                
            startActivity(new Intent(view.getContext(), HomeActivity.class));
        }
    });   

    newsListView.setOnGroupClickListener(new OnGroupClickListener() {
        public boolean onGroupClick(ExpandableListView parent, View view, int groupPosition, long arg3) {
            return false;
        }
    });

    newsListView.setOnChildClickListener(new OnChildClickListener() {
        public boolean onChildClick(ExpandableListView parent, View view, 
                                    int groupPosition, int childPosition, long id) {
            return false;
        }
    });

    showLoadingProgressDialog();

    downloadRssFeedTask = new DownloadRssFeedTask();

    downloadRssFeedTask.execute();
}  

as you can see from the code, it goes off and does a pull from an RSS feed in an AsyncTask.

The AsyncTask finishes, the progress dialog is dismissed and the activity is told that the task has completed:

   private void responseReceived(Channel newsResponse) {
    if (newsResponse != null)           
        newsListAdapter.buildView(newsResponse);
    }
    else {
    Toast toast = Toast.makeText(this.getApplicationContext(), 
                           "There is no news available at this time.", 
                           Toast.LENGTH_LONG);

        toast.show();
    }
}

However, the ExpandableListView does not redraw/refresh itself after the data is loaded, it is a blank screen. How do you get the ExpandableListView to refresh itself with the RSS data? I have tried invalidate and refreshDrawableState but there was no joy.

Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可是我不能没有你 2024-11-16 15:37:49

我猜您在下载 RSS feed 后忘记调用适配器上的 notifyDataSetChanged() 方法(可能您应该在 AsyncTaskonPostExecute() 方法中调用它>)。

I guess you forgot to call notifyDataSetChanged() method on adapter after downloading RSS feed (probably you should call it in onPostExecute() method in your AsyncTask).

陌路黄昏 2024-11-16 15:37:49

这应该有效。

newsListAdapter.notifyDataSetChanged()

This should work.

newsListAdapter.notifyDataSetChanged()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文