将用户输入的值传递给url并显示结果
问题描述:
基本上我试图将用户输入的值传递给一个url并显示基于该结果的结果....作为即时通讯没有错误我不太确定什么错误。任何帮助是极大的赞赏将用户输入的值传递给url并显示结果
当我上的按钮没有任何反应单击......我有事件侦听增加等
package com.example.jsonrestclient;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.EditText;
public class MainActivity extends Activity {
protected TextView tv1;
protected TextView tv2;
protected TextView tv3;
protected TextView tv4;
protected EditText ET1;
protected String ET2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.textView1);
tv2 = (TextView) findViewById(R.id.textView2);
tv3 = (TextView) findViewById(R.id.textView3);
tv4 = (TextView) findViewById(R.id.textView4);
ET1 = (EditText) findViewById(R.id.editText1);
ET2 = ET1.getText().toString();
}
public void button1OnClick(View v) {
String patientString = HttpHandler.HttpGetExec(HttpHandler.baseURI + ET2);
String patientFName = "NOT FOUND",
patientLName = "NOT FOUND",
DOB = "NOT FOUND",
patientGender = "NOT FOUND",
hospitalNumber = "NOT FOUND";
JSONObject patientObj = null;
try {
patientObj = new JSONObject(patientString);
patientFName = (String) patientObj.get("PatientFName");
patientLName = (String) patientObj.get("PatientLName");
DOB = (String) patientObj.get("DOB");
patientGender = (String) patientObj.get("PatientGender");
hospitalNumber = (String) patientObj.get("HospitalNumber");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tv1.setText(patientFName + " " + patientLName);
tv2.setText("DOB" + " " + DOB);
tv3.setText("Gender" + " " + patientGender);
tv4.setText("Hospital" + " " + hospitalNumber);
}
}
答
由于njzk2提到,你还为时过早评估ET2。
你在onCreate()中这样做,这意味着在用户有机会在ET1中输入任何内容之前ET2已经被分配,所以它总是空的。
你需要做的是将ET2 = ET1.getText().toString();
下移,作为button1OnClick
的第一行。
推测button1OnClick
绑定到你的视图xml中的参数?
另外,什么是HttpHandler?我无法从您的进口中知道。我不确定它是什么,但它可能是值得考虑的其余呼吁转移到一个AsyncTask。
是你在xml中定义的按钮的onClick?如果是,则显示代码 – 2013-03-13 14:03:55
ET2在ET1中写入任何内容之前进行评估 – njzk2 2013-03-13 14:19:49