如何在适配器类和活动中一起使用Interface

时间:2018-06-28 19:51:54

标签: android

当按下一项RecyclerView中的移动按钮时,如何更新操作栏中的项目计数?

我的WishlistActivity

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wishlist);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        dbHelper = new DatabaseHelper(this);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("Wishlist");
        toolbar.setTitleTextColor(Color.WHITE);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);

        wishlistRecycler = (RecyclerView) findViewById(R.id.wishlist_recycler);
        GridLayoutManager layoutManager = new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false);
        wishlistRecycler.setLayoutManager(layoutManager);
        wishlistRecycler.addItemDecoration(new Space(2,20,true,0));
        adapter = new wishlistAdapter(itemsWish,getApplicationContext(),cartCounter);
        wishlistRecycler.setAdapter(adapter);

        wish_count = dbHelper.getWishCount();
        emptyLayout = (RelativeLayout) findViewById(R.id.emptyWishLayout);
        wishLayout = (RelativeLayout) findViewById(R.id.wishLayout);
        contShopp = (Button) findViewById(R.id.continueShope);

        cartCounter = new MoveWishlistToCart() {
            @Override
            public void setCounter(int count) {
                Toast.makeText(WishlistActivity.this, "Called", Toast.LENGTH_SHORT).show();
                Log.d("Log","cartCounter: "+count);
            }
        };

我的wishlistAdapter类

public wishlistAdapter(List<WishlistItems> wishItems, Context context, MoveWishlistToCart cartCounter) {
        this.wishItems = wishItems;
        this.context = context;
        this.cartCounter = cartCounter;
    }

    @NonNull
    @Override
    public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View rootView;
        rootView = LayoutInflater.from(context).inflate(R.layout.wish_list,null,false);

        return new myViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(@NonNull myViewHolder holder, final int position) {

        WishlistItems wishlistItems = wishItems.get(position);
        dbHelper = new DatabaseHelper(context);

        wish_sku = wishlistItems.getWishItemSku();
        price = wishlistItems.getWishItemPrice();
        sCount = dbHelper.getCountTotal();

        holder.textBrandName.setText(wishlistItems.getWishItemBrandName());
        holder.textPrice.setText("₹"+wishlistItems.getWishItemPrice());

        String imageUrl = wishItems.get(position).getWishImage();
        Picasso.with(context).load("http://www.app.ssmkanmanam.com/public/images/"+imageUrl).into(holder.mImageview);

        holder.mRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(activity, "Hii", Toast.LENGTH_SHORT).show();
                removeFromWishlistDb(position);
                wishItems.remove(position);
                notifyDataSetChanged();
            }
        });

        holder.textMoveToBag.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeFromWishlistDb(position);
                wishItems.remove(position);
                notifyDataSetChanged();
                moveToCartDb();

                cartCounter.setCounter(1);


            }
        });
    }

我的界面类

public interface MoveWishlistToCart {
    public void setCounter(int count);
}

1 个答案:

答案 0 :(得分:1)

之所以发生这种情况,是因为最初您将空MoveWishlistToCart传递给了适配器,然后又对其进行了初始化。移动代码:

cartCounter = new MoveWishlistToCart() {
        @Override
        public void setCounter(int count) {
            Toast.makeText(WishlistActivity.this, "Called", Toast.LENGTH_SHORT).show();
            Log.d("Log","cartCounter: "+count);
        }
    };

在此行上方

adapter = new wishlistAdapter(itemsWish,getApplicationContext(),cartCounter);