2015-07-20 14 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

相关问题