-4

I have two Java classes. Where I want to retrieve TextView data from DrinkDetail.java into Cart.java in order to save it into Firebase database. The TextView is deliveryOption and wanted to save in Cart.java from Requests table.

How can I do it? Below are my Java codes.

DrinkDetail.java

public class DrinkDetail extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drink_detail);

private void showAlertDialogChooseOptions() {
alertDialog = new AlertDialog.Builder(DrinkDetail.this);
    alertDialog.setTitle("Drop by our kiosk or rider deliver?");

    LayoutInflater inflater = this.getLayoutInflater();
    View takeaway_deliver = inflater.inflate(R.layout.takeaway_delivery, null);

    btnTakeAway = (FButton)takeaway_deliver.findViewById(R.id.btnTakeAway);
    btnDelivery = (FButton)takeaway_deliver.findViewById(R.id.btnDelivery);
    deliveryOption = (TextView)takeaway_deliver.findViewById(R.id.deliveryOptionChosen);

    alertDialog.setView(takeaway_deliver);
    alertDialog.create();

    btnTakeAway.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            deliveryOption.setText("Take Away");
            alertDialogTakeAway.show();
        }
    });
}

Cart.java:

public class Cart extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart);

private void showAlertDialog() {
    alertDialog2 = new AlertDialog.Builder(Cart.this);
    alertDialog2.setTitle("Cash on Delivery:");
    alertDialog2.setMessage("Choose amount of money will be given to the rider upon order arriving: ");

    LayoutInflater inflater2 = this.getLayoutInflater();
    View cash_on_delivery = inflater2.inflate(R.layout.cash_on_delivery, null);

    btnOrderNow = (FButton)cash_on_delivery.findViewById(R.id.btnOrderNow);
    btnOrderNow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Submit to Firebase
            Request request = new Request(
                    Common.currentUser.getPhone(),
                    Common.currentUser.getName(),
                    editAddress.getText().toString(),
                    txtTotalPrice.getText().toString(),
                    "0", //status
                    editComment.getText().toString(),
                    paymentMethod.getText().toString(),
                    moneySelected.getText().toString(),
                    //WANTED TO SAVE DELIVERY OPTION "TAKE AWAY" HERE,
                    cart
            );
            requests.child(String.valueOf(System.currentTimeMillis()))
                    .setValue(request);

            //Order placed
            alertDialogOrderPlaced.show();
            alertDialog2.setCancelable(true);
        }
    });

    alertDialog2.setView(cash_on_delivery);
    alertDialog2.setIcon(R.drawable.icons8_cash_in_hand_filled_50);
}
Gowtham Subramaniam
  • 3,358
  • 2
  • 19
  • 31

4 Answers4

1

Make a public method in the class you want to retrieve the data from. Like getData(), something similar to this:

public Data getData(){
   return this.data;
}
Goku
  • 9,102
  • 8
  • 50
  • 81
Ameer Taweel
  • 949
  • 1
  • 11
  • 37
1

Data can be stored using SharedPreferences. In your DrinkDetail.java save the data.

SharedPreferences prefs = this.getSharedPreferences("File", MODE_PRIVATE);
prefs.edit().putString("deliveryOption", deliveryOption.getText()).apply();

In your Cart.java you can retrieve it back.

SharedPreferences prefs = this.getSharedPreferences("File", MODE_PRIVATE);
String deliveryOption = prefs.getString("deliveryOption", "default");
Goku
  • 9,102
  • 8
  • 50
  • 81
iamvinitk
  • 165
  • 3
  • 15
1

If your using Intent to navigate from DrinkDetail to Cart then you can pass that string in Intent itself.

like this

Intent intent=new Intent(DrinkDetail.this, Cart.class);
intent.putExtra("Key","String value you want to pass");
startActivity(intent);

and can retrieve that string in Cart.java place below code in onCreate() method

String value;
if(getIntent()!=null) 
    value=getIntent().getStringExtra("Key");
Goku
  • 9,102
  • 8
  • 50
  • 81
Suraj Nair
  • 1,729
  • 14
  • 14
0

To send the data from one activity to another activity

DrinkDetail.java is first activity from where you want to send data to another activity

DrinkDetail.java

TextView aText = (TextView )findViewById(R.id.textview);
String text = aText .getText().toString();

Intent myIntent = new Intent(view.getContext(),Cart.java);
myIntent.putExtra("mytext",text);
startActivity(myIntent);

Cart.java is second activity which receive the Intent data whatever you pass from DrinkDetail.java

Cart.java

public class Cart extends Activity {

TextView mTextview;

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

    aTextview = (TextView)findViewById(R.id.textView);

    aTextview.setText(getIntent().getStringExtra("mytext"));
}
Goku
  • 9,102
  • 8
  • 50
  • 81
Gowtham Subramaniam
  • 3,358
  • 2
  • 19
  • 31