-3

我在函数中使用 AsyncTask 如何在异步完成执行时返回一个值,因为我不能在这里使用 PostExecute 方法

AsyncTask.execute(new Runnable() {
            public void run() {}}

继承人的代码。我需要从 SpinnerList 中检索值以在主线程上使用 em。但是通常每当我要求数据时我都会得到空值,因为它还没有返回值。

AsyncTask.execute(new Runnable() {
            public void run() {
                try{
                    String fql = "SELECT uid, name FROM user WHERE is_app_user  AND uid IN (SELECT uid2 FROM friend WHERE uid1 = me())";
                    Bundle parameters = new Bundle();

                    parameters.putString("query", fql);
                    parameters.putString("method", "fql.query");
                    parameters.putString("access_token",  fb.getAccessToken());
                    String Response = fb.request(parameters);
                    JSONArray json = new JSONArray(Response);
                    list = new ArrayList<String[][]>();
                    String[][] friendsToList = new String[1][2];
                    SpinnerList = new ArrayList<String>();
                    String TempToSpinnerList=new String();
                    for(int i = 0; i < json.length(); ++i){
                        friendsToList[0][0] = json.getJSONObject(i).getString("uid");
                        friendsToList[0][1] = json.getJSONObject(i).getString("name");
                        TempToSpinnerList= friendsToList[0][1];
                            list.add(friendsToList);
                            SpinnerList.add(TempToSpinnerList);
                            Log.e("Test"," "+friendsToList[0][1]+" "+friendsToList[0][0]);
                        }


                        //dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        //spinner.setAdapter(dataAdapter);



    /*  jsonUser = fb.request(parameters);
        friendObj = Util.parseJson(jsonUser);

        friendArray = friendObj.getJSONArray("data");

        //
        friendlist = new ArrayList<String[][]>();
        String[][] friendsToList = new String[1][2];
        int friendCount = 0;
        String fId, fNm;
        JSONObject friend;
        for (int i = 0;i<friendArray.length();i++){
            //Get a JSONObject from the JSONArray
            friend = friendArray.getJSONObject(i);
            //Extract the strings from the JSONObject
           friendsToList[0][0] = friend.getString("id");
            friendsToList[0][1] = friend.getString("name");
            friendlist.add(friendsToList);
            //Set the values to our arrays
         Log.e("Tester",""+ friendsToList[0][0]+" "+ friendsToList[0][1]);
            friendCount ++;}*/
            }
                catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (FacebookError e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }   
            }});
4

2 回答 2

7

如果您想在 Asynctask 完成后执行某些功能,您可以在调用活动中设置回调函数。在 AsyncTask 类中创建一个构造函数并将调用活动传递给它。然后在您的任务完成时使用此引用调用回调函数。

您还可以从 onPostExecute 中更新 UI,如果它适合您,这会更简单。

public class MyTask extends AsyncTask<String, Void, String>
{
    private MainActivity activity;

    public MyTask(MainActivity activity) {
        this.activity = activity;
    }
    @Override
    protected Void doInBackground(String... params) {
        //Do background stuff
    }

    protected void onPostExecute() {
        //dismiss progress dialog if needed
        //Callback function in MainActivity to indicate task is done
        activity.taskDone("some string");
    }
}

MainActivity.java

//Pass your params array and the current activity to the AsyncTask
new MyTask(MainActivity.this).execute(params);

//Callback for AsyncTask to call when its completed
public void taskDone(String returnVal) {
    //Do stuff once data has been loaded
    returnText = returnVal;
}
于 2012-12-13T15:46:58.747 回答
1

您不会从 AsyncTasks 返回值。您需要将要更改的任何内容传递给 AsyncTask 并在 onPostExecute() 中更改值。请发布更多代码并更好地解释您的方案以获得完整答案。

编辑:正如 Jamis0n 所述,AsyncTask 计算的值也可以使用 get() 方法“获取”。

于 2012-12-13T15:40:59.750 回答