2

This question has been asked a lot of times and I've systematically gone through each and tried to find which version of performItemClick works. Unfortunately I just can't get any of them to work despite my onclick method being called.

I have a listview with 5 items embedded into a fragment. This fragment is controlled by a fragmentstatepageradapter and a viewpager. I'm trying to restore the saved state.

        lv.setAdapter(new SimpleAdapter(getActivity(), list,
                R.layout.list_imageview, new String[] { "answer" },
                new int[] { android.R.id.text1 }));

The simple adapter takes in a custom layout which has a textview and imageview within a layout. Initially the imageview is null and on click it is set to a tick or cross. This works when clicking the items myself just doesn't work programatically as follows:

@Override
    public void onViewStateRestored(Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        if (savedInstanceState != null) {
            for (int i = 0; i < 5; i++) {
                lv.performItemClick(lv.getAdapter().getView(i, null, null),
                        i, i);
                Log.e("restoring state", "" + i);
            }
        }
    }

I can confirm that onViewStateRestored is called and onItemClick is also called. I assume it's a problem in the way I'm performing the item click. I am currently just trying to get it work; I know I haven't yet checked which items have been clicked to selectively click them but that's an easy boolean[] away.

I thank you for your help, and sorry for such a simple question that's been repeated quite a few times; despite reading them I am still unable to get them to work.

AndroidPenguin
  • 3,445
  • 2
  • 21
  • 42

1 Answers1

1

I assume it's a problem in the way I'm performing the item click.

Right now you call performItemClick giving it a new View created by the getView method of the adapter. The problem is that if you setting the image is somehow related to the row view the onItemClick callback receives, you'll not see any results as that view is not related(it's "in the air") to the row view that is actually seen on the screen(or present in the ListView). But I'm just guessing.

Anyway, you shouldn't tie that ImageView work to the OnItemClickListener, you should implement it at the adapter level(and call notifydataSetChanged()), especially as you're trying to set multiple rows at once.

user
  • 86,916
  • 18
  • 197
  • 190
  • That explains it amazingly! I'm trying that – AndroidPenguin Jan 26 '13 at 10:55
  • Testing it looks like that it definitely isn't taking the IDs of the original shown list but indeed getting a new view. How do I implement it at the adapter level? (Please and thanks) – AndroidPenguin Jan 26 '13 at 10:58
  • @AndroidPenguin I don't know what you're doing in the `onItemClick` method(and I also didn't see that you use `SimpleAdapter`, aren't the ImageViews repeating when you scroll?>). I would modify the `list` that backs the adapter so each row `Map` includes another column named `state` to hold the state of the `ImageView`. In the `onViewStateRestored` I would then update the `state` column in each map and call `notifyDataSetChanged`. You'll need to make some modifications on how you work with the adapter. See this https://gist.github.com/4641788 – user Jan 26 '13 at 11:21