Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - Keep ListView's item highlighted once one has been clicked
    text
    copied!<p>So I have an activity with 2 <code>ListView</code> widgets, when you select a value in the first one, the second is populated with values related to the selection in the first <code>ListView</code>. 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.</p> <p>I've got it working on the second <code>ListView</code> using the <code>android:listSelector="#CCCCCC"</code> XML Attribute, but this seems to be wiped clean once a <code>OnItemClickListener</code> is introduced into the mix (like the one I use on my first <code>ListView</code>).</p> <p><strong>So far here's what I've got:</strong></p> <p>Custom <code>OnItemClickListener</code> I found browsing various answer regarding this topic (slightly modified it in order for it to load my info the second ListView): </p> <pre><code>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&lt;?&gt; 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&lt;String&gt;(getApplicationContext(), R.layout.list_item_white, mClubs)); } } </code></pre> <p>Here's my <code>list_selector.xml</code> file : </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_selected="true"&gt;&lt;shape&gt; &lt;solid android:color="#CCCCCC" /&gt; &lt;/shape&gt;&lt;/item&gt; &lt;item android:state_selected="false"&gt;&lt;shape&gt; &lt;solid android:color="#FFFFFF" /&gt; &lt;/shape&gt;&lt;/item&gt; &lt;/selector&gt; </code></pre> <p>The method (OnItemClick) is called and executed, but the background of my <code>ListItem</code> stays the same color :/</p> <p>I can't believe that this simple task has proven so complicated.</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload