0

我正在 Android 中创建一个应用程序,并且我有大量的字符串数据,允许 10,000 个或更多单词(项目),我想在列表视图中显示它。我的问题是我应该把我的(源)数据放在哪里

  1. XML 文件中的字符串数组
  2. 在数据库中(然后我必须放置一个外部数据库)
  3. 从简单的文本文件、CSV 等读取数据

在这里,我唯一关心的是速度,哪种方式更快以及为什么。

注意:我目前将数据作为字符串数组放入 Xml 中,并将其放入活动中的 Array 中,但是从 xml 加载数据需要几秒钟/片刻的时间,但只是第一次。

4

1 回答 1

2

执行代码以在 AsyncTask 中解析/加载 json/db 格式的内容以提高速度。我加载 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 开发者指南

于 2012-11-23T13:17:51.427 回答