EditText的值未出现在我的日志中

时间:2014-06-15 19:10:30

标签: java android

我是Android新手。我有以下用于Twitter客户端搜索的代码:

package com.michaelnares.twitterclient;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import twitter4j.*;
import twitter4j.conf.ConfigurationBuilder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * Created by michael on 01/05/2014.
 */
public class SearchActivity extends Activity {

    EditText queryEditText = null;
    String queryText = null;
    private Context context = this;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);
        queryEditText = (EditText) findViewById(R.id.queryEditText);
        queryText = (queryEditText.getText().toString());
        final Context context = this;

        Button queryButton = (Button) findViewById(R.id.queryButton);
        queryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(LogConstants.LOG, "Text value is " + queryText);
                if (queryText.equals(null)) {
                    Toast.makeText(context, "You did not enter a query", Toast.LENGTH_SHORT).show();
                } else {
                    new SearchAsyncTask().execute();
                    }
            }
        });
    } // ends onCreate()

    private class SearchAsyncTask extends AsyncTask<String, String, ArrayList<String>>
    {

       private ProgressDialog dialog = new ProgressDialog(SearchActivity.this);
        private ArrayList<String> searchResults;

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            dialog.setMessage("Getting data...");
            dialog.setIndeterminate(false);
            dialog.setCancelable(true);
            dialog.show();
        }

        @Override
        protected ArrayList<String> doInBackground(String... strings) {
            APIClient client = new APIClient();
            searchResults = client.getQueryResults(queryText);
            return searchResults;
        }

        @Override
        protected void onPostExecute(ArrayList<String> results)
        {
            dialog.dismiss();
            results.addAll(searchResults);
            if (searchResults == null)
            {
                Log.w(LogConstants.LOG, "searchResults is null");
            }
            final ListView searchListView = (ListView) findViewById(R.id.searchListView);
            final ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_list_item_1, results);
            adapter.notifyDataSetChanged();
            searchListView.setAdapter(adapter);
        }
    }
} //ends class

问题是,我试图记录EditText的结果,但是我从未在日志中看到过。这里发生了什么?我无法弄清楚为什么我从未在日志中看到“Text value is”+ queryText。

3 个答案:

答案 0 :(得分:0)

更改

 Log.i(LogConstants.LOG, "Text value is " + queryText);

 Log.i(LogConstants.LOG, "Text value is " + queryEditText.getText().toString());

答案 1 :(得分:0)

移动         queryText = (queryEditText.getText().toString());

在onClick

    queryButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            queryText = (queryEditText.getText().toString());
            Log.i(LogConstants.LOG, "Text value is " + queryText);
            if ("".equals(queryText)) {
                Toast.makeText(context, "You did not enter a query", Toast.LENGTH_SHORT).show();
            } else {
                new SearchAsyncTask().execute();
                }
        }
    });

答案 2 :(得分:0)

问题是如果正在创建屏幕(onCreate()),则初始化String值queryText,而不是在实际输入值时(屏幕创建和屏幕上的动态活动之后)。

这就是以下可行的原因

     @Override
                public void onClick(View view) {
 queryText = (queryEditText.getText().toString());
                    Log.i(LogConstants.LOG, "Text value is " + queryText);
                    if (queryText.equals("")) {
                        Toast.makeText(context, "You did not enter a query", Toast.LENGTH_SHORT).show();
                    } else {
                        new SearchAsyncTask().execute();
                        }
                }