0

我试图创建 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
 * */

@Override 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);
}

}

}

对于发布这个冗长的代码,我深表歉意。

谢谢

4

1 回答 1

0

我认为问题就在这里:在doInBackground ()意见开始之前执行。第二件事是,您应该将所有 UI 更改放入onPostExecute()doInBackground()用于应该在后台执行的长时间操作。.

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

于 2013-05-23T16:44:39.333 回答