【问题标题】:Communication between BaseAdapter and FragmentBaseAdapter 和 Fragment 之间的通信
【发布时间】:2018-08-01 14:56:58
【问题描述】:

首先,我知道这里已经回答了这个问题:Android communication between fragment and baseadapter 并且我尝试实现它,但由于这是我第一次使用接口并且没有了解它背后的逻辑,所以它不起作用。我的问题是我想设置这个片段的 TextView 的值

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;
    }
}

使用此适配器的值 (double totalCost)

Adapter.java

public class Cart extends BaseAdapter {
    private double totalCost = 0;

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

逻辑是,当我将项目添加到购物车(适配器)时,片段中的 TextView 也会更新。我如何使用界面执行此操作?

【问题讨论】:

    标签: java android


    【解决方案1】:

    直接回答你的问题:

    Adapter.java 中声明一个我们将调用的新接口TotalCostUpdater

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

    然后,仍然在您的适配器中,您需要一个实现此接口的字段,并通过构造函数进行初始化。更新值后,您可以从那里致电onTotalCostUpdated(totalCost)

    您的 Adapter.java 类将如下所示:

    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);
        }
    }
    

    然后您需要实现TotalCostUpdater,并在FragmentCart.java 中覆盖其onTotalCostUpdated 方法:

    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);
        }
    }
    

    当实例化你的适配器时(在某些时候你必须写一个new Cart()),你需要传入你的FragmentCart,它现在正在实现你Cart的构造函数中预期的接口:

    new Cart(fragmentCart);
    

    这应该可以解决问题。以下是您应该考虑的其他一些事项:

    • 适配器不应该是保存数据的适配器,您应该有一个 CartData 类来保存实际的购物车值。
    • 从您的MainActivity.cart 看来,适配器似乎是全局的。您应该直接在 Fragment 类中实例化它
    • 您应该使用ButterKnife lib 来摆脱所有那些findViewByIdcalls。

    【讨论】:

      猜你喜欢
      • 2015-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多