2011-05-04 39 views
0

我仍然工作在我的應用程序,它具有localy存儲RSS數據。 到目前爲止,我設法創建了一個本地數據庫並解析了在線RSS源。 我也設法將這些信息放在數據庫中,所以現在應用程序也可以在離線狀態下使用。問題與Android的佈局和進度

但是我似乎無法弄清楚如何做到以下幾點: 我有2個按鈕,其中一個讀取數據庫的標題,並會去選擇的詳細頁面(仍在對那個)

我現在的問題是,我似乎無法添加加載條出現在屏幕上,而應用程序從互聯網上下載的數據。屏幕只有10秒鐘的空白,然後彈出列表。這裏是我使用的代碼:

公共類SynchDB_Activity延伸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); 
    } 
} 

所以此代碼的工作,並把所有的數據在本地數據庫中。不過,我不能讓加載欄工作,這讓我發瘋。

如果有人可以幫助我,我將gratefull! 我一直在google搜索了一整天,似乎沒有這樣的伎倆....

千恩萬謝

編輯:正如你可以在代碼中看到我顯示列表中的完成時。這不是必需的,但從我刪除ArrayAdapter的那一刻起,該應用程序就不再工作了。

請記住,這一切對我來說是很新的,我認爲這東西在哪裏走錯了主要部分期運用不同的佈局...

回答

0

使用異步任務和進度條:

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

} 
+0

感謝您的答覆但是我不知道如何實現你給我 的代碼應該如何的看法模樣,使這項工作? 另外我在「ClassContext」和關於如何正確使用標記的一些錯誤「{」「}得到一個錯誤」 是否有機會你可以張貼intire項目?這會讓我更容易理解我做錯了什麼。 感謝 – Oblivion 2011-05-05 08:44:37

+0

您可以突出顯示在其上得到錯誤的行?你想要更新對應的佈局文件嗎? – 2011-05-06 04:33:46

+0

嘿,我試了一下你的代碼,最終我發現了它,非常感謝你的快速回復! – Oblivion 2011-05-06 11:11:32

0

你可能想使用AsyncTask。如下圖所示