从asynctask获取listactivity的结果

时间:2015-03-22 12:04:28

标签: android android-asynctask loader

我想从asynctask到listactivity获得结果,但它没有响应 我使用轻量级数据库而没有asynctask每一件事情都顺利但是当我使用长数据库的UI freez然后我决定使用Asynctask。 这是我的代码:

public class MainActivity extends ListActivity {

public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;

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) {



        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {


        }

        @Override
        public void afterTextChanged(Editable arg0) {
            /*if(rb_en.isChecked()){
                refresher(ed_txt.getText().toString(), "en");

            }
            else if (rb_fa.isChecked()) {
                refresher(ed_txt.getText().toString(), "per");

            }*/
            loader l=new loader();
            l.execute("Param 1","Param 2","param 3");

        }
    });



}




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);
        /*Thread t=new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub

            }
        }).start();*/
        tv_searched_word.setText(en[position]);

        return (row);
    }

}

@Override
protected Dialog onCreateDialog(int id) {

switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("loading");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}//end onCreatDialog

class loader extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... aurl) {

try {
    refresher(ed_txt.getText().toString(), "en");
    //setListAdapter(new AA());
}
catch (Exception e) {
    e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC", progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String result) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
setListAdapter(new AA());

}
}

protected void updatedisplay(String result){
    setListAdapter(new AA());
}

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();
}

1 个答案:

答案 0 :(得分:0)

试试,

adapter.notifyDataSetChanged();
在postExecute方法中