Android将参数从一个方法获取到其他类中的另一个方法

时间:2015-03-21 16:11:00

标签: android

Iam尝试在我的activity上设置searchview。当用户在searchview上键入search关键字时,我需要每次都将值传递给异步任务。 我想从此方法传递参数'query'

        public boolean onQueryTextChange(String query) {
                   //want this  'string query' for further processing..

                    return true;

到此LoadIdioms类>> String doinBackground->> 到这一行>> params.add(new BasicNameValuePair(“keyword”,query));

class LoadIdioms extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Loading IDIOMS. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting Idioms from url
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        //value captured from previous intent
        params.add(new BasicNameValuePair("keyword",query));     

  class LoadIdioms extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Loading IDIOMS. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting Idioms from url
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        //value captured from previous intent
        params.add(new BasicNameValuePair("keyword",searchkey));                    

我们如何轻松地做到这一点,帮助我的程序员..

3 个答案:

答案 0 :(得分:0)

将从该方法返回的布尔值传递给声明的字符串变量。然后你可以尝试在那个类中调用它。

答案 1 :(得分:0)

以这种方式将值传递到AsyncTask

LoadIdioms  task= new LoadIdioms();
task.execute(query);

以这种方式在doInBackground()的{​​{1}}方法中使用它

AsyncTask

答案 2 :(得分:0)

最后我决定改变我的编码方式

                public boolean onQueryTextChange(String query) {
                    searchkey=query;
                    new LoadIdioms().execute();
                    lv = getListView();


                    return true;

这里我在onquerytextchange方法中调用了异步任务类,因此每次发生文本更改时都会调用,而不是传递参数值。