Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top