从AsyncTask的返回结果片段 [英] return result from asynctask to a fragment

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

问题描述

首先,我的项目的一些信息:

First of all, some info about my project:

我有一个包含几个片段的应用。 (我有一个扩展FragmentActivity和另外一个扩展FragmentPagerAdapter一个类。)
所以实际上我的code是片段内运行。

I have an application that consists of several fragments. (I have one class that extends FragmentActivity and another one that extends FragmentPagerAdapter.) So actually my code is running inside the Fragments.

我把里面的片段之一的AsyncTask的做一些计算。该AsyncTask的是一个单独的文件和类不位于片段类中。

I call an asynctask inside one of the fragments to do some calculations. The asyncTask is a seperate file and the class is not located inside the Fragment class.

所以1文件是AsyncTask的,另外一个fragmet(从片段变量是不是由AsyncTask的访问直接!!!)。

So 1 file is the asynctask and another one the fragmet (variables from the fragment are not accessible direct by the asynctask!!!).

我可以通过使用ProgressDialog显示的AsyncTask的结果。
不过,我希望能够要返回的数据(AsyncTask的结果)来表示我有我的片段的变量。什么到目前为止,我已经试过是:

I can display the result of the asyncTask by using ProgressDialog. However I want to be able to return the data (result of the asynctask) back to a variable that I have in my fragment. What I have tried so far is:


  1. 使用从asyncatask.execute的get()()

我试图做这样的:

我用下面的code这是一个按钮,点击后调用。

I used the following code that was called after a button click.

mytask1 = new myAsyncTask(this);
        String result =mytask1.execute(value1).get();

不过,这将导致在主线程卡住,等待AsyncTask的完成。

However this results in the main thread to get stuck and wait for the asynctask to complete.

2,采用loginShare preferens

2.Using loginSharePreferens

loginPreferences = getActivity().getSharedPreferences("loginPrefs", Context.MODE_PRIVATE);
            loginPrefsEditor = loginPreferences.edit();
            loginPrefsEditor.putString("result", result);
            loginPrefsEditor.commit();

这给了我的错误。

3.我也试过这个结果
共享preferences共享preFS = preferenceManager.getDefaultShared preferences(getApplicationContext());

3.I tried also this
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext() );

这与第二个方法类似。

4。我试图创建我的片段的方法,将刚刚设置的值,我试图把它称为段内

4.I tried to create a method in my fragment that would just set the value and I tried to call it inside the fragment

MyFrragment1.this.setRestult(1); // is called in the onPostExecute of the asyncTask

public void setRestult(int result){
        this.test1=result; ///inside of the fragment
    }

这给了我错误:类型MyFragment1没有外围实例是范围访问

This gave me error: "No enclosing instance of the type MyFragment1 is accessible in scope"

我不知道还有什么尝试。任何想法?

I don't know what else to try. Any ideas ?

推荐答案

您可以使用它运行在UI线程,并在<$ C $称为 onPostExecute 方法C> doInBackground 结束。您可以覆盖在你的片段是这样的:

You can use the onPostExecute method which runs in the UI thread and is called after doInBackground finishes. You can override it in your Fragment like this:

new myAsyncTask() {

        @Override
        protected void onPostExecute( Result result ) {

            super.onPostExecute( result );
            // Do something with result here
        }
    }.execute(value1);

这篇关于从AsyncTask的返回结果片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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