2012-09-16 96 views
0

我無法從單獨的類中的asynctask中將數據返回到我的主要活動中。將數據從AsyncTask返回到活動

這是我的主要活動:

public class Main extends Activity { 
EditText searchOne; 
EditText searchTwo; 

Button findMovies; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.totlayout); 

    //set the UI elements 
    searchOne = (EditText) findViewById(R.id.searchOne); 
    searchTwo = (EditText) findViewById(R.id.searchTwo); 

    findMovies = (Button) findViewById(R.id.findMovies); 


} 
public void displayResults(View view){ 
    //do something in response to button 
    Intent intent = new Intent(this, DisplayResultsActivity.class); 

    //make person search url1 
    final StringBuilder personSearchURLOne = new StringBuilder(getName.getName1(searchOne)); 
    final String searchURLOne = personSearchURLOne.toString(); 
    Log.d("searchurlone", searchURLOne.toString()); 

    //make person search url2 
    final StringBuilder personSearchURLTwo = new StringBuilder(getName.getName2(searchTwo)); 
    final String searchURLTwo = personSearchURLTwo.toString(); 

    //get ID 1 this needs to get data back from asynctask in the type string to be passed to buildCreditURL 
    new getFirstID().execute(searchURLOne); 

    //get ID 2 
    new getFirstID().execute(searchURLTwo); 

    //make credit search url1 
    final StringBuilder creditURLOne = new StringBuilder(buildCreditURL.getCreditURLOne(firstID)); 

下面是一個包含我的AsyncTask我單獨的類文件:

package com.tot.tipofthetongue; 

import android.os.AsyncTask; 

import android.util.Log; 





import org.json.JSONArray; 
import org.json.JSONException; 

import org.json.JSONObject; 

public class getFirstID extends AsyncTask<String, Void, String> { 

public static String TAG_ID = "id"; 
public static String TAG_RESULTS = "results"; 


String personOne = null; 
static String firstID = null; 


@Override 
protected String doInBackground(String... personSearchURLOne) { 
    Log.d("personSearchURLOne in getFirstID containts", personSearchURLOne[0]); 

    JSONArray results = null; 

    JSONParser jParser = new JSONParser(); 

    JSONObject jSon = jParser.doInBackground(personSearchURLOne[0]); 

    try{ 
     results = jSon.getJSONArray(TAG_RESULTS); 

     for(int i = 0; i < results.length(); i++){ 
      JSONObject r = results.getJSONObject(i); 
      firstID = r.getString(TAG_ID); 

     } 


    } 
    catch(JSONException e){ 
     Log.e("Error", e.toString()); 
    } 
      /need this value as string to be passed back to the main activity 
    return firstID; 


} 




} 

如何firstID作爲一個字符串到我的主要活動,也可以是在屬於另一個單獨的類文件的另一個方法中使用。

修訂getFirstID:

package com.tot.tipofthetongue; 


import android.os.AsyncTask; 

import android.util.Log; 





import org.json.JSONArray; 
import org.json.JSONException; 

import org.json.JSONObject; 

public class getFirstID extends AsyncTask<String, Void, String> { 

public static String TAG_ID = "id"; 
public static String TAG_RESULTS = "results"; 


String personOne = null; 
static String firstID = null; 


@Override 
protected String doInBackground(String... personSearchURLOne) { 

    Log.d("personSearchURLOne in getFirstID containts", personSearchURLOne[0]); 

    JSONArray results = null; 

    JSONParser jParser = new JSONParser(); 

    JSONObject jSon = jParser.doInBackground(personSearchURLOne[0]); 

    try{ 
     results = jSon.getJSONArray(TAG_RESULTS); 

     for(int i = 0; i < results.length(); i++){ 
      JSONObject r = results.getJSONObject(i); 
      firstID = r.getString(TAG_ID); 

     } 


    } 
    catch(JSONException e){ 
     Log.e("Error", e.toString()); 
    } 
    return firstID; 


} 
@Override 
public void onPostExecute(String firstID){ 

} 




} 

回答

1

您可以通過使用意圖送東西到另一個活動。

Intent intent = new Intent(this, YourActivity.class); 
intent.putExtra("name", string); 

讀到它here

或者你可以把你的AsyncTask類的活動類中。

public class Main extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.get_data); 


} 

private class Download extends AsyncTask <Void, Void, String> { 

    protected String doInBackground(Void...params) { 

    } 

    @Override 
    protected void onPostExecute(String result) { 

    } 

    @Override 
    protected void onPreExecute() { 

    } 

} 

} 
+0

所以在我的getFirstID類中添加時:Inent intent = new Intent(this,Main.class);在doInBackground下我得到一個構造函數未定義的錯誤。 – user881667

2

覆蓋在getFirstID類中的方法onPostExecute(String result)

@Override protected void onPostExecute(String result) { 
    // Invoke method on Main activity or do other action. 
} 

此方法由UI線程執行,並且可以更新視圖。您可以通過在構造函數中將其設置在getFirstID類的字段中來添加對MainActivity的引用。

希望它有幫助!

+0

我在更新中添加了@Override,但我不確定如何在主線程上調用它並獲取我的值。我對此很新。你能詳細說明嗎? – user881667

2

一個非常簡單的方法就是做到這一點。

public class getFirstID extends AsyncTask<String, Void, Pair<String, String>> { 
    public static interface MyAsyncCallback { 
     /** 
     * Called when the AsyncTask is finished processing the url. 
     * @param url the url that the firstId was from 
     * @param firstId the id that was parsed out of the json object 
     */ 
     public void setFirstId(String url, String firstId); 
    } 

    private final MyAsyncCallback mCallback; 

    public getFirstID(MyAsyncCallback callback) { 
     mCallback = callback; 
    } 

    public Pair<String,String> doInBackground(String... params) { 
     String firstID = parseJsonForID(params); // I am not going to copy your alg into here. 
     return Pair.create(params[0], firstId); 
    } 


    public void onPostExecute(Pair<String,String> urlAndFirstId) { 
    mCallback.setFirstId(urlAndFirstId.first, urlAndFirstId.second); 
    } 
} 

public class Main extends Activity implements MyAsyncCallback { 
    .... 

public void setFirstId(String url, String firstID) { 
     // Example 
     Intent intent = new Intent(this, OtherActivity.class); 
     intent.putExtra("firstId", firstId); 
     intent.putExtra("url", url); 
     startActivity(intent); 
} 
} 
+0

在活動中,一旦啓動了AsyncTask,您如何知道AsyncTask何時完成並且FirstID在編碼時是否正確設置?問題在於,無法預測FirstID何時準備就緒,因爲它正在運行單獨的線程並在應用程序運行時確定。而不使用一些等待機制,例如AsyncTask.get();回調實現沒有意義。 – yorkw

+0

不,回調沒有意義。在回調中傳遞的字符串是值。它已準備好被使用。所以第一個ID是找到的字符串。在setFirstId(String firstId)的回調方法中處理所有內容;當你擁有這個id並且這個任務肯定完成後,你會做什麼。沒有等待。 –

+0

我更新了代碼,以便回調現在管理URI的firstId配對。所以當回調事件發生時,您知道該ID來自給定的url。 –