Skip to content Skip to sidebar Skip to footer

Edit Text Loses Content In List View On Scrolling

I have multiple edit text in list view. Number of edit texts is not fixed. When list view is scrolled edit text loses its content or some other edit text that is in focus shows val

Solution 1:

A generic solution for these kind of ListView problems is the View Holder pattern. It also has a small positive impact on performance.

Basically the idea is to create a class which represents the UI of one Listview item. It stores the View instances (EditTexts and whatnot) with their content intact.

privateclassMyViewHolder {
  public EditText myEditText;
  public ViewGroup myListItemLayout;
}

The View Holder object is created when a View is accessed for the first time in the list adapter's getView() and is then stored as the View objects' tags.

public View getView(int position, View convertView, ViewGroup parent) {

  MyViewHolderholder=newViewHolder(); 

  // Get the data object for this row.MyDataItemitem= items.get(position);

  // If we receive a null View we need to construct a one.if (convertView == null) {
    convertView = inflater.inflate(R.layout.my_list_item, parent, false);

    // Store the UI elements in the View Holder.
    holder.myEditText = (EditText)view.findViewById(R.id.editTextMyEditText);
    holder.myListItemLayout = (ViewGroup)view.findViewById(R.id.layoutMyListItemLayout);

    // Strore the View Holder as the View's tag.
    convertView.setTag(holder);
  }

  // Get the holder from the View's tag. 
  holder = (ViewHolder) convertView.getTag();

  ...
}

Now when the same View is later accessed again in getView() and convertView != null we will skip all the findViewById() code and go straight into:

holder = (ViewHolder) convertView.getTag();

Now the View has the correct content instead of some other list item's data.

As for rest of the getView() code you'll just replace all references to the View convertView with references to the View Holder i.e. instead of convertView.myTextView.setText("Hello world!") you'll use holder.myTextView.setText("Hello world!")

Some references: https://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolderhttp://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.htmlhttps://dzone.com/articles/optimizing-your-listviewhttp://www.vogella.com/tutorials/AndroidListView/article.html#adapterperformance_holder

Solution 2:

Your adapter has to save text for each EditText :

  1. create a map to save values
  2. Define and set a generic TextWatcher for every EditText
  3. set a Tag on the editText (using its position or give any id you want) you'll use this tag as the key in the hashMap
  4. create a method to retrieve the value for a given EditText and save the HashMap values in cache or sharedPreferences.
  5. Check position of editText and populate values accordingly by retrieving it from stored cache/sharedPreferences.

follow Get all EditText values for hint only.

Post a Comment for "Edit Text Loses Content In List View On Scrolling"