如何从另一个类访问TextView

时间:2018-07-08 15:19:40

标签: java android

  

我的主要启动类正在加载MainActivity,但我试图弄清楚如何从另一个类(从数据库中加载信息)访问TextView。我想将该信息发布到TextView。

private class DateValidation extends AsyncTask<String, Void, String> {


         @Override
         protected void onPreExecute() {
             super.onPreExecute();
             //Showing progress dialog


         }

        @Override
        protected String doInBackground(String... arg0) {
            String hallId = arg0[0];
            String date = arg0[1];


            String link;
            String data;
            BufferedReader bufferedReader;
            String result;

            try {
                data = "?id=" + URLEncoder.encode(hallId, "UTF-8");
                data += "&date=" + URLEncoder.encode(date, "UTF-8");


                link = "https://www.adoetech.co.tz/ehall/frontend/index.php/hall/validate-date" + data;
                URL url = new URL(link);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();

                bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                result = bufferedReader.readLine();
                Log.d("postData: ", link);
                return result;
            } catch (Exception e) {
                return "Exception: " + e.getMessage();
            }
        }

        @Override
        protected void onPostExecute(String result) {
            if (result != null) {
                try {
                    JSONObject jsonObj = new JSONObject(result);

                    boolean query_result = jsonObj.getBoolean("success");
                    String response = jsonObj.getString("data");
                    if (query_result) {

                        Toast.makeText(HallsDetails.this, response, Toast.LENGTH_LONG).show();
                    } else if (!query_result) {
                        Log.d("onPostExecute: ", "free");
                        Toast.makeText(HallsDetails.this, response, Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(HallsDetails.this, response, Toast.LENGTH_SHORT).show();
                        //JSONArray dateVal = jsonObj.getJSONArray("data");
  

我需要从这里设置setTest,并且从MainActivity帮助中清除了HallPrice,请

                    hallPrice.setText("300000000");


                }
            } catch (JSONException e) {
                e.printStackTrace();
                Log.e("onPostExecute: ", String.valueOf(result));
                Toast.makeText(HallsDetails.this, "Error parsing JSON data.", Toast.LENGTH_LONG).show();

            }
        } else {
            Toast.makeText(HallsDetails.this, "Couldn't get any JSON data.", Toast.LENGTH_SHORT).show();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

使用意图将数据从一个活动传递到另一个活动,如下所示:-

Intent in = new Intent(MainActivity.this, DateValidation.class);
in.putExtra("key", "300000000");
startActivity(in);

然后在您的DateValidation活动中执行:-

Intent i = getIntent();
String value = i.getStringExtra("key");

hallPrice.setText(value);

答案 1 :(得分:0)

您可以在AsyncTask中传递TextView。

public class DateValidation  extends AsyncTask<String,String,String> {

TextView mTextView;

public DateValidation (TextView textView){

    mTextView = textView;
}
@Override
protected String doInBackground(String... strings) {
    /**
     * do process here
     */
    return "Result String here";
}

@Override
protected void onPostExecute(String result) {

    mTextView.setText(result);

}

}