如何从适配器在主要活动中从接口设置中获取值

问题描述 投票:0回答:1

我如何从回收者视图适配器到其父活动获取购物车项目数量。

定义界面,在回收者视图适配器中设置项目计数并在主要活动中实现界面

interface OnItemClick {
        void onClick (String value);
}


private OnItemClick mCallback;
 public VegeHomeAdapter(Context ctx, ArrayList<ModelTopProd> mData, OnItemClick listener) {
        this.mData = mData;
        this.ctx = ctx;
        this.mCallback = listener;
    }
int total_items = Integer.parseInt(preferenceHelper.getCARTNO());

                                total_items+=1;
                                preferenceHelper.putCARTNO(""+total_items);
                                mCallback.onClick(""+preferenceHelper.getCARTNO());


     public class Home extends AppCompatActivity implements View.OnClickListener, OnItemClick {

    public interface OnItemClickListener {
            void onItemClick(View view, int position);
        }

}






How to set values to text view TextView qtyTV; got from adapter
android android-recyclerview interface sharedpreferences adapter
1个回答
0
投票

适配器:

public VegeHomeAdapter(Context ctx, ArrayList<ModelTopProd> mData, OnItemClick listener) {
    this.mData = mData;
    this.ctx = ctx;
    this.mCallback = listener;

    interface OnItemClick {
       void onClick (String value);
       void getTotalItems(int totalItems);
   }
}
  int total_items = Integer.parseInt(preferenceHelper.getCARTNO());

  total_items+=1;
  preferenceHelper.putCARTNO(""+total_items);
  mCallback.onClick(""+preferenceHelper.getCARTNO());
}

首页:

public class Home extends AppCompatActivity implements View.OnClickListener, OnItemClick {

  private int totalItems = 0

  @Override 
  public onClick(String itemName) { 
     //your implementation
  }

  @Override
  public void getTotalItems(int total)  {
      totalItems = total;
  }



}
© www.soinside.com 2019 - 2024. All rights reserved.