2

So I have ListView with the Single Choice mode enable. I have a selector for the background of the list item and it changes correctly to the activated state.

Using .setSelected() doesn't.

Even if I get the view for that list item and set its state to activated it still doesn't work.

So far this is the best solution I have come up with but the setActivated isn't working basically I have duplicated the code of the onItemClickListener but added setActivate().

InformationListAdapter adapter = new InformationListAdapter(getActivity().getApplicationContext(), info.infoSections);
lv.setAdapter(adapter);  
View v=adapter.getView(0, null, null);
v.setActivated(true);

InformationSection item = (InformationSection)lv.getItemAtPosition(0);
InformationWebFragment webFrag = (InformationWebFragment) getFragmentManager().findFragmentById(R.id.InfoWebFragment);
webFrag.updateDisplay(item);
2
  • Is that the code from your onItemClickListener? What are you trying to accomplish with the onClick?
    – MrZander
    Nov 22, 2011 at 20:28
  • I have exactly the same problem. I am stuck on it. How have you solved it?
    – Sotti
    Jan 1, 2014 at 17:55

2 Answers 2

12

To autoselect the first item call:

listView.setItemChecked(0, true);
1
  • 1
    Make sure this should be after bind to the adapter
    – Chaitu
    Oct 28, 2013 at 6:59
1

If you have access to the View which I suppose the line: View v=adapter.getView(0, null, null); gives you - then you can do this:

v.performClick();

That will fire the onClick-event and run the onClickListener for the specific View.

Edit

As we discovered in our discussion below one should call:

listView.performItemClick(View view, int position, long id);
3
  • performClick is returning false which means that it doesn't have a click listener attached but I definitely have a onItemClickListener attached to the listView
    – draksia
    Nov 22, 2011 at 20:45
  • Then the View returned from adapter.getView(0, null, null) is probably not the one you think. Nov 22, 2011 at 20:48
  • It turns out It needs to listview.performItemClick If you change your answer I can will give you credit.
    – draksia
    Nov 22, 2011 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.