• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android Listview and textWatcher Problems

tristan3560

Lurker
Jul 19, 2012
1
0
Good afternoon everybody :)
I have a listview for adding goals (not important :p). the listview's child has some textviews and edittextboxes, where the user can fill in its information. I have a Textwatcher which checks whether the text in one of the listview's childs has changed, so the data can be written to the class in the array off the listview + a global array (for passing data between activety's, but this is also not important).
Now my problem is, when I add an Item to the list, the text of all the previous existing items in edittext 'discription' changes to the text of newly added Item. For example when i have 1 Item in the listview that says 'test' and I add a new one to the list that says 'this is a new test' >> Both change to 'this is a new test'. I debugged the program a lot and noticed that in the end (after adding the new item), the program will go trough the textwatcher with 's' = 'this is a new test' and position = '0'. This probably causes the text of the first one to change.. But I don't understand why this is happening.
Can somebody help me?
this is my listview:

Code:
class listViewAdapter extends BaseAdapter {
        private Context context;
        private ArrayList<Goals> list;

        public listViewAdapter(final Context context, ArrayList<Goals> list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        public void synchronise(){
            geldclass.setGoals_list(this.list);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                final LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater.inflate(R.layout.goal_item, parent,
                            false);
            }

            Goals goal = (Goals) getItem(position);

            final EditText txtdiscription = (EditText) convertView.findViewById(R.id.editDiscription);
            txtdiscription.setText(goal.getDiscription());
            txtdiscription.addTextChangedListener(new textWatcher(txtdiscription, position));

            final EditText txtgoal = (EditText) convertView
                    .findViewById(R.id.editGoal);
            txtgoal.addTextChangedListener(new textWatcher(txtgoal, position));

            final EditText txtfrom = (EditText) convertView
                    .findViewById(R.id.editFrom);
            txtfrom.addTextChangedListener(new textWatcher(txtfrom, position));

            final EditText txtto = (EditText) convertView
                    .findViewById(R.id.editto);
            txtto.addTextChangedListener(new textWatcher(txtto, position));

            final CheckBox check_date = (CheckBox) convertView
                    .findViewById(R.id.check_enable_date);
            check_date
                    .setOnCheckedChangeListener(new OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {

                        }
                    });

            return convertView;
        }

        private class textWatcher implements TextWatcher {
            private View view;
            private int position;

            private textWatcher(View view, int position) {
                this.view = view;
                this.position = position;
            }

            @Override
            public void afterTextChanged(Editable s) {
                switch (view.getId()) {
                case R.id.editDiscription:
                    list.get(position).setDiscription(s.toString());
                    synchronise();
                    break;
                case R.id.editGoal:
                    list.get(position).setGoalvalue(Integer.parseInt(s.toString()));
                    synchronise();
                    break;
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub

            }
        }
    }
this is my goal child item in xml:


Code:
<TableLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textDiscription"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="@string/name"
            android:textSize="15dp"
            android:width="80dp" />

        <EditText
            android:id="@+id/editDiscription"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textNoSuggestions"
            android:width="120dp" >
        </EditText>
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textGoal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="@string/goal"
            android:textSize="15dp" />

        <EditText
            android:id="@+id/editGoal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="textNoSuggestions"
            android:width="200dp" >
        </EditText>
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/check_enable_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/from"
            android:textSize="15dp" />

        <EditText
            android:id="@+id/editFrom"
            android:enabled="false"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="date"
            android:ems="10" >
        </EditText>
    </TableRow>

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textfrom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="@string/to"
            android:textSize="15dp" />
        <EditText
            android:id="@+id/editto"
            android:enabled="false"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="date"
            android:ems="10" >
        </EditText>
    </TableRow>

</TableLayout>
And here is where I add the Items (don't pay attention to 'geldclass'.. Its a global class where the global array is stored):

Code:
public void onClick(View v) {
                Goals goal = new Goals("this is a new test");
                geldclass.addGoals(goal);
                goal_adapter = new listViewAdapter(GoalManager.this, geldclass.getGoals_list());
                goal_list.setAdapter(goal_adapter);
            }
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones