如何从回收器视图适配器获取购物车项目数量到其父活动。
定义接口,在回收器视图适配器中设置项目计数,并在主活动中实现接口
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
适配器:
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;
}
}