visit
implementation 'org.greenrobot:eventbus:3.2.0'
public class CartEvent {
public String cartItem;
public CartEvent(String cartItem) {
this.cartItem = cartItem;
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="//schemas.android.com/apk/res/android"
xmlns:app="//schemas.android.com/apk/res-auto"
xmlns:tools="//schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cart Item: 0"
android:id="@+id/cartTextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="50sp"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Items"
android:id="@+id/button"
android:onClick="open"
app:layout_constraintTop_toBottomOf="@id/cartTextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
See Also:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="//schemas.android.com/apk/res/android"
xmlns:app="//schemas.android.com/apk/res-auto"
xmlns:tools="//schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15sp"
tools:context=".MainActivity2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Item 1"
android:layout_marginTop="25sp"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/addItem1"
android:text="Add to cart"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/item1"
app:layout_constraintBottom_toBottomOf="@id/item1"
android:onClick="addItemToCart"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/item1"
android:text="Item 2"
android:layout_marginTop="25sp"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/addItem2"
android:text="Add to cart"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/item2"
app:layout_constraintBottom_toBottomOf="@id/item2"
android:onClick="addItemToCart"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/item2"
android:text="Item 3"
android:layout_marginTop="25sp"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/addItem3"
android:text="Add to cart"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/item3"
app:layout_constraintBottom_toBottomOf="@id/item3"
android:onClick="addItemToCart"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
public void addItemToCart(View view) {
}
List<CartEvent> cartEventList = new ArrayList<>();
TextView cartTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cartTextView = findViewById(R.id.cartTextView);
}
@Subscribe
public void onCartItemAdd(CartEvent cartEvent){
cartEventList.add(cartEvent);
String cartTotalItems = "Cart Items: "+cartEventList.size();
cartTextView.setText(cartTotalItems);
Toast.makeText(this, "Item added to cart.", Toast.LENGTH_SHORT).show();
}
@Override
protected void onStart() {
super.onStart();
if (!EventBus.getDefault().isRegistered(this))
EventBus.getDefault().register(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
public void addItemToCart(View view) {
EventBus.getDefault().post(new CartEvent("new Cart Item"));
}
Previously published at