0

I have an activity with a ListView that gets filled by a custom adapter. The adapter is filled via an ArrayList of Models with values from a JSON Object and Array.

What happens is that when you scan a barcode it fills the field with the value of the barcode but when you select the EditText of the second ListView layout it bugs back to the EditText you're trying to leave and due to the keyboard opening you can't see what happens but when you scroll up the ListView the previously filled in EditText is empty.

If you try to enter the value using the keyboard it 'works' but when you close the keyboard via the back button (Pixel 2 API V33) it also clears the previous input. But instead of having focus on the next EditText you gain focus on the ConstraintLayout of the first ListView item.

Filling adapter and calling the Adapter:

JSONArray resArr = objres.getJSONArray("Results");

for (int i = 0; i < resArr.length(); i++) {
    JSONObject temp = resArr.getJSONObject(i);
    ProductionLineModel productionLineModel = new ProductionLineModel(Barcode, temp.getString("..."), temp.getString("..."), temp.getString("..."), temp.getString("..."), temp.getString("..."));
    PalletIDModel palletIDModel = new PalletIDModel(temp.getString("..."), null, temp.getString("..."),  temp.getString("..."), temp.getString("..."), temp.getInt("..."));
    if (dataBaseHelper.AddProductionPallet(palletIDModel, Barcode)) {
        allLines.add(productionLineModel);
    } else {
        // Error handling
    }
}

ProductionListAdapter adapter = new ProductionListAdapter(this, R.layout.adapter_production_row, allLines, getIntent().getStringExtra("status"));

adapter.notifyDataSetChanged();

Lines_lv.setAdapter(adapter);

Adapter getView method (NOTE: This has two types of adapters the issue happens in both):

@NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        String Itemcode = getItem(position).getItemcode();
        String ItemDesc = getItem(position).getItemdescription();
        String PalletQty = getItem(position).getPalletqty();
        if (Objects.equals(status, "GECLAIMED")) {
            //get the information
            String Location = getItem(position).getLocation();

            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(mResource, parent, false);

            TextView tvitemcode = convertView.findViewById(R.id.textView37);
            TextView tvItemDesc = convertView.findViewById(R.id.textView38);
            TextView tvPalletQty = convertView.findViewById(R.id.textView39);
            EditText etvLocation = convertView.findViewById(R.id.PROD_location_etv);

            tvitemcode.setText(Itemcode);
            tvItemDesc.setText(ItemDesc);
            tvPalletQty.setText(PalletQty);
            etvLocation.setText(Location);

            EditText barcode = convertView.findViewById(R.id.PROD_barcode);
//            barcode.requestFocus();
            barcode.setOnEditorActionListener((v, actionId, event) -> {
                Log.e("Action", actionId + " + " + event);
                if (actionId == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER || actionId == EditorInfo.IME_ACTION_DONE) {
                    String newScan = barcode.getText().toString();
                    if (!newScan.isEmpty()) {
                        if (Objects.equals(getItem(position).getPalletcode(), newScan)) {
                            if (dataBaseHelper.UpdateProdPallet(getItem(position).getPalletcode(), getItem(position).getOrderNummer())) {
                                Toast.makeText(mContext, "Pallet geaccepteerd", Toast.LENGTH_SHORT).show();
                                barcode.setText(newScan);
                                Log.d("DEBUG", "Pallet accepted");
                                return true;
                            } else {
                                Toast.makeText(mContext, "Pallet NIET geaccepteerd", Toast.LENGTH_SHORT).show();
                                Log.e("DEBUG", "Pallet NOT accepted due: Database");
                                barcode.setText(newScan);
                            }
                        } else {
                            Toast.makeText(mContext, "Pallet NIET geaccepteerd", Toast.LENGTH_SHORT).show();
                            Log.e("DEBUG", "Pallet NOT accepted due: Codes not equal");
                            barcode.setText(newScan);
                        }
                    } else {
                        Toast.makeText(mContext, "Scan eerst een barcode", Toast.LENGTH_SHORT).show();
                        Log.e("DEBUG", "Pallet NOT accepted due: Nothing inserted");
                        barcode.setText(newScan);
                    }
                }
                return false;
            });
        } else {
            //get the information

            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(mResource, parent, false);

            TextView tvitemcode = convertView.findViewById(R.id.textView37);
            TextView tvItemDesc = convertView.findViewById(R.id.textView38);
            TextView tvPalletQty = convertView.findViewById(R.id.textView39);
            EditText etvLocation = convertView.findViewById(R.id.PROD_location_etv);
            TextView textView = convertView.findViewById(R.id.textView40);
            convertView.findViewById(R.id.PROD_barcode).setVisibility(View.INVISIBLE);
            TextView tvProgress = convertView.findViewById(R.id.Progress_tv);

            tvitemcode.setText(Itemcode);
            tvItemDesc.setText(ItemDesc);
            tvPalletQty.setText(PalletQty);
            tvProgress.setVisibility(View.VISIBLE);

            tvProgress.setText("Scan uw pallet");
            textView.setText("Barcode");
            final boolean[] PalletUpdated = {false};

            etvLocation.setOnEditorActionListener((v, actionId, event) -> {
                if (actionId == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER || actionId == EditorInfo.IME_ACTION_DONE) {
                    String newScan = etvLocation.getText().toString();
                    if (!newScan.isEmpty()) {
                        if (Objects.equals(getItem(position).getPalletcode(), newScan)) {
                            if (!PalletUpdated[0]) {
                                if (dataBaseHelper.UpdateProdPallet(getItem(position).getPalletcode(), getItem(position).getOrderNummer())) {
                                    Toast.makeText(mContext, "Pallet geaccepteerd", Toast.LENGTH_SHORT).show();
                                    tvProgress.setText("Scan uw locatie");
                                    textView.setText("Locatie");
                                    etvLocation.setText(newScan);
                                    PalletUpdated[0] = true;
                                } else {
                                    Toast.makeText(mContext, "Pallet NIET geaccepteerd", Toast.LENGTH_SHORT).show();
                                    etvLocation.setText(newScan);
                                }
                            } else {
                                if (dataBaseHelper.UpdateProdPallet(getItem(position).getPalletcode(), getItem(position).getOrderNummer())) {
                                    Toast.makeText(mContext, "Locatie verstuurd", Toast.LENGTH_SHORT).show();
                                    tvProgress.setText("Scan uw pallet");
                                    textView.setText("Barcode");
                                    etvLocation.setText(newScan);
                                    PalletUpdated[0] = false;
                                } else {
                                    Toast.makeText(mContext, "Pallet NIET geaccepteerd", Toast.LENGTH_SHORT).show();
                                }
                            }
                        } else {
                            Toast.makeText(mContext, "Pallet NIET geaccepteerd", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(mContext, "Scan eerst een barcode", Toast.LENGTH_SHORT).show();
                    }
                }
                return false;
            });
        }
        return convertView;
    }

This issue happens on multiple devices for me. I tested it with a Pixel 2 Emulator running on API Level 33 and a Zebra scanner running on API Level 29

Joossieπ
  • 15
  • 8
  • https://stackoverflow.com/questions/2679948/focusable-edittext-inside-listview?rq=1 – Dhruv Sakariya Dec 13 '22 at 12:41
  • @DhruvSakariya I dont see how that is related to my issue? the problem is that when I leave the focus or click a button it clears the value of the EditText. – Joossieπ Dec 13 '22 at 12:45
  • read about recycling pattern, why and how it works. it VERY should be applied for `ListView` by developer, but Google introduced `RecyclerView`, which may (and oftenly is) behave like usual `ListView`, but have recycling pattern "built-in", so dev have much less job to do in this topic – snachmsm Dec 13 '22 at 13:16
  • Would that fix the issue I'm having @snachmsm? – Joossieπ Dec 13 '22 at 15:49
  • 1
    imho it's crucial reason for disappearing text, so should be helpful. get familiar with that pattern, you will understood why your `EditText` is losing text (or any `View` may loose its state/content) – snachmsm Dec 13 '22 at 16:14
  • Thanks for the suggestion @snachmsm that did indeed 'fix' the issue. It's not a straight fix but it did solve my problem thanks! – Joossieπ Dec 14 '22 at 13:36

0 Answers0