问题与Android的布局和进度进度、布局、问题、Android

2023-09-05 05:14:59 作者:航帆直上

我仍然在我的应用程序,它具有localy存储RSS数据。 到目前为止,我设法创建一个本地数据库和解析在线RSS提要。 我也设法把数据库中的信息所以现在的应用程序也将提供,而脱机。

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.

不过,我似乎无法弄清楚如何做到以下几点: 我有2个按钮,其中一个读取数据库的标题,并会去选择一个详细页面(仍然在这一点)

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)

我现在的问题是,我似乎无法添加加载栏弹出在屏幕上,而应用程序是从网上下载的数据。屏幕是只是空白像10秒钟,然后在列表弹出。这里要说的是我使用的code:

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:

公共类SynchDB_Activity扩展ListActivity {

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);
    }
}

所以这个code ++工程,并把所有的数据在本地数据库中。不过,我只是不能作出这样的加载条的工作和它的驾驶我疯了。

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.

如果有人可以帮助我,我将gratefull! 我一直在Google上搜寻一整天,似乎没有这样的伎俩......

If anyone could help me out i would be gratefull!!! I've been googling all day and nothing seems to do the trick....

非常感谢

编辑:正如你可以在codeI看到显示的列表时,它的完成。这不是nececary,但从那一刻我删除一个ArrayAdapter应用程序将无法正常工作了。

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();
     }

}