First, I know this has been answered here : Android communication between fragment and baseadapter and I tried implementing it, but as it is my first time using interfaces and do not get the logic behind it, it did not work. My problem is I would like to set the value of the TextView of this fragment
Fragment.java
public class FragmentCart extends Fragment {
private TextView totalTxt;
private TextView totalItems;
private ListView itemList;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_cart, container, false);
totalTxt = (TextView)view.findViewById(R.id.totalTxt);
totalItems = (TextView)view.findViewById(R.id.totalItems);
itemList = (ListView)view.findViewById(R.id.itemList);
itemList.setAdapter(MainActivity.cart);
return view;
}
}
with the value (double totalCost) from this adapter
Adapter.java
public class Cart extends BaseAdapter {
private double totalCost = 0;
public getView() {
Item item.....
totalCost += item.getPrice();
}
}
The logic is that, as I add items to the cart (adapter) the TextView from the fragment updates as well. How do I do this with interface?