从异步任务获取结果到列表视图

get result from asynctask to list view

我正在开发一款将英语单词翻译成波斯语的应用程序 当我放置小型数据库时,一切顺利。 但是当我使用长数据库时 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();
}

https://github.com/mohammadi66/Dictionary-En-2-Fa.git

在你的代码中我看不到任何异步任务,但使用 Handler 可能会解决你的问题。

在您的 Activity 中定义处理程序如下:

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 的 doInBackground() 中,执​​行任何你喜欢的操作,然后当你得到你想要发送到列表视图的结果时添加以下代码:

@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 发送的数据作为 msg.obj.toString().. 现在使用此数据执行 UI 修改。

希望对您有所帮助。