ListView SelectionListener Issue

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

ListView SelectionListener Issue

Postby cvance383 » Tue Feb 24, 2009 9:32 pm

Before responding, please understand that I have googled this extensively and read the API. Now for my problem...

I have a listview (actually two) that I want to do a custom selection highlight for (where i use a custom background color, change the text color, and change the icon). So I wrote a custom SelectionListener class that implements AdapterView.OnItemSelectedListener, in the onItemSelected() method I set all the children of the adapterview to the default background and text color and default icon, then I set the selected child view to the special background color, and icon. In the onnothingselected method I set all the children to have the default background and icons. When I run my app, when I scroll through the list it works, the selected item has the right background and stuff and the others are default, but when I scroll back to the top of the list and then off the listview, the top element stays selected or highlighted. WTF? The tab I have at the top is also selected, so it appears two things are selected (the tab and first item in the list). Thanks for any help.

Here is my selectionlistener code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class OptionsSelectionListener implements AdapterView.OnItemSelectedListener
  3.  
  4. {
  5.  
  6.  
  7.  
  8.         @Override
  9.  
  10.         public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
  11.  
  12.         {
  13.  
  14.                 unSelectAll(parent);
  15.  
  16.                 ((OptionView) view).highlight();
  17.  
  18.         }
  19.  
  20.  
  21.  
  22.         @Override
  23.  
  24.         public void onNothingSelected(AdapterView<?> parent) {
  25.  
  26.                 unSelectAll(parent);
  27.  
  28.         }
  29.  
  30.        
  31.  
  32.         public void unSelectAll(AdapterView<?> parent)
  33.  
  34.         {
  35.  
  36.                 int numChildren = parent.getChildCount();
  37.  
  38.                 for(int k = 0; k < numChildren; k++)
  39.  
  40.                 {
  41.  
  42.                         ((OptionView) parent.getChildAt(k)).unhighlight();
  43.  
  44.                 }
  45.  
  46.         }
  47.  
  48. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
cvance383
Freshman
Freshman
 
Posts: 9
Joined: Tue Feb 03, 2009 11:18 pm

Top

Postby cvance383 » Thu Feb 26, 2009 6:54 pm

do people not understand the problem or just don't know a solution? is it a known bug?
cvance383
Freshman
Freshman
 
Posts: 9
Joined: Tue Feb 03, 2009 11:18 pm

Postby jagtap.jj1 » Fri Feb 27, 2009 6:09 am

can u explore your problem bit specific
java-android developer
User avatar
jagtap.jj1
Senior Developer
Senior Developer
 
Posts: 124
Joined: Fri Aug 22, 2008 11:52 am
Location: India(Mumbai)

Postby michael_g_hu » Fri Feb 27, 2009 8:18 am

I know this problem. first, you should not change your text color or background in the OnItemSelectedListener, for it will cause two problems. the first one as you described, the status of the items problem, and the second one is that there will have delay when you scroll quickly, do the color and backgournd change in your xml file will solve this problem , but about the icon, you changed the item's value, it needs another solution.
michael_g_hu
Developer
Developer
 
Posts: 35
Joined: Fri Mar 21, 2008 9:46 am

Postby cvance383 » Fri Feb 27, 2009 10:31 pm

i don't follow. so i'm not supposed to use an onitemselectedlistener? what do you mean by using xml, can you give me an example?
cvance383
Freshman
Freshman
 
Posts: 9
Joined: Tue Feb 03, 2009 11:18 pm

Postby michael_g_hu » Sat Feb 28, 2009 5:22 am

about the text color, you can use "setColor" to set the colorlist of the text, you should define the two states of color with "default" and "selected" just like in the color/text_color.xml, and use textView.setColor(R.color.text_color)
currently, i'm on my vacation, I don't have an example, try to google with color state of android
michael_g_hu
Developer
Developer
 
Posts: 35
Joined: Fri Mar 21, 2008 9:46 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests