如何解决这个问题?
措施:
List<Map<String, Object>> measures(BuildContext context) {
return[
{"label": S.of(context).distanceTranslate, "icon": "assets/icons/ruler2.png"},
{"label": S.of(context).distanceTranslate, "icon": "assets/icons/temp2.png"},
{"label": S.of(context).distanceTranslate, "icon": "assets/icons/paid4.png"},
{"label": S.of(context).distanceTranslate, "icon": "assets/icons/time2.png"},
{"label": S.of(context).distanceTranslate, "icon": "assets/icons/binary2.png"},
];
}
请帮忙。
答案 0 :(得分:1)
public class NewOrderAdapter extends RecyclerView.Adapter<NewOrderAdapter.ViewHolder> {
private ArrayList<NewOrderModel> newOrderList; //Main Arraylist
private ArrayList<CheckoutDialogModel> checkoutList; //Arraylist to be made
Context mContext;
String itemCode, casesEtVal, bottlesEtVal;
String[] arrCases, arrBottles; //Array for getting edittext values
ArrayList<String> skuArray = new ArrayList<>(); //textview array
ArrayList<String> casesArray = new ArrayList<>(); //Edittext1 array
ArrayList<String> bottlesArray = new ArrayList<>(); //Edittext2 array
boolean isOnTextChanged = false;
int id;
public NewOrderAdapter(ArrayList<NewOrderModel> newOrderList, Context context) {
this.newOrderList = newOrderList;
this.mContext = context;
arrCases = new String[newOrderList.size()];
arrBottles = new String[newOrderList.size()];
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.new_order_recycler_item, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
NewOrderModel model = newOrderList.get(position);
holder.txtModelName.setText(model.getItemCode());
id = model.getId();
holder.edtPrice1.setTag(position);
holder.edtPrice1.setText(newOrderList.get(position).getCasesPrice());
holder.edtPrice2.setTag(position);
holder.edtPrice2.setText(newOrderList.get(position).getUnitsPrice());
holder.edtPrice2.setOnClickListener(v -> {
final Snackbar snackbar = Snackbar.make(v, "",Snackbar.LENGTH_INDEFINITE);
View view = LayoutInflater.from(mContext).inflate(R.layout.new_order_snack, null);
snackbar.getView().setBackgroundColor(Color.TRANSPARENT);
Snackbar.SnackbarLayout snackbarLayout = (Snackbar.SnackbarLayout)snackbar.getView();
snackbarLayout.setPadding(0,0,0,0);
Button btnCheckout = view.findViewById(R.id.btnCheckOut); //Checkout button
btnCheckout.setOnClickListener(v12 -> {
ArrayList<String> fSKUarray = new ArrayList<>();
ArrayList<String> fCasesArray = new ArrayList<>();
ArrayList<String> fBottlesArray = new ArrayList<>();
if (!casesArray.isEmpty() && !bottlesArray.isEmpty()) {
for (int i = 0; i< casesArray.size(); i++) {
if (!casesArray.get(i).equals("0") && !casesArray.get(i).equals("")){
fCasesArray.add(casesArray.get(i));
fSKUarray.add(skuArray.get(i));
}
}
for (int i = 0; i<bottlesArray.size(); i++) {
if (!bottlesArray.get(i).equals("0") && !bottlesArray.get(i).equals("")) {
fBottlesArray.add(bottlesArray.get(i));
}
}
}
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
View view1 = LayoutInflater.from(mContext).inflate(R.layout.checkout_dialog, null);
RecyclerView orderRecycler = view1.findViewById(R.id.orderRecycler);
orderRecycler.setHasFixedSize(true);
orderRecycler.setLayoutManager(new LinearLayoutManager(view1.getContext()));
orderRecycler.addItemDecoration(new DividerItemDecoration(view1.getContext(), LinearLayoutManager.VERTICAL));
checkoutList = new ArrayList<CheckoutDialogModel>(){
{
for (int i = 0; i < newOrderList.size(); i++){
if (casesEtVal != null && bottlesEtVal != null){
String sku = newOrderList.get(i).getItemCode();
switch (newOrderList.get(i).getGroupCode()) {
case "114":
add(new CheckoutDialogModel("BEER", "", "", ""));
add(new CheckoutDialogModel("", "", sku,
newOrderList.get(i).getCasesPrice() + "."+ newOrderList.get(i).getUnitsPrice()));
break;
case "115":
add(new CheckoutDialogModel("LIQUOR", "", "", ""));
add(new CheckoutDialogModel("", "", sku,
newOrderList.get(i).getCasesPrice() + "."+ newOrderList.get(i).getUnitsPrice()));
break;
case "109":
add(new CheckoutDialogModel("WINE", "", "", ""));
add(new CheckoutDialogModel("", "", sku,
newOrderList.get(i).getCasesPrice() + "."+ newOrderList.get(i).getUnitsPrice()));
break;
}
}
}
}
};
checkoutList = getCheckoutList();
CheckoutDialogAdapter mAdapter = new CheckoutDialogAdapter();
mAdapter.setCheckOutList(checkoutList);
orderRecycler.setAdapter(mAdapter);
snackbar.dismiss();
builder.setView(view1);
final AlertDialog dialog = builder.create();
Button btnPlaceOrdr = view1.findViewById(R.id.btnPlaceOrdr);
btnPlaceOrdr.setOnClickListener(v13 -> {
dialog.dismiss();
});
dialog.show();
});
snackbarLayout.addView(view, 0);
snackbar.show();
});
}
public ArrayList<CheckoutDialogModel> getCheckoutList() {
return checkoutList;
}
@Override
public int getItemCount() {
return newOrderList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView txtModelName;
private EditText edtPrice1, edtPrice2;
public ViewHolder(@NonNull View itemView) {
super(itemView);
txtModelName = itemView.findViewById(R.id.modelName);
edtPrice1 = itemView.findViewById(R.id.price1);
edtPrice2 = itemView.findViewById(R.id.price2);
edtPrice1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (edtPrice1.getTag()!= null) {
newOrderList.get(getAdapterPosition()).setCasesPrice(edtPrice1.getText().toString());
}
}
@Override
public void afterTextChanged(Editable s) {
if (isOnTextChanged) {
isOnTextChanged = false;
try {
for (int i = 0; i<= id; i++) {
if (i!=id){
skuArray.add("0");
casesArray.add("0");
} else {
skuArray.add("0");
casesArray.add("0");
skuArray.set(id, newOrderList.get(getAdapterPosition()).getItemCode());
casesArray.set(id, casesEtVal);
}
}
Log.d("skuList", skuArray.toString());
Log.d("casesList", casesArray.toString());
} catch (NumberFormatException e){
for (int i = 0; i<= id; i++) {
if (i == id) {
skuArray.set(id,"0");
casesArray.set(id,"0");
}
}
}
}
}
});
edtPrice2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
isOnTextChanged = true;
if (edtPrice2.getTag()!=null) {
newOrderList.get(getAdapterPosition()).setUnitsPrice(edtPrice2.getText().toString());
}
}
@Override
public void afterTextChanged(Editable s) {
if (isOnTextChanged) {
isOnTextChanged = false;
try {
for (int i = 0; i<= id; i++) {
if (i!=id){
skuArray.add("0");
casesArray.add("0");
bottlesArray.add("0");
} else {
skuArray.add("0");
casesArray.add("0");
bottlesArray.add("0");
skuArray.set(id, newOrderList.get(getAdapterPosition()).getItemCode());
casesArray.set(id, casesEtVal);
bottlesArray.set(id, bottlesEtVal);
}
}
Log.d("skuList", skuArray.toString());
Log.d("casesList", casesArray.toString());
Log.d("bottlesList", bottlesArray.toString());
} catch (NumberFormatException e){
for (int i = 0; i<= id; i++) {
if (i == id) {
skuArray.set(id,"0");
casesArray.set(id,"0");
bottlesArray.set(id,"0");
}
}
}
}
casesEtVal = edtPrice1.getText().toString();
arrCases[getAdapterPosition()] = casesEtVal;
bottlesEtVal = edtPrice2.getText().toString();
arrBottles[getAdapterPosition()] = bottlesEtVal;
if (!casesEtVal.equals("") && !bottlesEtVal.equals("")) {
if (Integer.parseInt(bottlesEtVal) > Integer.parseInt(casesEtVal)) {
Toast.makeText(mContext, "Bottles can't be more than Cases!", Toast.LENGTH_SHORT).show();
edtPrice2.setText(newOrderList.get(getAdapterPosition()).getUnitsPrice().replace(bottlesEtVal, ""));
}
}
}
});
}
}
是一个函数,它在调用时返回一个值。
你需要调用它来获取它的价值。这是一个简单的解决方法:
public class CheckoutDialogAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<CheckoutDialogModel> mArrayList;
private static final int TYPE_HEADER = 1;
private static final int TYPE_ITEM = 2;
public CheckoutDialogAdapter() {}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v;
if (viewType == TYPE_HEADER){
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.checkout_dialog_header, parent, false);
return new VHHeader(v);
}
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.checkout_dialog_item, parent, false);
return new VHItem(v);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
int viewType = getItemViewType(position);
if (viewType == TYPE_ITEM) {
VHItem vhItem = ((VHItem)holder);
vhItem.itemName.setText(mArrayList.get(position).getItemName());
vhItem.itemQty.setText(mArrayList.get(position).getItemQty());
} else if (viewType == TYPE_HEADER) {
VHHeader vhHeader = ((VHHeader)holder);
vhHeader.headerName.setText(mArrayList.get(position).getHeaderName());
vhHeader.headerQty.setText(mArrayList.get(position).getHeaderQty());
}
}
@Override
public int getItemCount() {
return mArrayList == null ? 0 : mArrayList.size();
}
@Override
public int getItemViewType(int position) {
if (TextUtils.isEmpty(mArrayList.get(position).itemName)) {
return TYPE_HEADER;
} else {
return TYPE_ITEM;
}
}
public void setCheckOutList(ArrayList<CheckoutDialogModel> arrayList) {
this.mArrayList = arrayList;
notifyDataSetChanged();
}
public class VHHeader extends RecyclerView.ViewHolder {
TextView headerName, headerQty;
LinearLayout headerLayout;
public VHHeader(@NonNull View itemView) {
super(itemView);
headerName = itemView.findViewById(R.id.headerName);
headerQty = itemView.findViewById(R.id.headerQty);
headerLayout = itemView.findViewById(R.id.headerLayout);
}
}
public class VHItem extends RecyclerView.ViewHolder {
TextView itemName, itemQty;
LinearLayout itemLayout;
public VHItem(@NonNull View itemView) {
super(itemView);
itemName = itemView.findViewById(R.id.itemName);
itemQty = itemView.findViewById(R.id.itemQty);
itemLayout = itemView.findViewById(R.id.itemLayout);
}
}
答案 1 :(得分:0)
measures
是一个函数,您将它用作导致问题的变量。
更新您的代码如下
items: measures(context).map(
(e) => BottomNavigationBarItem(
icon: Image.asset(e['icon']),
label: e['label'],
),
).toList(),