Here i have data like menu item has multiple categories and multiple category has multiple items now my problem is the items will be selected by the given selected no. which are given with the categories.
here is the image. here is my Adapter
public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.MenuHolder> {
public static ArrayList<CartItemBean> cartItemBeanArrayList;
public static ArrayList<CartBean> cartList = new ArrayList<>();
public static CartItemBean cartItemBean;
public static CartBean cartBean;
public static String item, category, id;
Button addmenu_btn;
int listsize;
String select;
int selectInt;
int numChecked = 0;
CheckBox checkBox;
private List<MenuBean> menulist;
private Context mContext;
public MenuAdapter(Context context, List<MenuBean> menulist) {
this.menulist = menulist;
this.mContext = context;
}
@Override
public MenuHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowitem_menu, null);
v.setLayoutParams(new RecyclerView.LayoutParams(
RecyclerView.LayoutParams.MATCH_PARENT,
RecyclerView.LayoutParams.WRAP_CONTENT
));
MenuHolder mh = new MenuHolder(v, mContext);
return mh;
}
@Override
public void onBindViewHolder(MenuHolder menuHolder, final int i) {
final ArrayList itemlist = menulist.get(i).getItemBeansList();
listsize = itemlist.size();
menuHolder.menutitle.setText(menulist.get(i).getCategory());
menuHolder.menuitemselect.setText("Select-" + menulist.get(i).getNitem());
menuHolder.lnr_view.removeAllViews();
select = menulist.get(i).getNitem();
selectInt = Integer.valueOf(select);
cartItemBean = new CartItemBean();
cartItemBeanArrayList = new ArrayList<>();
cartBean = new CartBean();
for (int k = 0; k < itemlist.size(); k++) {
checkBox = new CheckBox(mContext);
checkBox.setId(Integer.parseInt(menulist.get(i).getItemBeansList().get(k).getId()));
checkBox.setText(menulist.get(i).getItemBeansList().get(k).getItem());
menuHolder.lnr_view.addView(checkBox);
final int finalK = k;
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
numChecked++;
Log.e("numChecked::::", "" + numChecked);
item = buttonView.getText().toString();
Log.e("CheckItem", item);
category = menulist.get(i).getCategory();
Log.e("Check Category", category);
id = menulist.get(i).getItemBeansList().get(finalK).getId();
Log.e("Check Id", id);
cartItemBean.setCategoryName(category);
cartItemBean.setCategoryItem(item);
/* //cartItemList.add(item);
//cartBean.setCategoryItem(item);
// cartBean.setCartItem(cartItemList);
// cartBean.setCategoryName(category);
// cartBean.setId(id);*/
cartItemBeanArrayList.add(cartItemBean);
} else {
numChecked--;
cartItemBeanArrayList.remove(cartItemBean);
// cartList.remove(cartBean);
}
}
});
}
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public int getItemCount() {
return (null != menulist ? menulist.size() : 0);
}
class MenuHolder extends RecyclerView.ViewHolder {
protected TextView menutitle, menuitemselect;
CardView menucardview;
LinearLayout lnr_view;
public MenuHolder(View itemView, final Context mContextv) {
super(itemView);
lnr_view = (LinearLayout) itemView.findViewById(R.id.lnr_view);
menutitle = (TextView) itemView.findViewById(R.id.menutitle);
menucardview = (CardView) itemView.findViewById(R.id.menucardview);
menuitemselect = (TextView) itemView.findViewById(R.id.menuitemselect);
}
}
}
Aucun commentaire:
Enregistrer un commentaire