Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to check whether the class being execute or not. Because it didnt give any value on inputName and inputEmail. The value is retrieve from the database.

This is part of the code (SingleSubject.java)

public class SingleSubject extends Activity {

EditText txtName;
EditText txtEmail;

String matrix_id;

// Progress Dialog
private ProgressDialog pDialog;

// JSON parser class
JSONParser jsonParser = new JSONParser();

// single product url
private static final String url_subject_details = "http://192.168.1.12/android_project/get_subject_details.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_STUDENT = "student";

private static final String TAG_MATRIX_ID = "matrix_id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.single_subject);


    // getting subject details from intent
    Intent i = getIntent();

    // getting matrix_id from intent
    matrix_id = i.getStringExtra(TAG_MATRIX_ID);
    TextView test = (TextView) findViewById(R.id.matrix_id);
    test.setText(matrix_id);

    // Getting complete matrix_id details in background thread
    new GetSubjectDetails().execute();


}

/**
 * Background Async Task to Get complete matrix_id details
 * */
class GetSubjectDetails extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(SingleSubject.this);
        pDialog.setMessage("Loading subject details. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }


    /**
     * Getting subject details in background thread
     * */
    protected JSONObject doInBackground(String... args) {

        JSONObject json = null;

        try {
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("matrix_id", matrix_id));
                json = jsonParser.makeHttpRequest(url_subject_details, "GET", params);

            }

        catch(Exception e)
            {
              e.printStackTrace();
            }   
        return json;

    }


    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(JSONObject json) {
        super.onPostExecute(json);

        // check your log for json response
        Log.d("Single Subject Details", json.toString());

        // json success tag
        success = json.getInt(TAG_SUCCESS);

        if (success.equals(1)) {
            // successfully received subject details
            JSONArray subjectObj = json.getJSONArray(TAG_STUDENT); // JSON Array

            // get first subject object from JSON Array
            JSONObject subject = subjectObj.getJSONObject(0);

        // subject with this matrix_id found
        // Edit Text
        txtName = (EditText) findViewById(R.id.inputName);
        txtEmail = (EditText) findViewById(R.id.inputEmail);

        // display subject data in EditText
        txtName.setText(subject.getString(TAG_NAME));
        txtEmail.setText(subject.getString(TAG_EMAIL));         

        }
        // dismiss the dialog once got all details      
        pDialog.dismiss();


    }
}

  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
187 views
Welcome To Ask or Share your Answers For Others

1 Answer

doInbackground is invoked on the background thread. No need to create a thread there

txtName = (EditText) findViewById(R.id.inputName);
txtEmail = (EditText) findViewById(R.id.inputEmail);
txtName.setText(subject.getString(TAG_NAME)); // updating ui not possible in doInbackground
txtEmail.setText(subject.getString(TAG_EMAIL));

Secondly you cannot update ui from a background thread. You need to update ui in onPostExecute .You can return result in doInbackground which is param to onPostExecute and update ui there.

You need to look at the docs of AsyncTask to understand more

http://developer.android.com/reference/android/os/AsyncTask.html

Edit:

Change this

 class GetSubjectDetails extends AsyncTask<String, String, String> {

to

 class GetSubjectDetails extends AsyncTask<String, String, JSONOBject> {

Then

 protected JSONObject doInBackground(String... params) {

            JSONObject json =null;            
            try {
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("matrix_id", matrix_id));
                json = jsonParser.makeHttpRequest(
                        url_subject_details, "GET", params);
                }catch(Exception e)
                {
                  e.printStacktrace();
                }   
    return json;
  }

Then

 @Override
 protected void onPostExecute(JSONObject json) {
    super.onPostExecute(json);
    pDialog.dismiss();
    // parse json
    // update ui  
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...