将runOnUiThread转换为AsyncTask [英] Converting runOnUiThread to AsyncTask
问题描述
我阅读了一些将runOnUiThread转换为AsyncTask所需的链接: Android:RunOnUiThread与AsyncTask
I read in some links that i need to convert my runOnUiThread to AsyncTask: Android: RunOnUiThread vs AsyncTask
但是我无法完成它.我正在实现一个AutoCompleteText,它可以从数据库进行查询.
But I am unable to get it done. I am implementing an AutoCompleteText which takes query from a database.
我的runOnUiThread以及新线程(它会编译):
My runOnUiThread along with the new thread (it compiles):
new Thread(new Runnable() {
public void run() {
final DataBaseHelper dbHelper = new DataBaseHelper(ActivityName.this);
dbHelper.openDataBase();
item_list = dbHelper.getAllItemNames();
ActivityName.this.runOnUiThread(new Runnable() {
public void run() {
ArrayAdapter<String> sAdapter = new ArrayAdapter<String>(
ActivityName.this,
android.R.layout.simple_dropdown_item_1line,
item_list);
itemNameAct = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView_item_name);
itemNameAct.setAdapter(sAdapter);
}
});
}
}).start();
我将工作线程部分放在doInBackground中,将代码的runOnUiThread部分放在onPostExecute中,但是它在启动时崩溃.
I put the worker thread part in doInBackground and the runOnUiThread part of code in onPostExecute but it crashes on launch.
推荐答案
这很尴尬.我问了一个问题,并亲自回答了这个问题:/ 其实我在尝试AsyncTask(Object,Void,Cursor),但对我没有任何好处.
This is awkward. I asked a question and am answering it myself :/ Actually I was trying AsyncTask(Object, Void, Cursor) and it was not doing me any good.
这是正在上课的班级
class autoComplete extends AsyncTask<Void, Void, Void> {
final DataBaseHelper dbHelper = new DataBaseHelper(ActivityName.this);
@Override
protected Void doInBackground(Void... params) {
dbHelper.openDataBase();
item_list = dbHelper.getAllItemNames();
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ArrayAdapter<String> sAdapter = new ArrayAdapter<String>(
ClassName.this, android.R.layout.simple_dropdown_item_1line,
item_list);
itemNameAct= (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView_item_name);
itemNameAct.setAdapter(sAdapter);
}
}
然后在onCreate中将其初始化为:
and then in onCreate i initialize it as:
new autoComplete().execute();
这篇关于将runOnUiThread转换为AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!