Having trouble getting ListView to show selected item as highlighted

by Timothy DeWees » Sat, 27 Dec 2008 07:08:09 GMT


Sponsored Links
 Hello,

I've seen a few post on this but have never seen or understood the
solution.  I have an application that is showing a list of items from
a database.  I extended the CursorAdapter and added an icon in front
of the item to easily show type.

When I navigate the items using the track-pad, it shows the selected
item; however, when I click the item, the selection goes away.  It's
still there and when I use the track-pad again, it seems to be getting
the focus back and then I can scroll.

What I want to happen is for the list item to appear highlighted when
I click it just like when I navigate to it with the trackpad.

Any suggestions?

Here are my onClick and onItemSelected:

    @Override
    protected void onListItemClick(ListView l, View v, int position,
long id) {
        super.onListItemClick(l, v, position, id);
        setSelection(position);
        movieId = id;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mDbHelper = new MoviesDbAdapter(this);
        mDbHelper.open();
        Button filterButton = (Button)findViewById
(R.id.filter_button);
        filterButton.setOnClickListener( new View.OnClickListener() {
                public void onClick(View v){
                        EditText filter = 
(EditText)findViewById(R.id.filter_text);
                        filterExpr = filter.getText().toString();
                        fillData();
                }
        });
        getListView().setAddStatesFromChildren(false);
        getListView().setItemsCanFocus(true);
        getListView().setOnItemSelectedListener( new
OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> parent, View v, int
possition, long id) {
                        v.setSelected(true);
                        movieId = id;
                }
                public void onNothingSelected(AdapterView<?> parent) {
                        parent.setSelection(-1);
                }
        });
        fillData();
    }

--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. problem with Hello, Gallery R.styleable not resolve

Thanks for catching the typo!  I should have just done a copy/paste... :)
Glad I could help somewhat.

Thanks,
Justin
----------------------------------------------------------------------
There are only 10 types of people in the world...
Those who know binary and those who don't.
----------------------------------------------------------------------


On Mon, Aug 31, 2009 at 9:39 PM, Bui Quang Hao(Johny.Bui) <








--~--~---------~--~----~------------~-------~--~----~

2. Create New TextView Style....

I want to create a new style that slightly modifies the default style
of a TextView...  However, I have not been able to find what the value
for the parent attribute should be...

Any help would be appreciated!

Current xml code:

<?xml version="1.0" encoding="utf-8"?>
<resources>
        <style name="TitleText" parent="??????????">
                <item name="android:textSize">25sp</item>
                <item name="android:textColor">#008</item>
        </style>
</resources>
--~--~---------~--~----~------------~-------~--~----~

3. Showing directions or drawing required path on Google Maps

4. setPadding() in onDraw() causing ListView draw loop

5. Problem in Geoceder Address and Country name are not Displaying

6. Services - best way to bind

7. TextView Line Breaks