Android - ListView - performItemClick

I encounter some difficulties when I try to use the performItemClick ListView function.

All I want to do is click programmatically in the first element of the list.

How can i do this? I looked at this function in the documentation, but I did not quite understand its parameters.

I tried something like:

myListView.performItemClick(myListView.getChildAt(0), 0, myListView.getChildAt(0).getId()); 

But this did not work (myListView.getChildAt(0) returns null)

Thank you in advance!

+37
android listview click
Nov 11 '11 at 12:54
source share
22 answers
 mList.performItemClick( mList.getAdapter().getView(mActivePosition, null, null), mActivePosition, mList.getAdapter().getItemId(mActivePosition)); 

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

+59
Apr 29 '13 at 11:54 on
source share

It worked for me.

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

use adapter to get view for item position. I do not need the remaining 2 parameters, so I left them zero. Leaving with null conversion causes the adapter to render a new view. This is a performance issue, but since it only happens once in a while, it will not have much effect. I do not need to specify a parent for anything, because I do not use it.

Position

- This is the place where your item is located. In addition, these 2 lines of code in front of your performItemClick function create the illusion of selecting a list item. They also ensure that the corresponding item is on the screen.

 listView.requestFocusFromTouch(); listView.setSelection(position); 
+23
Feb 03 2018-12-23T00:
source share

This works best for me. Run this in 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 he will put the message in the main thread to give ListView some time to run.

+12
Jun 13 '13 at 19:04 on
source share

I went with

 listView.getAdapter().getView(position, null, null).performClick(); 
+4
Jan 01 '13 at 20:48
source share

I tried the code below and it worked.

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

The first parameter (view) may be zero.

+3
Jan 25 '12 at 10:38
source share
 mList.performItemClick( mList.getChildAt(mActivePosition), mActivePosition, mList.getAdapter().getItemId(mActivePosition)); 

where mActivePosition is the position of the child view in the list view.

+2
May 28 '15 at 13:30
source share

Using the suggested @sulal code, you can put it in onLoadFinished if you use LoaderManager. For example, something like

 @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { //.... // mSelectedId keeps the currently selected id // INVID is an invalid value if (mSelectedId == INVID) { // nothing selected // sulal code new Handler().post(new Runnable() { @Override public void run() { mList.performItemClick( mList.getChildAt(mActivePosition), mActivePosition, mList.getAdapter().getItemId(mActivePosition)); mSelectedId = mList.getAdapter().getItemId(mActivePosition); } }); } 

mActivePosition can be 0 (i.e. the position on the first element) or the position stored during, for example, onPause

+1
Jun 26 '13 at 21:25
source share

At first I tried to use this code in my fragment (Master / Detail -> NameListFragment)

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

But that did not work. When I made the @Override onStart() method in the fragment, and I moved my code to onStart() . After that, it works right for me.

+1
Apr 04 '14 at 11:53 on
source share

I worked with performItemClick and it worked for me, but with a little problem. I tried to color the background of the pressed item, but it never worked, just notice that if you pass the transformation as NULL (second parameter) and then try to do something in OnClick (), this will not lead to the beacuse effect, getView method () the adapter will give you an excellent View object. you need to manipulate your adapter's getView () method to catch the View so you can activate it.

+1
Apr 09 '14 at 6:26
source share

If you work in case of unit test. Try using getInstrumentation (). WaitForIdleSync () to wait for the list to load and the ActivityInstrumentationTestCase2 extension. See this answer .

+1
May 27 '14 at 17:48
source share

I just encounter this freak problem today and I try my best to deal with it. My condition, when I first start the layout, I need to make some element checked. But when I use gridView.getChildAt (position), I always return null. I met this problem before, caused by not finishing the drawing layout. Therefore, I am sending a message. handler.postDelayed (.., ..), It works. Thanks for driving this Exception.

+1
Mar 18 '15 at 6:46
source share

it may be old, but it may help:

 lvList.performItemClick(null, index, lvList.getItemIdAtPosition(index) ); 

NOTE: the first parameter is null and will still work if you have a custom adapter, convertView will be populated with a custom layout and view, etc.

- checks / lucky encodings.

+1
Apr 15 '15 at 5:40
source share

This work for me. If you get a strange result when using getView, this is because the list item you want does not exist in the visible parts. Use below:

 private View getViewFromAdapterByPosition(int position, ListView listView) { View view; int firstVisiblePos = listView.getFirstVisiblePosition(); int lastVisiblePos = listView.getLastVisiblePosition(); if (position < firstVisiblePos || position > lastVisiblePos) { view = listView.getAdapter().getView(position, null, listView); } else { view = listView.getChildAt(position - firstVisiblePos); } return view; } 

And then,

 listView.performItemClick(getViewFromAdapterByPosition(index, listView), index, 0); 
+1
Dec 12 '15 at 2:04
source share

If you are using Listview (a simple array adapter or a custom adapter), define a listview and the others finally 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 setOnItemClickListener you should call execute click. Otherwise, it will be wrong.

+1
Feb 24 '16 at 10:13
source share

Try the following:

 public static boolean performClicKOnLisViewFromIndex(ListView listView, int index){ if(listView != null){ if(listView.getAdapter()!= null && listView.getAdapter().getCount() >0 && listView.getAdapter().getCount() > index ){ listView.performItemClick( listView.getAdapter().getView(index, null, null), index, listView.getItemIdAtPosition(index)); return true; } } return false; } 
+1
Aug 16 '17 at 2:47 on
source share

The executeClick function is likely to be called before filling in the list, placing a breakpoint in getView and on performItemClick and the check that is called first

0
Nov 11 2018-11-11T00:
source share

getListView().performItemClick(null, 0, 0) helped (for position 0 ).

0
Apr 22 '13 at 12:03
source share

This worked for me:

 listView.getAdapter().getView(1, null, null).performClick(); 
0
Jan 14 '15 at 2:37
source share

Decrease in experience .

using listview1.performItemClick also starts your listview1.OnItemClickListener if you use a listener with the same list in your code.

 Hope It helps 
0
Sep 03 '15 at 11:05
source share

If you get a strange result when using getView , this is because the list item you want does not exist in the visible parts. Use below:

 private View getViewFromAdapterByPosition(int position, ListView listView) { View view; int firstVisiblePos = listView.getFirstVisiblePosition(); int lastVisiblePos = listView.getLastVisiblePosition(); if (position < firstVisiblePos || position > lastVisiblePos) { view = listView.getAdapter().getView(position, null, listView); } else { view = listView.getChildAt(position - firstVisiblePos); } return view; } 

And then,

 listView.performItemClick(getViewFromAdapterByPosition(index, listView), index, 0); 
0
Nov 12 '15 at 5:16
source share

This works for me:

 listview.getSelectedView().performClick(); 
0
Jun 12 '17 at 12:51 on
source share

This is from the start of Android games. It creates a simple list of items that you can click to open a new action. Of course, each list item must be added to AndroidManifest.xml as a separate action with the name .ListItem #.

 public class MainActivity extends ListActivity { String tests[] = { "ListItem1", "ListItem2", "ListItem3", "ListItem4"}; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tests)); } @Override protected void onListItemClick(ListView list, View view, int position, long id) { super.onListItemClick(list, view, position, id); String testName = tests[position]; try { Class<?> classInstance = Class.forName("your.package.name." + testName); Intent intent = new Intent(this, classInstance); startActivity(intent); } catch (ClassNotFoundException e) { e.printStackTrace(); } } 

}

-four
Nov 11
source share



All Articles