简体   繁体   English

如何从另一个类访问TextView

[英]How to acces TextView From another class

I've got my main startup class loading MainActivity but I'm trying to figure out how to access the TextView from another class which is loading information from a database. 我的主要启动类正在加载MainActivity,但我试图弄清楚如何从另一个类(从数据库加载信息)访问TextView。 I would like to publish that information to the 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");

I need to setTest from here and I have declear hallPrice from MainActivity help Please 我需要从这里设置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();
        }
    }
}

Use intent to pass data from one activity to another like this:- 使用意图将数据从一个活动传递到另一个活动,如下所示:-

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

And in your DateValidation Activity do:- 在您的DateValidation活动中执行:

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

hallPrice.setText(value);

You can pass the TextView in your AsyncTask. 您可以在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);

}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM