-2

I've 2 activities Acivity 1 and Activity 2. I'm passing values from Activity 1 to Activity 2. Code is given below:
Activity 1

@Override
    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

Intent intent = new Intent(Activity1.this, Activity2.class);
                    intent.putExtra("accountName", hashMaps.get(i).get("accountNumber"));
                    intent.putExtra("accountNumber", hashMaps.get(i).get("accountName"));

}  

Activity 2

    @Override
        protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_expand_collapse);
    Bundle b1 = getIntent().getExtras();
            if (b1 != null) {
                String accountName = b1.getString("accountName");
                String accountNumber = b1.getString("accountNumber");
                Log.d(Tag, "Values are " + accountName);
                Log.d(Tag, "Values are " + accountNumber);
            }
    } 

Here is the AsyncTask code:

private class getData extends AsyncTask<Void, Void, HashMap<String, String>> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(CardViewExpandCollapse.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected HashMap<String, String> doInBackground(Void... voids) {

            HttpHandler httpHandler = new HttpHandler();

            String s = httpHandler.makeServiceCall(mUrl);

            Gson gson = new Gson();
            AccountDetail[] accountDetails = gson.fromJson(s, AccountDetail[].class);

            ArrayList<AccountDetail> details = new ArrayList<>(Arrays.asList(accountDetails));

            HashMap<String, String> accDetails = new HashMap<>();

            String address = details.get(0).getRow().getBillToAddress();

            Double balance = details.get(0).getRow().getTotalAccountBalance();

            Double invoices = details.get(0).getRow().getTotalOpenInvoicesValue();

            Log.e(Tag, "Response from URL " + s);

            accDetails.put("billToAddress", address);
            accDetails.put("totalAccountBalance", balance + "\tUSD");
            accDetails.put("totalOpenInvoicesValue", invoices + "\tUSD");

            hashMap.add(accDetails); // size = 3

            return accDetails;
        }

        @Override
        protected void onPostExecute(HashMap<String, String> accDetails ) {
            super.onPostExecute(accDetails );
            if (pDialog.isShowing()) {
                pDialog.dismiss();
            }

            /*accnumber = (TextView) findViewById(R.id.txt_acc_num1);
            accname = (TextView) findViewById(R.id.txt_acc_name1);
            address = (TextView) findViewById(R.id.txt_address1);
            totalaccbal = (TextView) findViewById(R.id.txt_total_acc_bal_value1);
            openinvoice = (TextView) findViewById(R.id.txt_open_invoices_value1);*/

            Log.e(Tag, "Address is " + address);
//            Log.e(Tag, "Balance is " + balance);
//            Log.e(Tag, "Invoice is " + invoices);

            /*I want values of getExtras() added to TextView here*/


            address.setText(accDetails.get("billToAddress"));
            totalaccbal.setText(accDetails.get("totalAccountBalance"));
            openinvoice.setText(accDetails.get("totalOpenInvoicesValue"));

        }
    }

Above codes are working fine. There are some TextViews in Activity 2. I want to put accountName and accountNumber in those TextViews in onPostExecute() method. How can I do that?
i.e., I want values of getExtras() to be added to TextView in onPostExecute() method. How can I do that?

Ajay Kulkarni
  • 2,900
  • 13
  • 48
  • 97
  • @0X0nosugar: No, it isn't duplicate. I want to put values of `getExtras` to `TextView` – Ajay Kulkarni Jan 12 '18 at 18:08
  • You have a String value. No matter where it came from, by your code (and question text) you already seem to have it. You ask how to make the TextView display that String value. By following the link and reading the accepted answer you will find out how to set the text for a TextView. – Bö macht Blau Jan 12 '18 at 18:13
  • @0X0nosugar: Please check updated question – Ajay Kulkarni Jan 12 '18 at 18:14
  • When do you start the AsyncTask? – Code-Apprentice Jan 12 '18 at 18:15
  • I start AsyncTask when I want to pull data from server and put them in view. Now I want to put values of getExtras() in textview – Ajay Kulkarni Jan 12 '18 at 18:16
  • What's stopping you from starting your intent in `onPostExecute()` ? – Pulak Jan 12 '18 at 18:22
  • 1
    What I understood is, you are getting the strings from the `AsyncTask` which you want to set to some `TextView`s in Activity 2, then why not start the `AsyncTask` in activity 1 and let it start the activity when its done? – Pulak Jan 12 '18 at 18:24
  • @AjayKulkarni If all you want is to put the value of `b1.getString()` into a `TextView`, the linked duplicate and the answer below show you how. – Code-Apprentice Jan 12 '18 at 18:29

1 Answers1

0

After you call setContentView(R.layout.yourLayoutForActivity2); you can access the elements of that layout by calling findViewById(R.id.theIdOfTheTextView).

So for example, if in the layout of Activity 2 you have a TextView with id "textviewid", yo could do:

TextView myTextView = (TextView)findViewById(R.id.textviewid);
myTextView.setText("here goes your text"); //you can put the String variable in there

that's it

Sebastian Breit
  • 6,137
  • 1
  • 35
  • 53