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 ()。ビューが開始される前に実行されます。2 つ目は、すべての 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;
}

あなたの場合、メソッドが a (WS 呼び出しから取得したもの) をdoInBackground()返すようにし、メソッドで JSON からのデータをビューに表示します。( )JSONObjectonPostExecute()EditTexts

于 2013-05-23T16:44:39.333 に答える