Android - Listview - Performitemclick

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! :)

List view performItemClick doesn't work

You can keep track the position of the current selected element:

OnItemClickListener listViewOnItemClick = new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
mSelectedItem = position;
mAdapter.notifyDataSetChanged();
}
};

And override the getView method of your adapter:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view = View.inflate(context, R.layout.item_list, null);

if (position == mSelectedItem) {
// set your color
}

return view;
}

Or

First you can create selector xml file like below in your drawable folder drawable/list_item_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_activated="true">
<shape android:shape="rectangle">
<solid android:color="#333333" />
<padding android:left="5dp" android:right="5dp" />
</shape></item>
<item><shape android:shape="rectangle">
<solid android:color="#222222" />
</shape></item>

</selector>

And then in your listview specify background as

android:background="@drawable/list_item_selector"

Error - with listview.performItemClick()

Where are you trying to use performClick()?

If calling performItemClick on the onCreate() method, there would be NullPinterException,

Because the screen is not fully visible to the user.

Try calling this method in onWindowFocusChanged() like below:

listview.getAdapter().getView(1, null, null).performClick();

Instead of that you can also use same delay after:

Handler().postDelayed(new Runnable() 
{
@Override public void run() {
listView.performItemClick(listView.getAdapter()
.getView(1, null, null), 0, listView.getAdapter().getItemId(1));
}
}, 2000);

Important note: This will work for single item that is item in position 1.

Also, If your question is all about using onWindowFocusChanged try checking this:

public void onWindowFocusChanged(boolean hasFocus) {

super.onWindowFocusChanged(hasFocus);

if(hasFocus)
Toast.makeText(context, text, duration).show();
}

Performing performItemClick

Try

mListView.performItemClick(mListView.getAdapter().getView(your_click_position, null, null), your_click_position, mListView.getItemIdAtPosition(your_click_position));

Or

mListView.performItemClick(mListView.getChildAt(your_click_position), your_click_position, mListView.getItemIdAtPosition(your_click_position));

instead of

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

ListView PerformItemClick not working

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.

ListView's performItemClick() causes IllegalStateException when called inside onLoadFinished()

The IllegalStateException occurs not because of calling listView.performItemClick() but because this call was ultimately causing a fragment transaction. Fragment transactions are disallowed in asynchronous callbacks like onLoadFinished(). I found some useful information here.



Related Topics



Leave a reply



Submit