我正在开发一个将英语单词翻译成波斯语的应用程序 当我把小数据库放在每个东西都顺利的时候。 但当我使用长数据库时UI冻结。 然后我使用Asynchronous在后台加载数据库,但我不能将执行方法的结果发送到Activity中的List View?
public class MainActivity extends ListActivity {
private database db;
private EditText ed_txt;
private TextView tv;
private RadioButton rb_en;
private RadioButton rb_fa;
private String[] searched_word;
private String[] en;
private String[] fa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db=new database(this, "dictionary", null, 1);
db.useable();
ed_txt=(EditText)findViewById(R.id.ed_txt);
tv=(TextView)findViewById(R.id.tv);
rb_en=(RadioButton)findViewById(R.id.rb_en);
rb_fa=(RadioButton)findViewById(R.id.rb_fa);
//refresher(ed_txt.getText().toString(), "en");
ed_txt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
if(rb_en.isChecked()){
refresher(ed_txt.getText().toString(), "en");
}
else if (rb_fa.isChecked()) {
refresher(ed_txt.getText().toString(), "per");
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
new AsynctaskClass(handler).execute();
}
});
}
public class AsynctaskClass extends AsyncTask<Void, Void, Void> {
private Handler handler;
public AsynctaskClass(Handler handler ){
this.handler = handler;
}
@Override
protected Void doInBackground(Void... params) {
Message message = handler.obtainMessage();
//response you wish to send to the listview as String object
message.obj =new String();
message.what = 1 ;//You can use any value which will help you to distinguish the Handler response received at the activity.
handler.sendMessage(message);
return null;
}
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
class AA extends ArrayAdapter<String>{
public AA(){
super(MainActivity.this,R.layout.row_search,en);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater in=getLayoutInflater();
View row=in.inflate(R.layout.row_search, parent, false);
TextView tv_searched_word=(TextView)row.findViewById(R.id.tv_searched_word);
tv_searched_word.setText(en[position]);
return (row);
}
}
private void refresher(String text, String field){
db.open();
int s = db.shmaresh_jostojoo(text, field);
if (ed_txt.getText().toString().equals("")) {
s = 0;
tv.setText(" لطفا کلمه مورد نطرتان را وارد کنید");
}else {
tv.setText(" تعداد "+s+" یافت شد ");
}
//searched_word[s];
en=new String[s];
fa=new String[s];
for (int i = 0; i < s; i++) {
//searched_word[i]=db.jostojoo(i, col, word, field);
if(field.equals("en")){
en[i]=db.jostojoo(i, 1, text, field);
}else{
en[i]=db.jostojoo(i, 2, text, field);
}
//en[i]=db.jostojoo(i, 1, text, field);
//fa[i]=db.jostojoo(i, 2, text, field);
}
setListAdapter(new AA());
db.close();
}
答案 0 :(得分:0)
在你的代码中,我看不到任何asynctask,但使用Handler可以解决你的问题。
在您的Activity中定义处理程序如下:
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
}
然后使用此处理程序对象并将其传递给asynctask,如下所示:
new AsynctaskClass(handler, other params as you need).execute();
现在你的asynctask看起来如下:
public class AsynctaskClass extends AsyncTask<Void, Void, Void> {
private Handler handler;
public AsynctaskClass(Handler handler, other params ){
this.handler = handler;
}
...
...
}
现在在Asynctask的 doInBackground()中,执行您喜欢的任何操作,然后当您获得要发送到listview的结果时,添加以下代码:
@Override
protected Void doInBackground(Void... params) {
Message message = handler.obtainMessage();
message.obj = response you wish to send to the listview as String object;
message.what = 1 //You can use any value which will help you to distinguish the Handler response received at the activity.
handler.sendMessage(message);
}
就是这样..现在当sendMessage()
将被调用asynctask ...在你的Acitivity中 handleMessage()将获得触发器..,因为你可以获得从asynctask as msg.obj.toString()
..现在使用此数据执行UI修改。
希望这会对你有所帮助。