8

I am trying to send data to server and in response i am getting some data which i need to display in next activity using listview,can any one tell me how to do this?

6 Answers6

3

use bundle to move your response from one activity to another

-create class that implements Parcelable

  public class ResponseData implements Parcelable{}

-Next save it in intent:

intent.putExtra(key, new ResponseData(someDataFromServer));

-Last step - retrive it:

Bundle data = getIntent().getExtras();
ResponseData response= (ResponseData ) data.getParcelable(key);

After that add it to an adapter to display it for the user

In other case you can save it in application context or database (not recommended)

Unii
  • 1,587
  • 15
  • 33
1

I would strongly suggest you to pass necessary parameters to next activity using Bundle and from next activity itself you should make a call to server to receive and display necessary information.

Mehul Joisar
  • 15,348
  • 6
  • 48
  • 57
0
  • Send data to server using AsyncTask

  • After getting result in doInBackground , continue with onPostexecute()

  • Start the next activity in onPostexecute()
Don Chakkappan
  • 7,397
  • 5
  • 44
  • 59
  • ok i am sending data using some fields like country,mother tongue,religion.it is kind of searching users list..and in response i will get user's name in list which i need to show in next activity. –  Dec 05 '14 at 10:13
  • http://stackoverflow.com/questions/7578236/how-to-send-hashmap-value-to-another-activity-using-an-intent?answertab=oldest#tab-top Is it ok for you ? – Don Chakkappan Dec 05 '14 at 10:15
0

Try using an event bus library, such as greenrobot EventBus. With it, the process will be done in 4 lines of code:

(In sender Activity)

EventBus.getDefault().register(this);
EventBus.getDeafult().postSticky(myDataObject);

(In receiving Activity)

DataObject passedData = EventBus.getDefault().getStickyEvent(DataObject.class);

You will also need a POJO data class:

public static class DataObject{
    private String data;

    public String getData(){
        return data;
    }

    public void setData(String data){
        this.data = data;
    }

}

Clean, quick and elegant :-)

Kelevandos
  • 7,024
  • 2
  • 29
  • 46
0
  1. Send data to server
  2. Get the response

  3. Put that string as Extra in Intent and start activity

    Intent i = new Intent(this, YourNextActivityName.class);
    i.putExtra("res",response.toString);
    i.startActivity();

  4. On Next Activity get that Data and display.

     Bundle extras = getIntent().getExtras();
     if (extras != null) {
        String response= extras.getString("res");
    }
    
Vipin Sharma
  • 594
  • 5
  • 19
0

Steps:

 1)Send data.
 2)Get Response
 3)if respose is ok, save that in a static string and fire intent and on the next 
   activity get the respose using classname.your string. simple and best way.
Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59