Search code examples
androidlistviewandroid-edittext

EditText in ListView loses content in scrolling


I've been digging to find a solution on my problem finding answers but none of them have worked for me. :/ I have a listview with edittext in each list item and when scrolling down and go up again content of the edited edittext has been lost or other edittext has taken his value.

please help me some one

MainActivity:-

public class MainActivity extends AppCompatActivity {

    private ArrayList<Friend> friendArrayList;
    ListViewAdapter adapter;

    String[] countriessArray =  { "USA", "USA", "USA", "USA",
            "China", "Japan", "France", "USA","USA", "USA", "USA", "USA",
            "China", "Japan", "France", "USA"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        friendArrayList = new ArrayList<Friend>();
        for (int i = 0; i < countriessArray.length; i++) {

            Friend item = new Friend(countriessArray[i]);
            friendArrayList.add(item);
        }

        ListView listView = (ListView) findViewById(R.id.list);
        listView.setItemsCanFocus(true);

        adapter = new ListViewAdapter(this, R.layout.item_listview, friendArrayList);

        listView.setAdapter(adapter);
    }

ListViewAdaper:-

public class ListViewAdapter extends ArrayAdapter<Friend> {

    ViewHolder holder;
    private List<Friend> myFriends;
    private Activity activity;

    public static HashMap<Integer,String> myList=new HashMap<Integer,String>();

    public ListViewAdapter(Activity context, int resource, List<Friend> objects) {

        super(context, resource, objects);
        this.activity = context;
        this.myFriends = objects;
    }

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

        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        final int viewpos = position;

        // If holder not exist then locate all view from UI file.
        if (convertView == null) {
            // inflate UI from XML file
            convertView = inflater.inflate(R.layout.item_listview, parent, false);
            // get all UI view
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);

        } else {
            // if holder created, get tag from view
            holder = (ViewHolder) convertView.getTag();
        }

        holder.friendName.setText(getItem(position).getName());

        //Edit Text Listeners:-

        holder.editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            @Override
            public void afterTextChanged(Editable s) {
                myList.put(position,s.toString().trim());
            }
        });

        holder.editText.setText(myList.get(position));

        return convertView;
    }

    private static class ViewHolder {

        private TextView friendName;
        private EditText editText;

        public ViewHolder(View v) {
            friendName = (TextView) v.findViewById(R.id.name);
            editText = (EditText) v.findViewById(R.id.edit);
        }
    }
}

Custom Object:-

public class Friend {

    private String name,editText;
    private int selection = 0;

    public Friend(String name) {
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setSelection(int selection) {
        this.selection = selection;
    }

    public int getSelection() {
        return selection;
    }

    public void setEditText(String editText) {
        this.editText = editText;
    }

    public String getEditText() {
        return editText;
    }
}

Solution

  • You need to store data in afterTextChanged as same object as you set in text

       holder.friendName.setText(myFriends.get(position).getName());
    
            //Edit Text Listeners:-
    
            holder.editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
    
                }
                @Override
                public void afterTextChanged(Editable s) {
                    myFriends.get(position).setName(s.toString);
                }
            });