0

I am using an Expandable listview in Android and having huge problems with interactive EditTexts within the child items due to Android's recycle policy.

I was wondering if I could somehow disable this recycling (there won't be too many elements there, so performance shouldn't be an issue)?

The problem is that there are duplicates in the EditTexts, i.e. if I type in something in the first EditText, it is possible that in some other EditText the same text appears, or if I collapse and expand a group, the strings in the EditTexts often get mixed up.

Following my code:

EDIT:

@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildItem item = getChild(groupPosition, childPosition);
    String comment = "";
    state = getStatelist().get(groupPosition);

    if (convertView == null) {
        holder = new ChildHolder();           
        convertView = inflater.inflate(R.layout.childitem_list_item, parent, false);
        holder.title = (EditText) convertView.findViewById(R.id.textTitle);          
        convertView.setTag(holder);
    } else {
        holder = (ChildHolder) convertView.getTag();
    }

    if(state.commentList != null){
        holder.title.setText(state.commentList[childPosition]);
    }
    else{
        holder.title.setText("");
    }

    holder.title.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            state = getStatelist().get(groupPosition);

            state.commentList[childPosition] = s.toString();                

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
}


public class States {

  public String code = "";
  public String name = "";
  public String[] commentList;
 }
deimos1988
  • 5,896
  • 7
  • 41
  • 57
  • Simply ignoring the non null `convertView`(building the row views each time) doesn't work? – user Jun 03 '14 at 07:15
  • Unfortunately it doesn't; since I am using EditTexts, duplicate entries appear when I don't use the viewHolder pattern. – deimos1988 Jun 03 '14 at 08:45
  • I don't see how you would have duplicates if you properly set the data. I was only talking about the convertView and not the ViewHolder pattern. – user Jun 03 '14 at 09:07
  • If you want to have EditText fields, do not use ListViews. Use LinearLayouts instead. It is as simple as that. – Philip Sheard Jun 03 '14 at 13:14
  • Have a look at this question http://stackoverflow.com/questions/2679948/focusable-edittext-inside-listview . I've seen that you deleted that line which was wrong:) – user Jun 03 '14 at 13:16
  • @PhilipSheard I need Expandable Listviews with EditTexts as children. I suppose using ListViews to accomplish that is rather difficult? – deimos1988 Jun 03 '14 at 15:54

1 Answers1

1

You have two different problems. What you want is to clear up the values of the edittexts in every rebuild. So you can change

  if(state.commentList != null){
        holder.title.setText(state.commentList[childPosition]);
    }
    else{
        holder.title.setText("");
    }

into

holder.title.setText("");
  if(state.commentList != null){
        holder.title.setText(state.commentList[childPosition]);
    }

Now if what you really really want is to remove view recycling, just make convertview always reinflate

state = getStatelist().get(groupPosition);
holder = new ChildHolder();           
convertView = inflater.inflate(R.layout.childitem_list_item, parent, false);
holder.title = (EditText) convertView.findViewById(R.id.textTitle);          
convertView.setTag(holder);
holder = (ChildHolder) convertView.getTag();
// You can remove the viewholder pattern because you're not using it anymore
if(state.commentList != null){
MLProgrammer-CiM
  • 17,231
  • 5
  • 42
  • 75
  • Interesting, inflating the view every time seems to work, but now I have to touch the EditText twice until it gets focus. How come? – deimos1988 Jun 03 '14 at 13:16
  • I don't have enough information to answer that question as it is. – MLProgrammer-CiM Jun 03 '14 at 13:24
  • If I simply inflate the view every time, when I click on one of the EditTexts, the cursor appears for a short time, then the keyboard appears and the EditText loses focus. Then I have to touch the EditText again to type stuff in. – deimos1988 Jun 03 '14 at 16:03
  • I don't know how your layout, activity/fragment or touch/edittext listeners are and I cannot infer it from a description of the behaviour. – MLProgrammer-CiM Jun 03 '14 at 17:36
  • My layout doesn't contain anything interesting, just regular settings, nothing like "android:beforeDescdencants" or anything, the only EditText listener is the one I posted in my question (addTextChangeListener). – deimos1988 Jun 04 '14 at 06:56