How to populate ListView from an AsyncTask?

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

How to populate ListView from an AsyncTask?

Postby Mierdin » Wed Jul 28, 2010 8:34 pm

Hellow all!

So, I imagine this is pretty easy but I can't quite do it - I'm trying to poplate a ListView with data in an array and like a good little boy, I'm using an ArrayAdapter to populate the Listview within the onCreate method of a ListActivity:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ArrayAdapter<String> arAdap;
  2.  
  3. public class start extends ListActivity {
  4.  
  5.     public static ArrayAdapter<String> arAdap;
  6.     public void onCreate(Bundle savedInstanceState) {
  7.  
  8.         super.onCreate(savedInstanceState);
  9.         setContentView(R.layout.main);
  10.  
  11.         String[] mStrings = new String[] { "Menu > Poll Devices" };
  12.         arAdap = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings);
  13.  
  14.  
  15.     }
  16. }
  17.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


I have another class, in which I'm using AsyncTask to retrieve data from a WebService. The method onPostExecute, which runs when the AsyncTask is done, contains my data in the form of an ArrayList<String>. It is below:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.          protected void onPostExecute(String[] result) {
  3.                 start.arAdap = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings);
  4.          }
  5.  
  6.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4



As you can see, I'm trying to set the public ArrayAdapter arAdap from this method in the AsyncTask class. I know there's good data in here because I was previously passing it to a simple TextView in the "start" class.

However, I'm getting errors (I'm sure related to context) because the "this" mentioned in the AsyncTask class is obviously not the same as the "this" in the actual ListView class.

So......how to do context for setting the ListView contents from this seperate class?
Or is there a better way to do it entirely that I totally missed? I'm open at this point. Thanks!
Mierdin
User avatar
Mierdin
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Jul 28, 2010 8:01 pm

Top

Re: How to populate ListView from an AsyncTask?

Postby mmh02 » Sat Sep 25, 2010 9:56 am

Hi Mierdin,

I just posted an example on my blog that implemented this. I basically created an instance variable named mainActivity in the my "main" class (start in your case), then saved the reference. Then in my AsyncTask, I just referred to the mainActivity, rather than "this".

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MyListActivity extends ListActivity {
  2.  
  3.     private MyListActivity mainActivity = null;
  4.  
  5.     ...
  6.  
  7.     public void onCreate(Bundle savedInstanceState) {
  8.         ...
  9.         mainActivity = this;
  10.         ...
  11.     }
  12.  
  13.     ...
  14. }
  15.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


FYI, here's the link to the post, which contains the full source code: http://www.hassanpur.com/blog/?p=27
mmh02
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Sep 25, 2010 9:43 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests