Issue with android layout and progressbar
I'm still working on my application that has to store RSS data localy. So far i managed to create a local database and parse the online RSS feed. I also managed to put this information in the database so now the app will also be available while being offline.
However i can't seem to figure out how to do the following: I have 2 buttons, one of them reads the database titles and will go to a detailed page of your selection (still working on that)
My current problem is that i can't seem to add a loading bar to pop up on the screen while the application is downloading the data from the internet. The screen is just blank for like 10 seconds and then the list pops up. here is the code that i'm using:
public class SynchDB_Activity extends ListActivity {
public List<Message> messages;
public List<String> titles;
//ProgressDialog dialog = ProgressDialog.show(SynchDB_Activity.this, "", "Downloading data...", true);
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.animal_list_view);
loadFeed();
}
private void loadFeed(){
try{
BaseFeedParser parser = new BaseFeedParser();
messages = parser.parse();
List<String> titles = new ArrayList<String>(messages.size());
DBAdapter db = new DBAdapter(this);
for (Message msg : messages){
titles.add(msg.getTitle());
db.addRow(
msg.getTitle(),
msg.getDescription(),
"bla",
"http:/",
msg.getLink());
}
//dialog.dismiss();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.row,titles);
this.setListAdapter(adapter);
} catch (Throwable t){
Log.e("AndroidNews",t.getMessage(),t);
}
}
So this code works and puts all the data in the local database. However i just cant make that loading bar working and it's driving me crazy.
If anyone could help me out i would be grat开发者_如何学Cefull!!! I've been googling all day and nothing seems to do the trick....
many thanks
edit: As you can see in the code i display the list when its done. This is not nececary, but from the moment i remove the ArrayAdapter the app won't work anymore.
Keep in mind that this all is quite new for me and i think that the main part where things are going wrong is useing the different layouts...
Use Async task and progress bar as shown here:
public void getrss()
{
try{
class test extends AsyncTask{
TextView tv_per;
int mprogress;
Dialog UpdateDialog = new Dialog(ClassContext);
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
mprogress = 0;
UpdateDialog.setTitle(getResources().getString(R.string.app_name));
UpdateDialog.setContentView(R.layout.horizontalprogressdialog);
TextView dialog_message = (TextView)UpdateDialog.findViewById(R.id.titleTvLeft);
tv_per = (TextView)UpdateDialog.findViewById(R.id.hpd_tv_percentage);
dialog_message.setText(getResources().getString(R.string.dialog_retrieving_data));
dialog_message.setGravity(Gravity.RIGHT);
UpdateDialog.setCancelable(false);
UpdateDialog.show();
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Object... values) {
// TODO Auto-generated method stub
ProgressBar update = (ProgressBar)UpdateDialog.findViewById(R.id.horizontalProgressBar);
update.setProgress((Integer) values[0]);
int percent = (Integer) values[0];
if(percent>=100)
{
percent=100;
}
tv_per = (TextView)UpdateDialog.findViewById(R.id.hpd_tv_percentage);
tv_per.setText(""+percent);
}
@Override
protected Object doInBackground(Object... params) {
// TODO Auto-generated method stub
//your code
}
super.onPostExecute(result);
UpdateDialog.dismiss();
}
}
new test().execute(null);
}
catch(Exception e)
{
e.printStackTrace();
}
}
You probably want to use AsyncTask.
精彩评论