2012-11-23 44 views
0

我正在Android中創建應用程序,並且我有大量的字符串數據,讓路數爲10,000或更多的單詞(項目),我想將它顯示在列表中視圖。我的問題是,我應該把我的(源)數據如何存儲大型列表項目以獲得最大速度和效率

  1. 字符串數組的XML文件
  2. 在DB(然後我必須把外部DB)從一個簡單的文本文件,CSV
  3. 讀取數據等

在這裏我唯一的擔心是速度,哪種方式更快,爲什麼。

注意:我目前將數據放在Xml中作爲字符串數組,並將其放入Array中,但速度很慢,從xml載入數據只需要幾秒鐘的時間,但只是第一次。

+2

怎麼樣,你自己測試它? – ariefbayu

+1

嘗試使用它與Json格式。 Android有Json支持。 –

+1

我的猜測是項目2(數據庫)的延遲加載(您可以加載100個項目,在用戶滾動到列表結尾之前,您可以獲得更多的100並從適配器調用notifyDataSetChanged。但是,如果您的單詞列表始終是靜態的,則JSON文件可以是更有效的選擇,就像@MuratNafiz說的。 – dougcunha

回答

2

執行代碼以更快的速度解析/加載內容形式json/db在AsyncTask中。我加載5000行,每行〜400個字符。沒有AsyncTask需要更長的時間。

private class YourTask extends AsyncTask<String, Void, String> { 
     @Override 
     protected String doInBackground(String... s) { 

      //Here you have to make the loading/parsing tasks 
      //Don't call any UI actions here. For example a Toast.show() this will couse Exceptions 
      // UI stuff you have to make in onPostExecute method 

     } 

     @Override 
     protected void onPreExecute() { 
      // This method will called during doInBackground is in process 
      // Here you can for example show a ProgressDialog 
     } 

     @Override 
     protected void onPostExecute(Long result) { 
      // onPostExecute is called when doInBackground finished 
      // Here you can for example fill your Listview with the content loaded in doInBackground method 

     } 

} 

要執行你只需要撥打:

new YourTask().execute(""); 

在這裏你可以瞭解更多關於AsyncTasks:

AsyncTask developer Guide