如何检查类的存在是否执行或不 [英] How to check whether the class being execute or not

查看:92
本文介绍了如何检查类的存在是否执行或不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查类的存在是否执行或不。因为它没有提供关于inputName和inputEmail任何价值。的值是从数据库中检索。

这是的code(SingleSubject.java)部分

 公共类SingleSubject延伸活动{

的EditText txtName的;
的EditText txtEmail;

字符串matrix_id;

//进度对话框
私人ProgressDialog pDialog;

// JSON解析器类
JSONParser jsonParser =新JSONParser();

//单品链接
私有静态最后弦乐url_subject_details =htt​​p://192.168.1.12/android_project/get_subject_details.php;

// JSON节点名称
私有静态最后弦乐TAG_SUCCESS =成功;
私有静态最后弦乐TAG_STUDENT =学生;

私有静态最后弦乐TAG_MATRIX_ID =matrix_id;
私有静态最后弦乐TAG_NAME =名;
私有静态最后弦乐TAG_EMAIL =电子邮件;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.single_subject);


    //获取来自意图主题的详细信息
    意图I = getIntent();

    //获取来自意图matrix_id
    matrix_id = i.getStringExtra(TAG_MATRIX_ID);
    TextView的试验=(TextView中)findViewById(R.id.matrix_id);
    test.setText(matrix_id);

    //获取在后台线程完成matrix_id细节
    新GetSubjectDetails()执行();


}

/ **
 *背景异步任务以获得完整的matrix_id细节
 * * /
类GetSubjectDetails扩展的AsyncTask<字符串,字符串,字符串> {

    / **
     *在启动后台线程显示进度对话框
     * * /
    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
        pDialog =新ProgressDialog(SingleSubject.this);
        pDialog.setMessage(加载主题的详细信息,请稍候...);
        pDialog.setIndeterminate(假);
        pDialog.setCancelable(真正的);
        pDialog.show();
    }


    / **
     *获得在后台线程主题的详细信息
     * * /
    受保护的JSONObject doInBackground(字符串参数... args){

        JSONObject的JSON = NULL;

        尝试 {
                名单<的NameValuePair> PARAMS =新的ArrayList<的NameValuePair>();
                params.add(新BasicNameValuePair(matrix_id,matrix_id));
                JSON = jsonParser.makeHtt prequest(url_subject_details,GET,则params);

            }

        赶上(例外五)
            {
              e.printStackTrace();
            }
        返回JSON;

    }


    / **
     *在完成后台任务之后关闭该进度对话框
     * ** /
    保护无效onPostExecute(JSONObject的JSON){
        super.onPostExecute(JSON);

        //检查你的日志,JSON响应
        Log.d(单一主题详细信息,json.toString());

        // json的成功标签
        成功= json.getInt(TAG_SUCCESS);

        如果(success.equals(1)){
            //成功接收主体细节
            JSONArray subjectObj = json.getJSONArray(TAG_STUDENT); // JSON数组

            //从JSON数组的第一个主题对象
            的JSONObject受试者= subjectObj.getJSONObject(0);

        //受本matrix_id发现
        //编辑文本
        txtName的=(的EditText)findViewById(R.id.inputName);
        txtEmail =(EditText上)findViewById(R.id.inputEmail);

        在EditText上//显示目标数据
        txtName.setText(subject.getString(TAG_NAME));
        txtEmail.setText(subject.getString(TAG_EMAIL));

        }
        //关闭该对话框,一旦得到了所有细节
        pDialog.dismiss();


    }
}

  }
 

解决方案

doInbackground 的后台线程调用。无需创建一个线程有

  txtName的=(的EditText)findViewById(R.id.inputName);
txtEmail =(EditText上)findViewById(R.id.inputEmail);
txtName.setText(subject.getString(TAG_NAME)); //更新UI不可能在doInbackground
txtEmail.setText(subject.getString(TAG_EMAIL));
 

其次,你无法更新从后台线程的用户界面。您需要在 onPostExecute 更新用户界面。你可以在 doInbackground 返回的结果是参数为 onPostExecute 和更新用户界面那里。

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

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

编辑:

更改此

 类GetSubjectDetails扩展的AsyncTask<字符串,字符串,字符串> {
 

 类GetSubjectDetails扩展的AsyncTask<字符串,字符串,JSONObject的> {
 

然后

 受保护的JSONObject doInBackground(字符串... PARAMS){

            JSONObject的JSON = NULL;
            尝试 {
                名单<的NameValuePair> PARAMS =新的ArrayList<的NameValuePair>();
                params.add(新BasicNameValuePair(matrix_id,matrix_id));
                JSON = jsonParser.makeHtt prequest(
                        url_subject_details,GET,则params);
                }赶上(例外五)
                {
                  e.printStacktrace();
                }
    返回JSON;
  }
 

然后

  @覆盖
 保护无效onPostExecute(JSONObject的JSON){
    super.onPostExecute(JSON);
    pDialog.dismiss();
    //解析JSON
    //更新用户界面
}
 

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


    }
}

  }

解决方案

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  
}

这篇关于如何检查类的存在是否执行或不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆