2015-07-20 16 views
-1

我的問題是:在)的Android程序如何通過在onPostExecute獲得雙重和字符串數據(的AsyncTask的方法,將接收該數據的另一個活動。例如傳遞double座標和字符串名稱,我得到了onPostExecute()方法,然後將這些字符串數據傳遞給另一個活動。我附上我的代碼。提前致謝。如何從OnPostExecute方法導致

protected void onPostExecute(String result) { 
    super.onPostExecute(result); 

    String[] map = result.split("[\":,]"); 

    int i = 64; 
    int a = 1; 
    String[] lat = new String[6000]; 
    while (i < 5000) { 
     lat[a] = map[i]; 
     i = i + 84; 
     a = a + 1; 
    } 

    int i2 = 76; 
    int a2 = 1; 
    String[] lng = new String[6000]; 
    while (i2 < 5000) { 
     lng[a2] = map[i2]; 
     i2 = i2 + 84; 
     a2 = a2 + 1; 
    } 

    int i3 = 1; 
    map_lat = new double[61]; 
    while (i3 < 60) { 
     map_lat[i3] = Double.parseDouble(lat[i3]); 
     Log.i("JSON", "Lat: " + map_lat[i3]); 
     i3 = i3 + 1; 
    } 

    int i4 = 1; 
    map_lng = new double[61]; 
    while (i4 < 60) { 
     map_lng[i4] = Double.parseDouble(lng[i4]); 
     Log.i("JSON", "Lng: " + map_lng[i4]); 
     i4 = i4 + 1; 
    } 

    int i5 = 40; 
    int a5 = 1; 
    map_loc = new String[6000]; 
    while (i5 < 4950) { 
     map_loc[a5] = map[i5]; 
     Log.i("JSON", "Loc: " + map_loc[a5]); 
     i5 = i5 + 84; 
     a5 = a5 + 1; 
    } 

    int i6 = 58; 
    int a6 = 1; 
    map_info = new String[6000]; 
    while (i6 < 4950) { 
     map_info[a6] = map[i6]; 
     Log.i("JSON", "Info: " + map_info[a6]); 
     i6 = i6 + 84; 
     a6 = a6 + 1; 
    } 


double[] ab = map_lat; 
double[] ac = map_lng; 
String[] ad = map_loc; 
String[] ae = map_info; 
String af = "0"; 


    public double[] Map(){ 
    double[] ab = map_lat; 
    double[] ac = map_lng; 
    String[] ad = map_loc; 
    String[] ae = map_info; 
     return ab; 
} 

double[] ag = Map(); 
+0

可能重複的[如何將字符串從一個活動發送到另一個?](http://stackoverflow.com/questions/18146614/how-to-send-string-from-one-activity-to-another) – Soham

+0

你在做什麼似乎是一些沉重的計算。它不屬於在UI線程上運行的'onPostExecute'。考慮這樣做在你的'runInBackground'方法 – kiruwka

回答

0

問的問題,請谷歌it.Have你試過that。還有以前噸的例子。

您可以使用意圖,這是活動之間發送的消息。在一個意圖,你可以把所有類型的數據,字符串,整數等

在你的情況下,在活性2,睡前活動1,你將存儲一個字符串這樣的留言:

Intent intent = new Intent(activity2.this, activity1.class); 
intent.putExtra("message", message); 
startActivity(intent); 

在活動1,中的onCreate(),你可以通過檢索包(其中包含所有由調用活動發送的消息)的字符串消息,並在其上調用getDouble():

Bundle bundle = getIntent().getExtras(); 
String message = bundle.getDouble("message"); 
+0

我的問題越來越變數出來onPostExecute的。我完成後,我可以發送一個意圖的變量。感謝您的幫助! – user3087155

0

如果你想開始一個新的活動,你應該做什麼Soham說,並把數據的意圖。

如果要將數據發送回啓動AsyncTask的活動,可以發送該上下文的副本,並在執行結束時讓AsyncTask調用特定的方法,並通過該方法發送結果。

0

一個好方法,從onPostExecute獲取信息是使用一個接口。

public interface AsyncClass { 
    void processFinish(String[] output); 
} 

然後回到我們的MainActivity中,你可以調用它來獲取你想要的數據。

@Overide 
public void processFinish(String[] output) { 
    String[] temp = output; 
} 

而作爲Soham回答,你可以,如果你需要將它傳遞給另一類輸出傳遞給意圖。

+0

對不起,我不明白如何把接口放在onPostExecute。我很抱歉問。 – user3087155

+0

我現在已經創建了一個名爲AsyncClass的接口,並將我的變量map_loc放在那裏。按照您的建議,我也將代碼放入了我的MainActivity中。我沒有從processFinish獲取任何數據。我的問題是從onPostExecute獲取任何無效的數據。也許我錯過了一些編程基礎。非常感謝您的幫助。 – user3087155

相關問題