-1

My question is: In android programming how to pass double and string data obtained in onPostExecute() method of AsyncTask to another activity that will receive this data. for example to pass double coordinates and string names, that i got in onPostExecute() method and then transfer these string data to another activity. I attach my code. Thanks in advance.

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();
  • possible duplicate of [How to send string from one activity to another?](http://stackoverflow.com/questions/18146614/how-to-send-string-from-one-activity-to-another) – Soham Jul 20 '15 at 16:47
  • what you are doing seems like some heavy computation. It doesn't belong to `onPostExecute` that's run on UI thread. Consider doing it in your `runInBackground` method – kiruwka Jul 20 '15 at 18:12

3 Answers3

0

Before asking question please google it.Have you tried that.There are tons of examples.

You can use intents, which are messages sent between activities. In a intent you can put all sort of data, String, int, etc.

In your case, in activity2, before going to activity1, you will store a String message this way :

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

In activity1, in onCreate(), you can get the String message by retrieving a Bundle (which contains all the messages sent by the calling activity) and call getDouble() on it :

Bundle bundle = getIntent().getExtras();
String message =  bundle.getDouble("message");
Community
  • 1
  • 1
Soham
  • 4,397
  • 11
  • 43
  • 71
  • My problem is getting variables out of onPostExecute. After i have done so i can send the variables with an intent. Thankful for help! – user3087155 Jul 22 '15 at 12:58
0

If you want to start a new activity, you should do what Soham said and put the data on the intent.

If you want to send the data back to the activity that started the AsyncTask, you could send a copy of that context and have the AsyncTask call a specific method at the end of the execution and send the result through that method.

jujyfruits
  • 86
  • 6
0

A good way to get information from onPostExecute is to use an Interface.

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

Then back in our MainActivity you can call this to grab the data you want.

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

And as Soham answered, you can pass this output to an intent if you need to pass it to another class.

Ryan
  • 54
  • 2
  • 3
  • I'm sorry i don't understand how to put the interface in onPostExecute. I'm sorry for asking. – user3087155 Jul 21 '15 at 16:24
  • I have now created an interface called AsyncClass and put my variable map_loc there. I have also put the code in my MainActivity as you suggested. I dont get any data from processFinish. My problem is to get any data from onPostExecute that is void. Maybe i have missed some programming basic. Very thankful for help. – user3087155 Jul 22 '15 at 12:45