ArrayAdapter redesigned convertView combines my old layout with the new one (ANDROID <5)
I am developing for Android> = 4 with Android Studio. THIS ONLY ON ANDROID <5 (Pre Lolipop) . The Lolipop ArrayAdapter has the correct behavior.
There is no better explanation than some GIFS:
I have a custom ArrayAdapter where I ask questions. Each element has its own layout depending on its type:
- Numeric Value: Label + Numeric EditText
- Text value: shortcut + EditText multiple lines
- Boolean value: label + checkbox
Each array adapter element inflates the same exact layout, overwriting getView (int position, View convertView, parent ViewGroup). I just show or hide the fields depending on the question type using setVisibility (true) or setVisibility (false).
@Override public View getView(int position, View convertView, ViewGroup parent){ mView = convertView; if(mView == null){ LayoutInflater mLayoutInflater; mLayoutInflater = LayoutInflater.from(getContext()); mView = mLayoutInflater.inflate(R.layout.item_liste_question,null); } final Question mQuestion = getItem(position); if(mQuestion != null){ TextView tvQuestion = (TextView) mView.findViewById(R.id.tv_question_nom); EditText questionNombre = (EditText) mView.findViewById(R.id.et_question_nombre); EditText questionTexte = (EditText) mView.findViewById(R.id.et_question_texte); CheckBox questionBool = (CheckBox) mView.findViewById(R.id.cb_question_bool); tvQuestion.setText(mQuestion.getQuestionLB()); if(mQuestion.getTypeReponse().equals("Num")){ questionTexte.setVisibility(View.GONE); questionNombre.setVisibility(View.VISIBLE); questionBool.setVisibility(View.GONE); } else if(mQuestion.getTypeReponse().equals("Text")){ questionTexte.setVisibility(View.VISIBLE); questionNombre.setVisibility(View.GONE); questionBool.setVisibility(View.GONE); } else if(mQuestion.getTypeReponse().equals("Bool")){ questionBool.setVisibility(View.VISIBLE); questionNombre.setVisibility(View.GONE); questionTexte.setVisibility(View.GONE); } } return mView; }
But when I click on the EditText field, the keyboard appears, shrinking the screen size, forcing the ArrayAdapter to shoot elements that are off-screen. There is nothing wrong with this, but these views are being recycled and reused by mixing the old layout with the new one.
For example: I have 5 original questions:
- Question1 - EditText (numeric)
- Question2 - EditText (numeric)
- Question3 - EditText (numeric)
- Question4 - EditText (Multilines)
- Question5 - Checkbox
I click on the first question and this is what I get:
- Question1 - EditText (numeric) + strange ghost field
- Question2 - EditText (numeric)
- Question3 - EditText (numeric)
- Question4 - EditText (Multilines)
- (Question5 is an off-screen checkbox)
After a lot of testing, I realized that since the fifth question comes out of the screen when I try to edit the first, convertView will give me a reworked view of the fifth question. And even though I reformat it as I wish, I still have a mysterious ghost field that gets confused with my keyboard (not numeric, as I would like).
What can I do to have the same correct behavior on Android 5+ on Android <5?
source share
No one has answered this question yet
See similar questions:
or similar: