Question

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long   l) {

            if (selectedCourses.contains(courseNames.get(position))){
                selectedCourses.remove(selectedCourses.lastIndexOf(courseNames.get(position)));
                Toast.makeText(getApplicationContext(),
                      courseNames.get(position) + " removed", Toast.LENGTH_LONG)
                      .show();

                row = view;
                row.setBackgroundResource(R.drawable.charback);

                System.out.println(selectedCourses);
            }

            else{
                selectedCourses.add(courseNames.get(position));
                Toast.makeText(getApplicationContext(),
                        courseNames.get(position) +" added", Toast.LENGTH_LONG)
                          .show();

                row = view;
                row.setBackgroundResource(R.drawable.white);

                myarrayAdapter.notifyDataSetChanged();
                System.out.println(selectedCourses);

            }

The selected item turns white, but also about 12 items late another random item turns white just as it comes into view? Can someone please help me??

Was it helpful?

Solution

Try this:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long   l) {

        if (selectedCourses.contains(courseNames.get(position))){
            selectedCourses.remove(selectedCourses.lastIndexOf(courseNames.get(position)));
            Toast.makeText(getApplicationContext(),
                  courseNames.get(position) + " removed", Toast.LENGTH_LONG)
                  .show();

            row = view;
            row.setBackgroundResource(R.drawable.charback);

            System.out.println(selectedCourses);
        }

        else{
            selectedCourses.add(courseNames.get(position));
            Toast.makeText(getApplicationContext(),
                    courseNames.get(position) +" added", Toast.LENGTH_LONG)
                      .show();

            row = view;
            row.setBackgroundResource(R.drawable.white);

            System.out.println(selectedCourses);

        }

I removed myarrayAdapter.notifyDataSetChanged(); line....

OTHER TIPS

What will be happening is that your getView method in your adapter is not recycling/reusing the convertView that is supplied.

If the convertView is not null, that means it is a view that is being reused . That means you should explicitly set the background resource to the default, unless the item is selected. The reason you're seeing the white background re-used is because you're not resetting the background to the default.

Something like:

if (convertView != null) {
   if (selectedCourses.contains(courseNames.get(position))){
       convertView.setBackgroundResource(R.drawable.white);
   } else {
       convertView.setBackgroundResource(R.drawable.charback);
   }
}

You can use Holder class for this :

In your adapter class create another static class that holds your components: see below code

static class MyHolder {

        TextView mTextView;
        RelativeLayout lv;
        ImageView imgInvite;
        RelativeLayout rel_bckground;
    }

After that use it in your getView method :

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

           View row = convertView;
            holder = null;

          if (row == null) {

         // initialize your components
         holder = new MyHolder();
         holder.mImageView = (ImageView) row.findViewById(R.id.someID);

        }else{

         holder = (MyHolder) row.getTag();
             }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top