我正在Android中创建一个应用程序,我有大量的字符串数据,让一万个或更多的单词(项目),我想在列表视图中显示它。我的问题是我应该在哪里放置我的(来源)数据
这里我唯一关心的是速度,哪种方式更快,为什么。
注意:我目前正在将Xml中的数据作为字符串数组放入并在活动中将其放入Array中,但它很慢,从xml加载数据需要几秒/时,但这是第一次。
答案 0 :(得分: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的更多信息: