AsyncTask 后 Android 刷新片段视图 [英] Android Refreshing Fragment View after AsyncTask

查看:19
本文介绍了AsyncTask 后 Android 刷新片段视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的活动中的 AsyncTask 完成时更新列表片段,但我不确定我是否做错了什么.目前,我有一个启动 AsyncTask 的按钮:

I am trying to update list fragment when an AsyncTask in my activity completes but I am not sure if I am doing something wrong. Currently, I have a button that initiates the AsyncTask:

search = (Button)findViewById(R.id.search);
    search.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String productname = prodname.getText().toString().trim();
                    if (NetworkManager.isOnline(getApplicationContext())){
                        // Go to Other screen
                        AsyncFetcher results = new AsyncFetcher(currActivity);
                        String _url = "http://192.168.1.3:3000/search.json?
                                       utf8=%E2%9C%93&q="+productname;
                        // progressDialog = ProgressDialog.show(
                        //    ClassifiedsActivity.this, "", "Loading...");
                        results.execute(_url);
                    } else {
                        // Throw some warning saying no internet 
                        // connection was found
                    }
                }
            });

一旦我的执行完成,我将在我的活动中实例化片段:

Once my execute is finished I have following to instantiate fragment within my activity:

ResultFragment resultfrag = new ResultFragment();
getSupportFragmentManager().beginTransaction()
                           .replace(R.id.content_frame, resultfrag).commit();

但是它似乎没有用列表片段替换我的内容.这是我的布局文件:

However it doesnt seem to replace my content with the list fragment. Here is my layout file:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

推荐答案

首先替换一个片段与刷新它完全不同.是的,它还会重新加载其中的每个视图和数据.但是您必须将其与新数据相关联.所以我建议你在你的片段中创建一个刷新方法并将你刷新的数据发送到这个方法,然后通知你的适配器 dataSetChanged.

First replacing a fragment is nothing like refreshing it. Yes, it reloads every view in it and data as well. But you have to relate it with your new data. So i would recomment you to create a refresh method in your fragment and send this method your refreshed data, then notify your adapter as dataSetChanged.

要实现这一点,您需要到达当前附加的片段并调用其刷新方法.您可以使用 findFragmentByTag 来获取它.

To achieve this, you're gonna need to reach your currently attached fragment and call its refresh method. You can use findFragmentByTag to reach it out.

澄清一点

当你的 AsyncTask 完成后,你应该做类似这样的 onPostExecute 方法:

When your AsyncTask is done, you should do something like this onPostExecute method:

ResultFragment resultFrag = (ResultFragment) getSupportFragmentManager()
                                 .findFragmentByTag("FragToRefresh");
if (resultFrag != null) {
    resultFrag.refreshData(refreshedArray);
}

在你的 ResultFragment 中你需要有 refreshData 方法,就像这样:

And in your ResultFragment you need to have refreshData method, which is something like this:

public void refreshData(ArrayList<YourObject> data) {
   yourArray = new ArrayList<YourObject>(data);
   yourAdapter.notifyDataSetChanged();
}

只要您的任务完成,您的列表就会刷新.

And your list will be refreshed whenever your task is done.

这篇关于AsyncTask 后 Android 刷新片段视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆