Workflow when loading data from server and filling activity listview


Last Updated: 2013-02-13 14:14:43
  1. SloppyAndroid

    SloppyAndroid New Member

    Hello,

    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) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);


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


    // 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)
    {
    setContentView(R.layout.activity_city_list);


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

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

    Advertisement
  2. Pcriulan

    Pcriulan New Member

    Hi,

    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

    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