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 6799075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:48:19+00:00 2026-05-26T18:48:19+00:00

I have a feeling this is due to some sort of rendering optimization or

  • 0

I have a feeling this is due to some sort of rendering optimization or something, but I’m not sure what.

I have a list view and when an item is selected, I change its background color so that it visually remains selected.

The problem occurs when the list extends beyond the screen (it scrolls), if I select an item near the top and then scroll down it also displays another selected item that was once out of the view (i can never see 2 selected items on the screen at the same time). This also works in the opposite way, if I select an item near the bottom and then scroll to the top, it will display another item as being selected.

Also another note, the distance between the selected items is not uniform, if i rotate my device into landscape, the distance between the selected items is less.

If it’s not clear what the problem is I also attached pictures.

This is with a minimal list, it only displays one selected item.
enter image description here

Here I have selected an item near the top.
enter image description here

After scrolling down (see scroll bar) it displays another item as being selected.
enter image description here

Now for the code.

The list fragment.

public class ResultListFragment extends ListFragment {

BookListAdapter mBooksArray;
BookData api;
View footer;
ListView list;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Activity parent = getActivity();    
        BookSearch app = (BookSearch) parent.getApplicationContext();

    api = app.bookAPI;

    mBooksArray = new BookListAdapter(/*some params*/);
    mBooksArray.currentActivity = parent;


}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        list = (ListView) inflater.inflate(R.layout.search_results, null);

    footer = inflater.inflate(R.layout.load_more, null);

    return list;
}

public void onActivityCreated( Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    list.addFooterView(footer);

    list.setAdapter(mBooksArray);
}

}

And the ArrayAdapter, the color gets changed in the onClick method.

public class BookListAdapter extends ArrayAdapter<Book> {

ArrayList<Book> books;
private BookData bookData;
Activity currentActivity;
final BookListAdapter self = this;
private View selected = null;



public void update() {
    currentActivity.runOnUiThread(new Runnable() {
        public void run() {     
            self.notifyDataSetChanged();
        }
    }); 
}

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

        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.search_list_view_item, null);
    }
    /*some code to set the image and text */
    v.setOnClickListener(new OnItemClickListener(position));
    return v;
}

public void onClick(int position, View view) {
            // this log always reports the correct position when i select a list item
    Log.i(new Integer(position).toString(), books.get(position).title);
    if(selected != null) {
        selected.setBackgroundResource(R.drawable.list_view_bg);
    }
    selected = view;
    selected.setBackgroundResource(R.color.listSelected);

}

private class OnItemClickListener implements OnClickListener{           
        private int mPosition;

        OnItemClickListener(int position){
            mPosition = position;
        }
        @Override
        public void onClick(View view) {
        BookListAdapter.this.onClick(mPosition, view);
        }               
    }
}
  • 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-26T18:48:19+00:00Added an answer on May 26, 2026 at 6:48 pm

    The ListView recycles views (the part with View v = convertView; if (v == null) { } ...), so you’re setting the background on a view that will be used again and again. Instead, you also need to set a “selected” flag on your model (the Book object itself). In the part of your getView that is commented out, you then say:

    if (book.isSelected()) { 
      v.setBackgroundResource(R.color.listSelected) 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a feeling this is not an easy task but I need to
I have a feeling this is a repost but I can't seem to find
I have a feeling this XML is not valid, can someone please explain why?
I have a feeling this is a stupid question but I can't find the
I have the feeling the answer to this question is no but I figured
I have a feeling I am completely borking this MySQL query but I'll ask
I have a feeling that I already know the answer to this one, but
I have a feeling this should be a common requirement, but I can't work
I have a feeling this is a rather trivial question, but I'm stumped. In
I have a feeling this isn't possible with the current API, but I have

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.