本文介绍了如何调用从AsyncTask的onPostExecute notifyDataSetChanged()()的另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OnCreate()中,我调用类ChannelStore创建一个单,我的片段的列表视图使用由channelstore创建的数据集。一旦doInBackground()完成我需要调用notifyDateSetChanged()适配器上,但我不知道如何实现这一目标是我的AsyncTask是一个类(ChannelStore)和适配器是在我的片段(ChannelListFragment)。有人可能会导致我的解决方案吗?

ChannelListFragment:

 公共类ChannelListFragment扩展ListFragment {
    私有静态最后弦乐TAG =ChannelListFragment;    公众的ArrayList<渠道> mChannels;
    公共ChannelAdapter适配器;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        。getActivity()的setTitle(R.string.channels_title);
        setHasOptionsMenu(真);        尝试{
            。mChannels = ChannelStore.get(getActivity())getChannels();
        }赶上(XmlPullParserException | IOException异常| InterruptedException的
                |为ExecutionException | TimeoutException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }
        适配器=新ChannelAdapter(mChannels);
        setListAdapter(适配器);
    }
...公共类ChannelAdapter扩展ArrayAdapter<渠道> {
        公共ChannelAdapter(ArrayList的<渠道>通道){
            超级(getActivity(),0,信道);
        }

本的AsyncTask在ChannelStore:

 私有类ChannelSetup扩展的AsyncTask<字符串,字符串,字符串> {
    @覆盖
    保护字符串doInBackground(字符串... PARAMS){
        尝试{
             //下载XML饲料    }    @覆盖
    保护无效onPostExecute(字符串结果){
        尝试{
             //解析XML数据源
        ....        Log.i(TAG,添加程序的通道......);
        addProgrammes(mProgrammes);        Log.i(TAG,每通道计划:
                + listChannelsProgrammes(mChannels));        //需要以某种方式更新列表视图适配器    }


解决方案

如果在onPostExecute您可以访问由channelstore和你的适配器创建数据集,你可以改变你的数据集,并通过调用notifyDateSetChanged更新您的适配器()。另一种方式来做到这一点是使用

  runOnUiThread(新的Runnable(){
   公共无效的run(){
    //你的code更新适配器。
   }
});

在每一个地方,你需要访问UI线程更新的东西。
你onPostExecute所以你必须访问片段适配器上的UI线程始终运行。为了做到这一点,你可以用

  getFragmentManager()。getFragmentByTag('你的片段标记')。ChannelAdapter.notifyDataSetChanged()

In onCreate() I am calling a class ChannelStore to create a singleton and my fragment's listview is using that dataset created by channelstore. Once the doInBackground() finishes I need to call notifyDateSetChanged() on the adapter but I am not sure how to accomplish that being that my AsyncTask is in one class (ChannelStore) and the adapter is in my fragment (ChannelListFragment). Can someone lead me to the solution?

ChannelListFragment:

public class ChannelListFragment extends ListFragment {
    private static final String TAG = "ChannelListFragment";

    public ArrayList<Channel> mChannels;
    public ChannelAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActivity().setTitle(R.string.channels_title);
        setHasOptionsMenu(true);

        try {
            mChannels = ChannelStore.get(getActivity()).getChannels();
        } catch (XmlPullParserException | IOException | InterruptedException
                | ExecutionException | TimeoutException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        adapter = new ChannelAdapter(mChannels);
        setListAdapter(adapter);
    }
...

public class ChannelAdapter extends ArrayAdapter<Channel> {
        public ChannelAdapter(ArrayList<Channel> channels) {
            super(getActivity(), 0, channels);
        }

The AsyncTask in ChannelStore:

private class ChannelSetup extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... params) {
        try {
             // download xml feed

    }

    @Override
    protected void onPostExecute(String result) {
        try {
             // parse xml feed
        ....

        Log.i(TAG, "Adding programmes to channels...");
        addProgrammes(mProgrammes);

        Log.i(TAG, "Programmes per channel: "
                + listChannelsProgrammes(mChannels));

        // need to update listview adapter somehow

    }
解决方案

If in onPostExecute you can access your dataset created by channelstore and your adapter, you can change your dataset and update your adapter by calling notifyDateSetChanged(). another way to do it is use

runOnUiThread(new Runnable() {
   public void run() {
    // your code to update adapter.
   }
});

in every where that you need to access ui thread to update something.your onPostExecute always runs on UI thread so you have access your fragment adapter. in order to do that you can use

getFragmentManager().getFragmentByTag('your fragment tag').ChannelAdapter.notifyDataSetChanged()

这篇关于如何调用从AsyncTask的onPostExecute notifyDataSetChanged()()的另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:15