2

When the myListView item is selected manually it is highlighted but when the selection is made programmatically (via myListView.performItemClick(myListView.getAdapter().getView(17, null, null), 17, 17);) the item is not highlighted event if I add this 2 call :

l.setSelection(position);
l.setItemChecked(position, true);

Please let me know how to highlight listview item selected programmatically.

Thanks.

public class PlaylistActivity extends ListActivity {

private static ListView myListView;                                 

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);

    easyTracker  = EasyTracker.getInstance(PlaylistActivity.this);

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

    if (!MainActivity.playlistItem.isEmpty())
        MainActivity.playlistItem.clear();

    for (Story aStory : MainActivity.stories) {
        if (aStory.get_text() != null && ((aStory.get_text()).toLowerCase(Locale.getDefault()).
                contains("5"))){
            MainActivity.playlistItem.add(aStory);
        }
    }

    setListAdapter(new MyStoryAdaptater(PlaylistActivity.this,R.layout.activity_mainview, MainActivity.playlistItem));

myListView = getListView();
myListView.setSelector(android.R.color.darker_gray);
myListView.performItemClick(myListView.getAdapter().getView(17, null, null), 17, 17);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    l.setSelection(position);
    l.setItemChecked(position, true);

}
}
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • 1
    possible duplicate of [Programmatically select item ListView in Android](http://stackoverflow.com/questions/10788688/programmatically-select-item-listview-in-android) – Rohit5k2 Jan 10 '15 at 23:38
  • 1
    It not a duplicate because I'm not asking how to select but how to highlight. –  Jan 10 '15 at 23:40
  • Answer of that question solves both. If it doesn't work for you then no problem. – Rohit5k2 Jan 10 '15 at 23:41
  • Did you try `requestFocus()` or `requestChildFocus()`? – Rohit5k2 Jan 10 '15 at 23:43
  • Yes i tryed requestFocus() but not requestChildFocus(). I gonna try it. –  Jan 11 '15 at 00:19
  • You need to have a proper background selector defined for the list items, as shown in the answer that @Rohit5k2 pointed out. – GreyBeardedGeek Jan 11 '15 at 00:27

0 Answers0