Solution:
Okay,I’ve found the solution from your answers since your answers are not completely working
First we need to use Handler to post a Runnable to main/UI thread to run UpdateDisplay() and but define ProgressDialog under UIThread not other threads which was also crucial.
Here is the final code :
public final String RSSFEEDOFCHOICE = "http://www.deals2buy.com/rssgen/tech.xml";
public final String tag = "RSSReader";
private RSSFeed feed = null;
private Handler handler = new Handler();
private ProgressDialog dialog;
/** Called when the activity is first created. */
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
dialog = ProgressDialog.show(RSSReader.this, "Loading",
"Loading, please wait..");
Thread t = new Thread() {
public void run() {
feed = getFeed(RSSFEEDOFCHOICE);
handler.post(new Runnable() {
public void run() {
dialog.dismiss();
UpdateDisplay();
};
});
}
};
t.start();
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Problem :
When Android application first load, this is what happens :
- Retrieving RSS content from Url and parse it into
RSSFeed feedinstance variable viagetFeed(String url). - Showing these feeds to users in ListView via
UpdateDisplay()method.
When these are happening, I wanted to show a ProgressDialog to users to notify them about system are retriving content like “Loading, please wait…” Once getFeed(String Url):RSSFeed is done, ProgressDialog will be dismissed and UpdateDisplay():void will be invoked.
So this is my first attempt :
public final String RSSFEEDOFCHOICE = "somewhere.xml"; //coming from http
public final String tag = "RSSReader";
private RSSFeed feed = null;
/** Called when the activity is first created. */
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
ProgressDialog dialog = ProgressDialog.show(RSSReader.this, "Loading...", "Loading, please wait");
feed = getFeed(RSSFEEDOFCHOICE);
dialog.dismiss();
UpdateDisplay();
}
And it doesn’t show the ProgressDialog so it didn’t work the way I want. Since I belived it is because ProgressDialog is continues progress, it should be handled by another Thread.
Here is my second attempt :
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Thread t = new Thread()
{
public void run()
{
ProgressDialog dialog = ProgressDialog.show(RSSReader.this, "Loading...", "Loading, please wait");
feed = getFeed(RSSFEEDOFCHOICE);
dialog.dismiss();
}
};
UpdateDisplay();
}
It was okay till UpdateDisplay() since this method was handled by main thread and it wasn’t waiting anything so I thought maybe I should have put it under t Thread after dialog.dismiss().
So I did this :
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Thread t = new Thread()
{
public void run()
{
ProgressDialog dialog = ProgressDialog.show(RSSReader.this, "Loading...", "Loading, please wait");
feed = getFeed(RSSFEEDOFCHOICE);
dialog.dismiss();
UpdateDisplay();
}
};
}
But then debugger said at run-time : only the main thread which handles onCreate():void can reach and use View objects because it creates them.
Now, I am so messed up and feel like quitting programming.
Some one can help me :/
Thanks in advance.
Okay,I’ve found the solution from your answers since your answers are not completely working
First we need to use
Handlerto post a Runnable to main/UI thread to runUpdateDisplay()and but defineProgressDialogunderUIThreadnot other threads which was also crucial.Here is the final code :