0

I am very new to Android Studio, and have found myself stuck in this concept. I am attempting to pass Price + Name data to a Cart activity upon a button press (Add to Cart).

After attempting intent methods, it seems that after pressing "Add to Cart", the cart is opened with the data, but the data is not saved in the new activity for more additions.

Right now I have the following:

 Button button = (Button) findViewById(R.id.addcart);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //How to pass information here
        }
    });

I am hoping to pass textView6 and textView7 to the cart activity. If possible, I would be interested in passing the image as well! Any start on this would be appreciated. Thank you!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
ttattini
  • 196
  • 1
  • 17

3 Answers3

1

To pass data trhoug activities you can use the same intent you use to open the new activity. You can set extras like this:

Intent i = new Intent(context, CartActivity.class);
i.putExtra("price", textView6.getText().toString());
i.putExtra("name", textView7.getText().toString());
startActivity(i);

And then in onCreate() of the just created activity you can retrieve this data getting the intent used to open this activity and getting its extras:

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   Intent data = getIntent();
   String price = data.getStringExtra("price");
   String name = data.getStringExtra("name");
}

Hope this help.

Marc Estrada
  • 1,657
  • 10
  • 20
0

When you create your Intent you have to do :

Intent i = new Intent(this, ToClass.class);
i.putExtra("someName", findViewById(R.id.textView6 ).getText().toString());
i.putExtra("someName2", findViewById(R.id.textView7 ).getText().toString());
startActivity(i); 

And then in your second Activity, use :

Intent intent = getIntent();
String someName= intent.getStringExtra("someName");
String someName2= intent.getStringExtra("someName2");
Gazouu
  • 372
  • 3
  • 16
  • "Cannot Resolve Method, getText" – ttattini May 10 '18 at 16:29
  • If I have Multiple "Add to Cart" Buttons on Multiple separate activities, then would I just make several unused "Strings" waiting to getString in the cart class? – ttattini May 10 '18 at 16:30
  • @user2592356 Then you need a kind of database, try to edit your first post with what you really need – Gazouu May 10 '18 at 16:54
0

You say your using an intent, but what are you doing with the values? Are you saving them somewhere in the CartActivity?

For the image just pass a reference or name of the image. No need to pass the whole PNG... and again use an intent putExtra() call.

Dave
  • 867
  • 6
  • 11
  • Yes, my goal is to push the "Price" and "Name" value on the corresponding page (With the Add to Cart button), to the Cart Page. However, I need these values to stick in the Cart Page once they are passed, as the user may add the item to the Cart again, or may add a different item to the cart – ttattini May 10 '18 at 16:32
  • Thats a different problem. For that you need a data store of some kind. Write out a file or keep in memory in a singleton class. Lots of ways to save state. – Dave May 10 '18 at 16:55