Android 从asynctask到列表视图获取结果

Android 从asynctask到列表视图获取结果,android,android-asynctask,Android,Android Asynctask,我正在开发一个将英语单词翻译成波斯语的应用程序 当我把小数据库,一切顺利。 但当我使用长数据库时,UI冻结。 然后我在后台使用异步加载数据库,但我无法将executioner方法的结果发送到Activity中的列表视图 public class MainActivity extends ListActivity { private database db; private EditText ed_txt; private TextView tv; private RadioButton rb_

我正在开发一个将英语单词翻译成波斯语的应用程序 当我把小数据库,一切顺利。 但当我使用长数据库时,UI冻结。 然后我在后台使用异步加载数据库,但我无法将executioner方法的结果发送到Activity中的列表视图

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();
}
public类MainActivity扩展了ListActivity{
专用数据库数据库;
私人编辑文本ed_txt;
私家图文电视;
私人无线按钮;
专用无线按钮rb_fa;
私有字符串[]搜索到的单词;
私有字符串[]en;
私有字符串[]fa;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db=新数据库(此“字典”,null,1);
db.useable();
ed_txt=(EditText)findViewById(R.id.ed_txt);
tv=(TextView)findviewbyd(R.id.tv);
rb_en=(单选按钮)findViewById(R.id.rb_en);
rb_fa=(单选按钮)findViewById(R.id.rb_fa);
//刷新器(ed_txt.getText().toString(),“en”);
ed_txt.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列arg0、int arg1、int arg2、int arg3){
如果(rb_en.isChecked()){
刷新器(ed_txt.getText().toString(),“en”);
}
else if(rb_fa.isChecked()){
刷新器(ed_txt.getText().toString(),“per”);
}
}
@凌驾
更改前的公共void(字符序列arg0、int arg1、int arg2、,
int arg3){
//TODO自动生成的方法存根
}
@凌驾
public void PostTextChanged(可编辑arg0){
//TODO自动生成的方法存根
新建AsynctaskClass(处理程序).execute();
}
});
}
公共类AsynctaskClass扩展了AsyncTask{
私人经办人;
公共AsynctaskClass(处理程序){
this.handler=handler;
}
@凌驾
受保护的Void doInBackground(Void…参数){
Message Message=handler.obtainMessage();
//要作为字符串对象发送到listview的响应
message.obj=新字符串();
message.what=1;//您可以使用任何有助于区分在活动中收到的处理程序响应的值。
handler.sendMessage(message);
返回null;
}
}
Handler=newhandler(){
@凌驾
公共无效handleMessage(消息消息消息){
超级handleMessage(msg);
}
};
AA类扩展阵列适配器{
公共机管局(){
super(MainActivity.this,R.layout.row\u search,en);
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
LayoutInflater in=getLayoutInflater();
查看行=in.充气(R.layout.row\u搜索,父项,false);
TextView电视搜索词=(TextView)row.findViewById(R.id.tv搜索词);
tv_search_word.setText(en[位置]);
返回(行);
}
}
专用无效刷新器(字符串文本、字符串字段){
db.open();
int s=db.shmaresh_jostojoo(文本,字段);
if(ed_txt.getText().toString().equals(“”){
s=0;
tv.setText(“电视台”);
}否则{
tv.setText(“تعد㶧دد”+s+“ی㶧㶬د”);
}
//搜索单词[s];
en=新字符串[s];
fa=新字符串[s];
对于(int i=0;i

在您的代码中,我看不到任何异步任务,但使用处理程序可以解决您的问题

在活动中,按如下方式定义处理程序:

Handler handler = new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                    }
 }
  new AsynctaskClass(handler, other params as you need).execute();
public class AsynctaskClass extends AsyncTask<Void, Void, Void> {

    private Handler handler;
    public AsynctaskClass(Handler handler, other params ){
     this.handler = handler;
   }
   ...
   ...
}
然后使用此处理程序对象并将asynctask传递给它,如下所示:

Handler handler = new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                    }
 }
  new AsynctaskClass(handler, other params as you need).execute();
public class AsynctaskClass extends AsyncTask<Void, Void, Void> {

    private Handler handler;
    public AsynctaskClass(Handler handler, other params ){
     this.handler = handler;
   }
   ...
   ...
}
现在,您的异步任务将如下所示:

Handler handler = new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                    }
 }
  new AsynctaskClass(handler, other params as you need).execute();
public class AsynctaskClass extends AsyncTask<Void, Void, Void> {

    private Handler handler;
    public AsynctaskClass(Handler handler, other params ){
     this.handler = handler;
   }
   ...
   ...
}
就是这样..现在当在asynctask内调用
sendMessage()
时..在您的活动中handleMessage()将获取触发器..,因为您可以获取从asynctask发送的数据作为
msg.obj.toString()
。现在使用此数据执行UI修改


希望这能对您有所帮助。

我做了一些像您所说的,但我有点困惑?您能在git hub上添加问题吗?