0

mainActivity

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

    long ProductId = Long.parseLong(i.getStringExtra("product_id"));
    long CustomerId = 1;
    int sizeId = 1; // this is where i need int value to be passed
    int quantity = 201;
}

//here i am getting this id value successfully bt i want that value to be assigned to sideId int in on create method
@Override
public void onClickCheckbox(Integer id) {
    Log.d(TAG, String.valueOf(id));
}

interface

public interface OnSizeClickInterface {
 void onClickCheckbox(Integer id);
}

adapter

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    holder.checkBox.setText(pList.get(position).getCode());
    holder.checkBox.setOnClickListener(view -> onSizeClickInterface.onClickCheckbox(pList.get(position).getId()));

}

this is where i am passing the value everything works fine! as i mentioned i want that id value to be in on create method i hope i will get my answer soon! thanks

2 Answers2

1

Make sizeId as a global variable and assign the new value in the onClickCheckbox () of the MainActivity, and you can resume your task from there itself such as updating views.

MainActivity

private int sizeId = 1;

@Override
public void onClickCheckbox(Integer id) {
    Log.d(TAG, String.valueOf(id));
    sizeId = id;
    // update views if required 
    // Perform any other tasks if required
}
0

Interfaces can be implements with 2 way, One way you have already implemented and the other way you want to use inside specific method.

You have to change your code.

public int sizeId;// Either initialize sizeId=1
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_product_details);

    long ProductId = Long.parseLong(i.getStringExtra("product_id"));
    long CustomerId = 1;

    int quantity = 201;

    productSizeAdapter = new ProductSizeAdapter(this, pList, new OnSizeClickInterface() {
        @Override
        public void onClickCheckbox(int id) {
            sizeId = id;//this is where i am getting perfect value 
            //You should use sizeId here because it is click event.
            SizeQuantity sizeQuantity = new SizeQuantity(sizeId, quantity); 

        }
    });
    //If you want to use here than you have to initialized sizeId=1. But I don't think you want to use here, because it will be called only once not every time when clicked
    //SizeQuantity sizeQuantity = new SizeQuantity(sizeId, quantity); 

}
Keyur Nimavat
  • 3,595
  • 3
  • 13
  • 19