0
我正在加載自動完成文本視圖與數據寫入我的CSV文件,但該文件包含數以千計的數據,因此它需要大量的時間來加載,有什麼辦法以便它可以在後臺完成。 我已經使用異步任務,但無法正確實施。加載自動完成文本視圖花費大量時間
我的代碼:
ArrayAdapter<String> TitleAdapter;
protected Void doInBackground(Void... params) {
readTitle();
return null;
}
public void readTitle()
{
InputStream is = _context.getResources().openRawResource(
R.raw.title);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr, 8192);
try {
String test;
while ((test = br.readLine()) != null) {
String[] RowData = test.split(",");
String Title = RowData[0];
TitleAdapter.add(Title);
}
isr.close();
is.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
我的堆棧跟蹤:
03-01 15:06:35.396: ERROR/AndroidRuntime(572): FATAL EXCEPTION: AsyncTask #1
03-01 15:06:35.396: ERROR/AndroidRuntime(572): java.lang.RuntimeException: An error occured while executing doInBackground()
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.os.AsyncTask$3.done(AsyncTask.java:200)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at java.lang.Thread.run(Thread.java:1096)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.view.ViewRoot.requestLayout(ViewRoot.java:594)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.view.View.requestLayout(View.java:8125)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.view.View.requestLayout(View.java:8125)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.widget.AbsListView.requestLayout(AbsListView.java:993)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:790)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:31)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.widget.ArrayAdapter.notifyDataSetChanged(ArrayAdapter.java:247)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.widget.ArrayAdapter.add(ArrayAdapter.java:179)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at com.Salary.Others.ReadFileHelper.readJobTitle(ReadFileHelper.java:111)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at com.Salary.Others.ReadFileHelper.doInBackground(ReadFileHelper.java:57)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at com.Salary.Others.ReadFileHelper.doInBackground(ReadFileHelper.java:1)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
我認爲這是因爲在doinbackground我們不能做任何UI工作。 那麼,如何處理這個問題,有沒有其他的方法..