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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:58:57+00:00 2026-05-29T21:58:57+00:00

So I have an activity with 2 ListView widgets, when you select a value

  • 0

So I have an activity with 2 ListView widgets, when you select a value in the first one, the second is populated with values related to the selection in the first ListView. This mechanic works without a problem, but now I want the user choices to stay highlighted. I’ve read a good ammount of question related to this topic and it seems there is a myriad of ways one can accomplish this but after trying about 4-5 of em’ I still can’t get it to work.

I’ve got it working on the second ListView using the android:listSelector="#CCCCCC" XML Attribute, but this seems to be wiped clean once a OnItemClickListener is introduced into the mix (like the one I use on my first ListView).

So far here’s what I’ve got:

Custom OnItemClickListener I found browsing various answer regarding this topic (slightly modified it in order for it to load my info the second ListView):

private class ItemHighlighterListener implements OnItemClickListener {

    private View oldSelection = null;

    public void clearSelection() {
        if(oldSelection != null) {
            oldSelection.setBackgroundColor(android.R.color.transparent);
        }
    }

    public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
        clearSelection();
        oldSelection = view;
        view.setBackgroundDrawable(view.getContext().getResources().getDrawable(R.drawable.list_selector));
        loadClubs(mXMLPortalOptions.getRegion(pos).getId());
        mClubList.setAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.list_item_white, mClubs));
    }
}

Here’s my list_selector.xml file :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_selected="true"><shape>
            <solid android:color="#CCCCCC" />
        </shape></item>

    <item android:state_selected="false"><shape>
            <solid android:color="#FFFFFF" />
        </shape></item>

</selector>

The method (OnItemClick) is called and executed, but the background of my ListItem stays the same color :/

I can’t believe that this simple task has proven so complicated.

If I have omitted code that could be useful or if my question is lacking details, feel free to point that out and I’ll do my best to explain myself.

  • 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-29T21:58:58+00:00Added an answer on May 29, 2026 at 9:58 pm

    Put a position variable for selected item. Change the position in onItemClicked() method. Check the selected position in List Adapter inside getView() and set the background for the selected item.

    public class TestAdapter extends BaseAdapter
    {
        private Context context;
        private ArrayList<TestList> testList;
        private int selectedIndex;
        private int selectedColor = Color.parseColor("#1b1b1b");
    
        public TestAdapter(Context ctx, ArrayList<TestList> testList)
        {
            this.context = ctx;
            this.testList = testList;
            selectedIndex = -1;
        }
    
        public void setSelectedIndex(int ind)
        {
            selectedIndex = ind;
            notifyDataSetChanged();
        }
    
        @Override
        public int getCount()
        {
            return testList.size();
        }
    
        @Override
        public Object getItem(int position)
        {
            return testList.get(position);
        }
    
        @Override
        public long getItemId(int position)
        {
            return position;
        }
    
        private class ViewHolder
        {
            TextView tv;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View vi = convertView;
            ViewHolder holder;
            if(convertView == null)
            {
                vi = LayoutInflater.from(context).inflate(R.layout.test_list_item, null);
                holder = new ViewHolder();
    
                holder.tv = (TextView) vi;
    
                vi.setTag(holder);
            }
            else
            {
                holder = (ViewHolder) vi.getTag();
            }
    
            if(selectedIndex!= -1 && position == selectedIndex)
            {
                holder.tv.setBackgroundColor(Color.BLACK);
            }
            else
            {
                holder.tv.setBackgroundColor(selectedColor);
            }
            holder.tv.setText("" + (position + 1) + " " + testList.get(position).getTestText());
    
            return vi;
        }
    
    }
    

    Now set the selectedIndex variable when a list item clicked.

    public class TestActivity extends Activity implements OnItemClickListener
    {
        // Implemented onItemClickListener
    
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            adapter.setSelectedIndex(position);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an activity which have ListView. When I want to access this ListView
I have Activity with ListView inside it and in the onCreate method of the
I have an Activity in Android, with two elements: EditText ListView When my Activity
I have ListView activity class: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] naziviMolitvi =
I have an Activity with an AutoCompleteTextView and a ListView with an adapter that
I have an Activity with the ListView with text items inside. When user presses
I have created an Activity that shows a listview and on swipe action another
In my example activity, I have - a ListView containing - multiple HorizontalScrollView containing
I have Activity with ListView and buttons - clear all and OK. On button
I have a listview extends Activity and use custom adapter because I want icon

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.