1

I have a small list of almost 10 items (and this will not be greater than 20 in any case). It will not be changed after activity is created.

My Current Setup:

  • I am using a RecyclerView for the list.
  • I have a Checkbox and 2 EditText (say 1 and 2) in each list item.
  • I do some calculations based on if checkbox checked and the value of EditText-1 and populate the new values to another EditText-2.
  • On some cases user edits the Value of EditText-2 and calculations happen again and it will be populated in all other places including EditText-1 of for all the items.

Problems solved:

Another Problem:

Now, as data change of EditText-1 changes values for all other items of RecyclerView, I am calling notifyDatasetChanged() to reflect the changes after doing calculations. But this causes the EditText to loose focus. And it intends as all the views are recreated after notifyDatasetChanged. Even stable ids are also not useful in this situation. (There is bug related this issue: https://code.google.com/p/android/issues/detail?id=204277). The value is not even reflects and EditText looses it focus.

What can I do?:

  • Is there any workaround? I am seeing strange changes in items values after setting stable ids.
  • As the Items are fixed and no items are added and removed after onCreate, Using a scroll view with the container and adding items by manually inflating views in loop (and updating also will be ease, I think) will make my life a little easier?
Community
  • 1
  • 1
kirtan403
  • 7,293
  • 6
  • 54
  • 97

2 Answers2

0

Lets take an array of objects to save the statuses of your EditText along with the values to be populated in your list. For example each of your items in the list might look like this class.

public class ListItemCustom {

    // Add some extra parameters to handle the state of the EditTexts
    public String textOfEditText1;
    public String textOfEditText2;
    public boolean focusEditText1;
    public boolean focusEditText2;

    // Contains original values of your list item
    public OriginalListItem mOriginalListItem;
}

Now take an Array or ArrayList of ListItemCustom custom and populate your items there inside onCreate. Then pass this list in your adapter and then handle the EditText and the values accordingly.

Hope this helps.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
0

The solution is simple, don't call notifyDatasetChanged() when your data is changing instead call notifyItemChanged() or notifyItemRangeChanged() which will update only that view which has the changes and your focus will remain as it was.

hArsh
  • 111
  • 1
  • 10