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