Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6679813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:24:09+00:00 2026-05-26T04:24:09+00:00

I have an Android activity in which I have a ListView bound to a

  • 0

I have an Android activity in which I have a ListView bound to a custom ArrayAdapter. Each row of the ListView has two EditText (numeric) fields.

The ArrayAdapter is initially populated from an SQLite DB. However the user can add a row at the end of the ListView, or (by long-pressing on a row) delete any row in the ListView. When they hit the ‘Save’ button, their changes are persisted.

I am keeping track of changes as they are made by attaching a CustomTextWatcher for the AfterTextChanged() event to each EditText in the ArrayAdapter’s getView() method (passing in the EditText and the corresponding object in the ArrayAdapter’s item list) and then setting the matching property of that object to the content of the EditText. This is so that on saving I can simply iterate through the underlying object list and make the appropriate DB changes, knowing that the object list is up-to-date.

Code for the adapter class and the CustomTextWatcher:

private class CustomAdapter extends ArrayAdapter<DataItem> {
    private ArrayList<DataItem> items;

    public CustomAdapter(Context context, int textViewResourceId, ArrayList<DataItem> items) {
        super(context, textViewResourceId, items);
        this.items = items;
}

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;

            DataItem wed = items.get(position);

            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.log_exercise_row, null);
            }

            if(wed != null)
            {
                     EditText text1 = (EditText) v.findViewById(R.id.text1);
                     EditText text2 = (EditText) v.findViewById(R.id.text2);

                text1.addTextChangedListener(new CustomTextWatcher(text1,wed));
                text2.addTextChangedListener(new CustomTextWatcher(text2,wed));


                int weight = wed.getWeight();

                if(weight != -1)
                {
                   text1.setText(weight+"");
                }


                int reps = wed.getReps();

                if(reps != -1)
                {
                    text2.setText(reps+"");
                }


            }

            return v;
    }

private class CustomTextWatcher implements TextWatcher {

    private EditText EditText; 
    private DataItem item;

    public CustomTextWatcher(EditText e, DataItem item)
    {
        this.EditText = e;
        this.item = item;
    }

    @Override
    public void afterTextChanged(Editable arg0) {

        String text = arg0.toString();

        if(text != null && text.length() > 0)
        {
            int val;

            try
            {
                val =Integer.parseInt(text);
            }

            catch(NumberFormatException e)
            {
                val=-1;
            }

            if(EditText.getId()==R.id.weightEditText)
            {
                item.setWeight(val);
            }

            else if(EditText.getId()==R.id.repsEditText)
            {
                item.setRepetitions(val);
            }
        }
    }

This all works fine except when items are added or deleted when unwanted duplication of EditText content happens.

To Illustrate with an example:

Start with 3 rows, EditText’s all empty
In row 2, enter ‘5’, ‘6’
Click ‘More’->Adds an (empty) row on the end. Now 4 rows.
Delete last row->3 rows displayed, BUT 2nd AND 3rd rows now display ‘5’, ‘6’->???????

It looks like the relative position of EditText objects within the ListView is not stable after rebinding which is causing the problem. E.g. EditText object ‘X’ starts in position 3, then after a rebind it is in position 1 – but it still has a CustomTextWatcher attached to it referencing the data object in position 3. The other issue is the fact that addTextChangedListener() does not affect the TextWatchers already attached to the EditText.

I’m pretty new to Android, so I’m not sure if there is a better approach to solve my problem. Any help is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:24:09+00:00Added an answer on May 26, 2026 at 4:24 am

    It looks like a custom (‘ViewHolder’) class was what I needed in order to keep the references between a EditText and its associated data object properly synchronised on each ‘bind’ of the data. Also, placing the event listener calls when the convertView object was null in the getView() method meant only one listener was added per EditText object.

    Thanks to http://www.vogella.de/articles/AndroidListView/article.html#listsactivity for pointing me in the right direction.

    public class CustomAdapter extends ArrayAdapter<DataItem> {
    private ArrayList<DataItem> items;
    private Activity Context;
    
    public CustomAdapter(Activity context, int textViewResourceId, ArrayList<DataItem> items) {
        super(context, textViewResourceId, items);
        this.items = items;
        this.Context = context; 
    }
    
    static class ViewHolder {
        protected EditText weight;
        protected EditText reps;
    
    }
    
     public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
    
            DataItem wed = items.get(position);
    
    
            if (v == null) {
                LayoutInflater inflator = Context.getLayoutInflater();
                v = inflator.inflate(R.layout.log_exercise_row, null);
                final ViewHolder viewHolder = new ViewHolder();
                viewHolder.text1 = (EditText) v.findViewById(R.id.text1);
                viewHolder.text2 = (EditText) v.findViewById(R.id.text2);
    
    
                viewHolder.text1.addTextChangedListener(new CustomTextWatcher(viewHolder, viewHolder.text1));
                viewHolder.text2.addTextChangedListener(new CustomTextWatcher(viewHolder, viewHolder.text2));
    
                v.setTag(viewHolder);
                viewHolder.text1.setTag(wed);
                viewHolder.text2.setTag(wed);
    
            }
    
            else
            {
                ViewHolder holder = (ViewHolder) v.getTag();
                holder.text1.setTag(wed);
                holder.text2.setTag(wed);   
            }
    
            ViewHolder holder = (ViewHolder) v.getTag();
    
            // set values
    
            if(wed.getWeight() != -1)
            {
                holder.text1.setText(wed.getWeight()+"");
            }
    
            else
            {
                holder.weight.setText("");
            }
    
            if(wed.getRepetitions() != -1)
            {
                holder.text2.setText(wed.getRepetitions()+"");
            }
    
            else
            {
                holder.reps.setText("");
            }
    
            return v;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Activity in Android, with two elements: EditText ListView When my Activity
I have an activity with a ListView which is populated through a custom ArrayAdapter.
I have a custom listView with editText in each row to carry value entered
i have an android application which has a listview for listing 14 items inside
I have an activity in Android which uses a ListView. When I click on
I’m new to android,I have an activity class which fetches Json string from url
I am building an android application which has a listview and when the user
I have a custom listview which changes size when one of the rows is
I have a custom ListView adapter which implements an ImageThreadLoader class. Unfortunately, the class
I have a custom ListView adapter which implements an ImageThreadLoader class. Unfortunately the class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.