0

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?

Chain Cross
  • 351
  • 1
  • 2
  • 12

1 Answers1

0

To answer your question directly :

In Adapter.java declare a new interface that we'll call TotalCostUpdater:

public interface TotalCostUpdater {
    void onTotalCostUpdated(int totalCost);
}

Then, still in your adapter, you need a field implementing this interface, initialised through a constructor. From there you can call onTotalCostUpdated(totalCost) after you updated the value.

Your Adapter.java class will then look like this :

public class Cart extends BaseAdapter 
{

    public interface TotalCostUpdater {
        void onTotalCostUpdated(int totalCost);
    }

    private double totalCost = 0;

    private TotalCostUpdater costUpdater;

    public Cart(TotalCostUpdater costUpdater) {
        this.costUpdater = costUpdater;
    }

    public getView() 
    {
        Item item.....
        totalCost += item.getPrice();
        costUpdater.onTotalCostUpdated(totalCost);
    }
}

You then need to implement TotalCostUpdater, and override its onTotalCostUpdated method in FragmentCart.java :

public class FragmentCart extends Fragment implements Cart.TotalCostUpdater 
{
    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;
    }

    @Override
    public void onTotalCostUpdated(int totalCost) {
        totalTxt.setText(totalCost);
    }
}

When instantiating your adapter (at some point your must have written a new Cart()), you need to pass in your FragmentCart, which is now implementing the interface expected in your Cart's constructor :

new Cart(fragmentCart);

This should do the trick. Here are a few other things you should consider :

  • The adapter should not be the one holding the data, you should rather have a CartData class holding the actual cart values.
  • The adapter seems to be global from your MainActivity.cart. You should instantiate it directly within the Fragment class
  • You should use ButterKnife lib to get rid of all those findViewByIdcalls.
ArcDexx
  • 453
  • 5
  • 15