0

I have a layout that consists of 3 contiguous ListViews. Selecting an item in the left most list will start the second list, and so on. I am trying to begin the fragment with the first item in the left most list selected, and because a lot of logic is required, I am attempting to trigger on click instead of redo-ing all the logic. I am using this:

mListView.performItemClick(mListView, 0, mListView.getItemIdAtPosition(0));

Is what I am attempting possible? and if it is, where do I call this?

ANSWER:

So it seems that the reason this wasn't working for me is because I was manually highlighting a View within the row View and it wasn't instantiated yet (or something like that).

I used this code to call it after the ListView was laid out:

mListView.post(new Runnable() {
        @Override
        public void run() {
            mListView.performItemClick(mListView, 0, mListView.getItemIdAtPosition(0));
        }
    });

EDIT:

I have 4 lists that depend on data being sent from the previous ones before being instantiated, and for some strange reason, this runnable method only works for the first list. For the others, I used this answer: https://stackoverflow.com/a/15399035/1549672. Also, strangely enough, this does not work on the first list. All the lists are nearly identical.

Community
  • 1
  • 1
user1549672
  • 486
  • 1
  • 6
  • 16
  • I don't really see the benifit of your current code over attaching the same `OnItemClickListener` to each of the `ListView`s, unless I'm not understanding something correctly. Personally though, for the sake of readability, I would consider having separate listeners for the 3 lists. The basic flow should not require a huge amount of logic. – MH. Aug 20 '13 at 19:26
  • Each of the lists have their own listeners. What I want is when the fragment that contains the 3 lists is first created, I want the first item of the first list to be clicked which will open its corresponding 2nd list etc. – user1549672 Aug 20 '13 at 19:33
  • Basically, the lists are made so that each list is dependent on the previous one so I can't just open them – user1549672 Aug 20 '13 at 20:35
  • How about you use an `OnItemSelectedListener` in stead? I'd prefer calling `listview.setSelection(index)` over programmatically sending click events. – MH. Aug 20 '13 at 21:03
  • So setSelection will trigger the OnItemSelectedListener? I'll try that and get back to you – user1549672 Aug 20 '13 at 21:38
  • Okay turns out my issue now is that when I select an item in the list, I change the background of a part of the row. I'm trying to select the first item in onResume() but when the fragment is first created, I guess the view within the row isn't created yet... – user1549672 Aug 20 '13 at 21:49
  • 1
    You can try wrapping the selection change in a `Runnable` and post that to the `ListView`. That way the selection change will not happen until the `ListView` has been measured and laid out etc. I don't think it would really matter though, as long as you set the adapter before attempting to change the selection. – MH. Aug 20 '13 at 21:56
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35872/discussion-between-user1549672-and-mh) – user1549672 Aug 20 '13 at 22:19
  • Posting to the view with a runnable worked! If you answer the question, I'll accept it – user1549672 Aug 20 '13 at 22:28
  • Sorry, was afk for a second there. Glad to hear that worked out though - find the comment as answer below. – MH. Aug 20 '13 at 22:42

1 Answers1

0

As per earlier comment:

You can try wrapping the selection change in a Runnable and post that to the ListView. That way the selection change will not happen until the ListView (and its children) has been measured and laid out etc. Do make sure you set the adapter before attempting to change the selection.

MH.
  • 45,303
  • 10
  • 103
  • 116