Reputation: 105
I am trying to fill my list view in android (using my own adapter) with some data returned by a thread, which gets some xml content from a website. This works properly!
The List View is filled correctly with the returned Array List object. Thats the way I do it:
//.. function name blabla
ll_wait.setVisibility(View.VISIBLE); // A List View object displaying 'please wait'
lv1=(ListView)findViewById(R.id.listView1);
TextView tv1 = (TextView)findViewById(R.id.tv_Status);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String sortierung = prefs.getString("pref_sortierung", "Entfernung");
String distanz = prefs.getString("pref_distance", "10");
refreshElements refresh = new refreshElements(lat, lon, sortierung, distanz); // its a thread
refresh.start();
while(refresh.isAlive()) {
// HERE IS MY PROBLEM
// HERE IS MY PROBLEM
// HERE IS MY PROBLEM
// HERE IS MY PROBLEM
}
try {
// assigning some data, works correctly
column_names = refresh.getElementColumn(1);
// assigning some data, works correctly
EfficientAdapter adapter = new EfficientAdapter(getApplicationContext());
lv1.setAdapter(adapter);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage() + e.getLocalizedMessage() + e.hashCode() + e.getCause() + e.toString(), Toast.LENGTH_SHORT).show();
}
dialog.dismiss();
ll_wait.setVisibility(View.GONE);
// -... end of function
As you can see, in my code, there is a comment saying "HERE IS MY PROBLEM". Of course the main thread waits (as a result of while(){...}) due the refresh thread isn't alive anymore and has been finished. And thats the point! My complete thread is freezing during the operation! I've tried to move the listView Adapter stuff into a second, seperate thread... But it says that no list view adapter can be set out of the main thread. After the freezing (sometimes the application crashes... timeout i dunno) the data will be assigned to some statics, which were used in my own EfficientAdapter.
Is it possible to wait for the data of the Thread A (refresh thread) and using the returned Array List as a list view content WITHOUT freezing the whole application?
I'll hope you got the point, because I am from germany and my english is not perfect.
Upvotes: 0
Views: 1342
Reputation: 137312
Yes, it's possible.You can use AsyncTask, which is part of the Android SDK, to accomplish that.
In doInBackground()
you build your arraylist.
In onProgressUpdate()
you can update results on UI thread during processing. (this method is executed on the UI thread).
In onPostExecute()
you are in the stage after finishing the process, and you can post results to the UI (this method is executed on the UI thread)
Upvotes: 3