如何在Textview中放置另一个活动

时间:2018-01-12 17:59:53

标签: android textview

我有2项活动Acivity 1Activity 2。我将值从Activity 1传递到Activity 2。代码如下:
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);
            }
    } 

这是AsyncTask代码:

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

        }
    }

以上代码工作正常。 Activity 2中有一些TextView。我想将accountNameaccountNumber放在onPostExecute()方法的那些TextView中。我怎么能这样做?
即,我想在onPostExecute()方法中将getExtras()的值添加到TextView中。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

调用setContentView(R.layout.yourLayoutForActivity2)后; 您可以通过调用findViewById(R.id.theIdOfTheTextView)来访问该布局的元素。

例如,如果在Activity 2的布局中你有一个id为“textviewid”的TextView,你可以这样做:

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

就是这样