Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - ListView - performItemClick

mList.performItemClick(
        mList.getAdapter().getView(mActivePosition, null, null),
        mActivePosition,
        mList.getAdapter().getItemId(mActivePosition));

Where mActivePosition is your click position! All the best! :)


This worked for me.

listView.performItemClick(
    listView.getAdapter().getView(position, null, null), position, position);

use the adapter to get the view for the position of the item. The other 2 parameters I didn't want so I left them null. Leaving convertView null causes the adapter to render a new view. It's a performance issue but since this is only happening once in a while it wont have much effect. I don't need to specify the parent for anything because I'm not using it.

position is just the spot where your item is located. Additionally these 2 lines of code before your performItemClick create the illusion of having the list item selected. They also ensure the appropriate item is on the screen.

listView.requestFocusFromTouch();
listView.setSelection(position);

This works best for me. Run this on the main thread.

new Handler().post(new Runnable() {
    @Override
    public void run() {
        mList.performItemClick(
                mList.getChildAt(mActivePosition),
                mActivePosition,
                mList.getAdapter().getItemId(mActivePosition));
    }
});

This is similar to Arun Jose's answer, but it will queue a message to the main thread to give the ListView some time to initiate.


I tried the code below and it worked.

getListView().performItemClick(null, 0, getListAdapter().getItemId(0));

The first parameter (view) can be null.


I went with

listView.getAdapter().getView(position, null, null).performClick();

When using Listview (simple array adapter or custom adapter) define listview and other finally make perform click.

For example:

 //At onCreate function:

lv = (ListView) findViewById(R.id.listView);
        lv.setAdapter(new CustomAdapter(List_item.this, list, images));


lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)    {
// on click function works
    }
}


int position = 0;
lv.performItemClick(lv.getAdapter().getView(position, null, null), position, lv.getAdapter().getItemId(position));

Note: After creating the setOnItemClickListener only you should call perform click. Otherwise, it will not correctly.