Android的清爽片段查看AsyncTask的后 [英] Android Refreshing Fragment View after AsyncTask

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

问题描述

我想一个的AsyncTask 在我的活动完成时更新名单的片段,但我不知道如果我做错了什么。目前,我有一个按钮,启动的AsyncTask

 搜索=(按钮)findViewById(R.id.search);
    sea​​rch.setOnClickListener(
            新View.OnClickListener(){
                @覆盖
                公共无效的onClick(视图v){
                    。字符串产品名称= prodname.getText()的toString()修剪();
                    如果(NetworkManager.isOnline(getApplicationContext())){
                        //转到其它屏幕
                        AsyncFetcher结果=新AsyncFetcher(currActivity);
                        字符串_url =htt​​p://192.168.1.3:3000/search.json?
                                       UTF8 =%E2%9C%93&安培; Q =+产品名称;
                        // progressDialog = ProgressDialog.show(
                        // ClassifiedsActivity.this,,载入中...);
                        results.execute(_url);
                    } 其他 {
                        //引发一些警告说,没有互联网
                        //连接被发现
                    }
                }
            });
 

在我的执行所是我下面要在我的活动实例片段完成:

  ResultFragment resultfrag =新ResultFragment();
getSupportFragmentManager()的BeginTransaction()
                           .replace(R.id.content_frame,resultfrag).commit();
 

不过它似乎犯规来代替我的内容列表片段。 这是我的布局文件:

 <的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>

    <的FrameLayout
        机器人:ID =@ + ID / content_frame
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent/>
< / LinearLayout中>
 

解决方案

首先更换一个片段是不一样清爽的。是的,它重新加载在它和每一个数据视图,以及。但是,你必须与你的新数据与它。所以我会电子书籍,您可以创建在片段的刷新方法,并发送这个方法你刷新的数据,然后通知你的适配器 dataSetChanged

要做到这一点,你会需要达到自己的当前连接的片段,并调用它的刷新方法。您可以使用 findFragmentByTag 来实现它了。

编辑:澄清一点的

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

  ResultFragment resultFrag =(ResultFragment)getSupportFragmentManager()
                                 .findFragmentByTag(FragToRefresh);
如果(resultFrag!= NULL){
    resultFrag.refreshData(refreshedArray);
}
 

而在你的 ResultFragment 您需要有 refreshData 方法,它是这样的:

 公共无效refreshData(ArrayList中< YourObject>数据){
   yourArray =新的ArrayList< YourObject>(数据);
   yourAdapter.notifyDataSetChanged();
}
 

每当你的任务是完成你的名单将被刷新。

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>

解决方案

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.

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.

Edit: To clarify little bit more

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

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.

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

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