如何检查类是否正在执行

时间:2014-03-25 11:39:45

标签: java android

我想检查该类是否正在执行。因为它没有给inputName和inputEmail赋予任何价值。该值是从数据库中检索的。

这是代码的一部分(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();


    }
}

  }

1 个答案:

答案 0 :(得分:1)

在后台线程上调用

doInbackground。无需在那里创建一个线程

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));

其次,你无法从后台线程更新ui。您需要在onPostExecute中更新ui。您可以将doInbackground中的结果返回到onPostExecute的param,并在那里更新ui。

您需要查看AsyncTask的文档以了解更多

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

编辑:

更改此

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

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

然后

 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;
  }

然后

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