andrewb
andrewb

Reputation: 3095

Display number of items in TextView after async task in Android

I call a web service which fetches 0-n items which are displayed in a ListView in an Android activity.

In my onCreate() I have

new GetData().execute();

Which populates an object called customerDataList.

And in the onPostExecute I use a list adapter to display the returned data.

Above the ListView I would like to display the number of items retrieved, however if I do something like this:

new GetData().execute();
TextView numberOfItemsElm = (TextView) findViewById(R.id.number_of_items);
numberOfItemsElm.setText(customerDataList.size());

0 is displayed because that line is run before the execution has finished.

Any ideas?

Upvotes: 0

Views: 88

Answers (1)

codeMagic
codeMagic

Reputation: 44571

0 is displayed because that line is run before the execution has finished.

That is exactly right

So, move these lines

TextView numberOfItemsElm = (TextView) findViewById(R.id.number_of_items);
numberOfItemsElm.setText(customerDataList.size());

to your onPostExecute() after setting the adapter on your list.

Alternatively, you could place the code to set the text in a method which is called in onPostExecute() in case you use that code somewhere else in the Activity.

Although, that should actually throw a ResourceNotFoundException since you are sending an int as the param. It should be something like

numberOfItemsElm.setText("" + customerDataList.size());

Upvotes: 2

Related Questions