首页 > 解决方案 > ArrayList TextInput 等于条件不显示用户输入的内容

问题描述

所以我的数组中有 3 个从 API 获取的对象,在我的情况下,当用户输入一些文本并且文本输入的值将与促销名称(下图的底行)进行比较时,它将显示促销名称屏幕上。但是当我输入文字时,它只会显示第二个位置(HELLO),我该如何解决这个当我输入像PROMOBERKAH这样的文字时它会显示PROMOBERKAH,当我输入JULICERIA时它会显示JULICERIA?谢谢。

在此处输入图像描述

这是我的条件代码,

public void onTextChanged(CharSequence s, int start, int before, int count) {
                for (int i=0;i<ar1.size();i++) {
                    if (ar1.contains(edtVoucherCode.getText().toString())) {
                        int position = ar1.indexOf(edtVoucherCode.getText().toString());
                        codeNotFound.setText("Kode promo berhasil digunakan");
                        codeNotFound.setTextColor(Color.parseColor("#1ECBB5"));
                        codeNotFound.setVisibility(View.VISIBLE);
                        isValid = true;
                        usePromoPrivateId = jsonData.get(position).getId_promo();
                        usePromoPrivateName = jsonData.get(position).getPromo_name();
                        if (jsonData.get(position).getPromo_type().equals("1")) {
                            usePromoPrivateDiscount = jsonData.get(position).getValue_based();;
                        } else {
                            usePromoPrivateDiscountPrice = jsonData.get(position).getValue_based();;
                        }
                    } else {
                        codeNotFound.setText("Kode promo tidak ditemukan");
                        codeNotFound.setTextColor(Color.parseColor("#DF2C2E"));
                        codeNotFound.setVisibility(View.VISIBLE);
                        isValid = false;
                    }
                }
}

标签: javaandroidarraylist

解决方案


通过将 indexOf 添加到我的条件代码来解决

int position = ar1.indexOf(edtVoucherCode.getText().toString());

推荐阅读