1. Check out our companion app, Forums for Android! Download from Google Play

Workflow when loading data from server and filling activity listview

Discussion in 'Application Development' started by SloppyAndroid, Feb 7, 2013.

  1. SloppyAndroid

    SloppyAndroid New Member
    Thread Starter

    Feb 6, 2013

    I'm pretty new to Android development and I just hit a brick wall in the development of an app, I'm currently working on. I hope you can help me.

    The problem is that I have a list of locations (states), and when one is selected, a list of cities from that state is loaded via http request and JSON and then is supposed to be inserted into a listview in another activity.

    So now I have the StateList activity and the CityList activity. Currently, the statelist activity starts the citylist activity via an intent and gives it a state name. Then, the citylist activity uses that name and requests data from a server via AsyncTask. As result I get an array of City objects, which are supposed to be inserted into an adapter for a listview.

    Now begins my problem: I don't want the second activity to show up before all data is loaded. I also don't want the first activity to do the request and then have to transfer the whole result to the second activity using intent extras.

    So what would I have to do now to have the second activity load all the data and only then really show up? I tried only setting the content view after receiving the data, but since the base class onCreate() is tied to some manipulation of the layout, I would also have to call onCreate() at that point, which is not allowed.

    Can somebody please point me in the right direction, which would be the right solution to this problem?

    Here some code snippets of what I tried already:

    The onCreate() method of my second activity (which is supposed to hide):
    [high]protected void onCreate(Bundle savedInstanceState) {

    // send request for citylist data
    CitylistRequest request = new CitylistRequest(this, new State(0, getIntent().getExtras().getString("state")));

    // the following will throw an exception, because it relies on the layout being inflated!
    super.onCreate(savedInstanceState, "Cities", getIntent().getExtras().getString("state"));
    }[/high]The method, which is called upon receiving all data:
    [HIGH]public void receiveCitylistRequest (City[] cities)

    ListView list = (ListView)findViewById(R.id.listview_cities);

    CityViewAdapter adapter = new CityViewAdapter(this, cities);
    }[/HIGH]Thank you!


  2. Pcriulan

    Pcriulan New Member

    Feb 12, 2013

    don't use AsyncTask for network calls because AsyncTask is strongly tied to your activity. Use a Service instead. Create some lsiteners to your request object (onSuccess, onFailed, onStarted, etc.). Doing that way allow you to do something in your User Interface in asyncrhonous way.

    Take a look to some librairies :

    RoboSpice but may be overkill for you
    RESTDroid it's my library. It's an alpa release but it's quite simple to use.
  3. jonbonazza

    jonbonazza Well-Known Member

    Jul 13, 2010
    Um.. no, a Service should NOT be used for this. AsyncTask is perfectly acceptable to be used for network calls, however I strongly advise using a regular Thread instead, since AsyncTasks are queued and only up to 10 ( I think it's 10) can be in queue at any given time. This can create issues, albeit rarely.

Share This Page