Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Jakub Michalewski
Jakub Michalewski
2,773 Points

Android SimpleCursorAdapter how to keep item styling on scroll

I have a Simple Cursor Adapter which works fine and displays all the data. My listener changes the color on click:

listViewM.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    boolean exists = false;
                    TextView item = (TextView) view.findViewById(R.id.r_lv_name);
                    String selectedAnswer = item.getText().toString();
                    MultiSelection multiSelection = new MultiSelection((int) id, selectedAnswer);

                    for (MultiSelection mm : mMultiSelectionsArray) {
                        if (id == mm.getId()) {
                            mMultiSelectionsArray.remove(mm);
                            exists = true;
                            break;
                        } else {
                            exists = false;
                        }
                    }

                    if (!exists) {
                        mMultiSelectionsArray.add(multiSelection);
                        item.setTextColor(Color.parseColor("#2EFE2E"));
                    } else {
                        mMultiSelectionsArray.remove(multiSelection);
                        item.setTextColor(Color.parseColor("#000000"));
                    }

                }
            });

Now on scroll adapter is recycling views and marking new items as selected (by adding the color). I guess I need to keep status somehow and then apply it on view creation but after 3 days of looking I give up. Can anyone help please?