我试图创建3个标签。每个选项卡都将从数据库加载数据并显示它。 请在下面的代码中告诉我这个问题。我对片段感到困惑。
public class PatientDetail extends Fragment {
public static Context ctx;
EditText id;
EditText name;
EditText age;
EditText gender;
EditText room;
EditText bed;
EditText email;
EditText phone;
EditText adate;
EditText address;
String pid = "100";
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
// single product url
private static final String url_product_detials = "http://10.105.87.29/get_patient_details.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCT = "product";
private static final String TAG_ID = "patient_id";
private static final String TAG_NAME = "patient_name";
private static final String TAG_ROOM = "room_no";
private static final String TAG_BED = "bed_no";
private static final String TAG_EMAIL = "email";
private static final String TAG_PHONE ="phone";
private static final String TAG_AGE ="age";
private static final String TAG_GENDER ="gender";
private static final String TAG_DATE ="admission_date";
private static final String TAG_ADDRESS ="address";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
new GetPatientDetails().execute();
return (RelativeLayout) inflater.inflate(R.layout.patient_details, container, false);
}
类GetPatientDetails扩展了AsyncTask {
/**
* Before starting background thread Show Progress Dialog
* */
@覆盖 protected void onPreExecute(){ super.onPreExecute(); }
/**
* Getting product details in background thread
* */
protected String doInBackground(String... params) {
// updating UI from Background Thread
((Activity)ctx).runOnUiThread(new Runnable() {
public void run() {
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("patient_id",pid.toString()));
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(
url_product_detials, "GET", params);
// check your log for json response
Log.d("Single Product Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray productObj = json
.getJSONArray(TAG_PRODUCT); // JSON Array
// get first product object from JSON Array
JSONObject product = productObj.getJSONObject(0);
// product with this pid found
// Edit Text
name = (EditText)getView().findViewById(R.id.name);
age = (EditText)getView().findViewById(R.id.age);
gender = (EditText)getView().findViewById(R.id.gender);
id = (EditText)getView().findViewById(R.id.id);
email = (EditText)getView().findViewById(R.id.email);
phone = (EditText)getView().findViewById(R.id.phone);
adate = (EditText)getView().findViewById(R.id.adate);
room = (EditText)getView().findViewById(R.id.room);
bed = (EditText)getView().findViewById(R.id.bed);
address = (EditText)getView().findViewById(R.id.address);
// display product data in EditText
name.setText(product.getString(TAG_NAME));
age.setText(product.getString(TAG_AGE));
gender.setText(product.getString(TAG_GENDER));
id.setText(product.getString(TAG_ID));
email.setText(product.getString(TAG_EMAIL));
phone.setText(product.getString(TAG_PHONE));
adate.setText(product.getString(TAG_DATE));
room.setText(product.getString(TAG_ROOM));
bed.setText(product.getString(TAG_BED));
address.setText(product.getString(TAG_ADDRESS));
}else{
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
// pDialog.dismiss();
super.onPostExecute(file_url);
}
}
}
我为发布这个冗长的代码而道歉。
由于
答案 0 :(得分:0)
我认为问题在于:doInBackground ()
在视图启动之前执行。
第二件事是, 您应该将所有用户界面更改放在onPostExecute()
中,doInBackground()
用于应在后台执行的长时间操作。 。< / p>
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.patient_details, container, false);
new GetPatientDetails().execute();
return view;
}
在您的情况下,让您的doInBackground()
方法返回JSONObject
(您从WS调用中获得),然后在onPostExecute()
方法中,从JSON显示您的数据进入你的观点。(EditTexts
)