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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:11:27+00:00 2026-06-10T12:11:27+00:00

I did my custom ArrayAdapter to highlight the selected item of a ListView. The

  • 0

I did my custom ArrayAdapter to highlight the selected item of a ListView.
The code is as following:

public class HeroListViewAdapter extends ArrayAdapter<Hero> {

public static int NO_POS = -1;

private List<Hero> heroes;
private int selected_pos = NO_POS;

public HeroListViewAdapter(Context context, List<Hero> heroes){
    super(context, R.layout.hero_list_item_view, heroes);
    this.heroes = heroes;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = GuiBuilder.createHeroListItemView(heroes.get(position),getContext(),parent,false);
    if(position == selected_pos){
        rowView.setBackgroundColor((rowView.getResources().getColor(R.color.list_item_selected_color)));
    }
    return rowView;
}

public void setSelectedPosition(int selected_pos){
    this.selected_pos = selected_pos;
}

public int getSelectedPosition(){
    return selected_pos;
}

public Hero getSelectedHero(){
    if(selected_pos>=0 && selected_pos<heroes.size())
        return this.getItem(selected_pos);
    else
        return null;
}

}

where the methods modify an inner member to keep track of the selected position and the getView method test the value of this inner member in order to highlight the item by changing his background. Things are used in the following way:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
   list_adapter.setSelectedPosition(position);
   listView.invalidate();
}
});

Now…everything works fine as intended on my 2.2.1 Froyo (level API 8) smartphone. But running the application on the emulator (level API 16) when I click an item the background doesn’t change.
So the question is: is this an API issue (and backward compatibility?) or an emulator one? Do I have to worry about that the application may not work as intended on other devices than mine.

  • 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-06-10T12:11:29+00:00Added an answer on June 10, 2026 at 12:11 pm

    The fact that it works on smartphone is a pure luck. This isn’t intended to work.

    listView.invalidate() just isn’t enough. You should call notifyDataSetChanged() in you adapter after list_adapter.setSelectedPosition(position) call.

    invalidate() just redraws a view. Adapter’s getView() is called during list view layout run (not draw run) and it is getView() where you change background.

    Also you can call listView.requestLayout() instead of listView.invalidate(). It’s ugly but it should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom list adapter: class ResultsListAdapter extends ArrayAdapter<RecordItem> { in the overridden
Did I not get enough sleep or what? This following code var frame=document.getElementById(viewer); frame.width=100;
I created a custom event class, and a EventDispatcher-derived class. What i did, is
I need a custom layer (extended by new variable and a method) and did
I moved some hardcoded settings to a custom Section (so I did not use
I already did my studies, I know how to make a custom formatter, or
I wanted to make a basic custom keypad for the iPhone , I did
Did you ever have the following situation: you need to store information, but a
I am trying to create my 'custom IsPostBack', all I did was create a
I did a test on custom dependency properties on WPF userControls, and does not

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.