I have a ListView Adapter in which are names, prices and ImageButtons (of Cancel) for every product(Listview row), Listview has its own Onclick Event which is used to edit clicked Product/Row. My question is how should I pass a number(position) of ImageButtons that are in this ListView back to a Fragment so i can delete that row. its been bothering me for quite a while. Here is the code
Code from Adapters Class receiptListAdapter ( i can paste whole code if necessary )
...
ImageButton test = (ImageButton)view.findViewById(R.id.imgBtnDelete);
test.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
test.setTag(position); // this is working and i get the position from the ImageButtons inside dynamic Clickable ListView
}
});
...
Code from Fragment ReceiptItemsFragment (Where i want to get that number)
...
public void deleteProduct(int number){
//note: this is working if am calling it from the fragment, but i need it to call from adapter
receiptItemArrayList.remove(number);
TextView txtTotalAmmount = (TextView) getView().findViewById(
R.id.txtReceiptTotalAmmount);
double totalAmmount = getTotalAmmount(receiptItemArrayList);
txtTotalAmmount.setText("Sum: " + String.valueOf(totalAmmount));
receiptListAdapter.notifyDataSetChanged();
}
...
I tried
like this ((ReceiptItemsFragment)context).deleteProduct(position);
but i get Cannot cast from Context to ReceiptItemsFragment
I tried it static but then I cant run the code from Fragment. Should I try to use Interface to pass data and how (i know only how to do that between fragments and activity)? any suggestions?