简体   繁体   中英

Android EditText field not functioning

I have a single activity in an android app which is supposed to connect to a servlet.I ahev two EditText fields and taking two inputs through editText_name.getText().toString method.But the app is unable to catch the input and print it in the Log.My code snippet is as follows

    public class ServletActivity extends Activity {

Button button;
    TextView outputText;
//public String URL = "http://10.0.2.2:8080/MyHL7Server /getPatientDetails?patientID";
public String PatientDataURL = "http://192.168.161.129:8080/MyHL7Server/getPatientDetails?patientID";
public String SensorListURL="http://192.168.161.129:8080/MyHL7Server/getSensorDetails?CenterID";

public String PatientInfo=new String();
 public String SensorList=new String();

public String PatientIdInput=new String();
 public String CenterIdInput=new String();
 private ProgressDialog progressMessage;
 int patientDataFlag,sensorDataFlag=0;

 EditText PatientIdInputField;
 EditText CenterIdInputField;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_servlet);

     //final  Button ConnectServerButton=(Button)findViewById(R.id.button2);

        button = (Button) findViewById(R.id.button);
        outputText = (TextView) findViewById(R.id.textView3);
        PatientIdInputField=(EditText)findViewById(R.id.editText1);
        CenterIdInputField=(EditText)findViewById(R.id.editText2);
        PatientIdInput=PatientIdInputField.getText().toString();
        CenterIdInput=CenterIdInputField.getText().toString();
        Log.e("PatientIdInput is",PatientIdInput);
        Log.e("CenterIdInput is",CenterIdInput);
    System.out.println("Inside Servlet Activity");
    Log.e("Testing","Servlet Activity");




    ;
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

                Log.e("Patient Id Input",PatientIdInput);
                Log.e("CenterId Input",CenterIdInput);
                PatientDataURL.concat("=").concat(PatientIdInput);

                Log.e("Debug PatientDataURL created",PatientDataURL);
                Log.e("Debug","Calling GetPatientDataTask");

                GetPatientDataTask patientTask = new GetPatientDataTask();
                patientTask.execute(new String[] { PatientDataURL });
                Log.e("PatientData",PatientInfo);
                Toast.makeText(getApplicationContext(),
                        "Patient data is : "+PatientInfo, Toast.LENGTH_LONG).show();    
                System.out.println("Patient Data Obtained "+PatientInfo);




        }
    });
}

The app is able to connect to the servlet through AsyncTask.But I am concerned with the userinput field remaining null even when I give some input.

Can anybody throw some light on this problem? Do I have to change anything in my AndroidManifest.xml file???

Any help will be appreciated.

You are fetching the data in onCreate() .

Move these lines in button's onClick event.

PatientIdInput=PatientIdInputField.getText().toString();
CenterIdInput=CenterIdInputField.getText().toString();

Hope you understand the difference.

Move you

PatientIdInput=PatientIdInputField.getText().toString();
    CenterIdInput=CenterIdInputField.getText().toString();

Calls into the button's onClick listener.

You are trying to get data in onCreate, but it will empty on start. You should try to read data from EditText on you onClick. Move this code into your click handler:

PatientIdInput=PatientIdInputField.getText().toString();
CenterIdInput=CenterIdInputField.getText().toString();
Log.e("PatientIdInput is",PatientIdInput);
Log.e("CenterIdInput is",CenterIdInput);
    PatientIdInputField=(EditText)findViewById(R.id.editText1);
    CenterIdInputField=(EditText)findViewById(R.id.editText2);
    PatientIdInput=PatientIdInputField.getText().toString();
    CenterIdInput=CenterIdInputField.getText().toString();

Move the code inside of:

button.setOnClickListener(new View.OnClickListener()

If You want the edit-text data when you click on button .

than put code for getting data from edit-text in button's click listener .

because your code is executed but, at that time it has no value so, you not get anything.

so, when you need the value of edit-text at that place of code you put edit-text value getting code .

  button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

            /* code executed when you click on button */

            PatientIdInput=PatientIdInputField.getText().toString();
            CenterIdInput=CenterIdInputField.getText().toString();

            Log.e("Patient Id Input",PatientIdInput);
            Log.e("CenterId Input",CenterIdInput);
            PatientDataURL.concat("=").concat(PatientIdInput);

            Log.e("Debug PatientDataURL created",PatientDataURL);
            Log.e("Debug","Calling GetPatientDataTask");

            GetPatientDataTask patientTask = new GetPatientDataTask();
            patientTask.execute(new String[] { PatientDataURL });
            Log.e("PatientData",PatientInfo);
            Toast.makeText(getApplicationContext(),
                    "Patient data is : "+PatientInfo, Toast.LENGTH_LONG).show();    
            System.out.println("Patient Data Obtained "+PatientInfo);
    }
});

And remove edit-text value getting code from onCreate() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM