Can anyone tell me how I can find the relevant list item id for a touch event associated with a listview?
I want to set the background of a list item to a different color when the user presses down on it, and change it back when they release. I prefer to do this with code and not the background setting.
I already have an onclicklistener, but want to get this ontouchlistener working as well.
I know this is an old question. But if anyone else had this same question the solution is the pointToPosition method found in classes that derive AbsListView.
public boolean onTouch(View arg0, MotionEvent arg1) {
// ... other logic ...
int lListViewPosition = mYourListView.pointToPosition((int)arg1.getX(), (int)arg1.getY());
// get the item id
int lItemId = mYourListView.getItemIdAtPosition(lListViewPosition);
// or get the actual item
Object lYourItemObj = mYourListView.getItemAtPosition(lListViewPosition);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
//do Magic
}
Hope this helps!
Related
I've overridden onListItemClick in ListFragment to get the selected item.
The function works great the first time I open the fragment. But after I navigate to some different activities and return via the back button or finish(), the position reported is wrong. Do I need to do something to reset this fragment after it's returned to?
onListItemClick:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Brewery brewery = ((BreweryAdapter)getListAdapter()).getItem(position);
if (mCallbacks != null) {
mCallbacks.onBrewerySelected(brewery);
}
}
My BreweryAdapter is very simple- it just populates the view pieces with their data.
Any ideas? My code matches the examples I've looked at online. Thanks!
I have a ListView in multiple selection mode. Whenever I click an item, I want to handle that event. For this, I use the following logic.
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
OverlayTypes selected = (OverlayTypes) getListAdapter().getItem(
position);
boolean isChecked = getListView().getCheckedItemPositions().valueAt(
position);
Log.i(TAG, position+" is "+isChecked);
}
But whenever an item gets checked, getChecked is 'false' and 'true' vice versa. Why?
It might be an issue with timing, the value of the checked box may not be updating until after you have printed out to the LogCat.
Try changing your line
boolean isChecked = getListView().getCheckedItemPositions().valueAt(position);
to
boolean isChecked = l.getCheckedItemPositions().valueAt(position);
to see if reading the local copy instead of your global copy helps.
My problem was that I was overly ambitious in get the getView() method of my custom adapter. In getView(), I tried to set the Checkable view to the value that it has in my settings, not realizing that checked state is handled within the adapter.
If that makes sense.
In the following code, on click of list item I want to fetch variable present inside the object, How I can achive this
Below is code snippet
private void onListViewItemClick() {
// TODO Auto-generated method stub
// item click switch to next activity
listCustomListViewId.setOnItemClickListener(new OnItemClickListener() {
/* on click gets list view item id */
public void onItemClick(AdapterView<?> myAdapter, View myView,
int myItemInt, long mylng) {
// fetching clicked item id
Object o = listCustomListViewId.getItemAtPosition(myItemInt);
Log.i("Victory Item Id:.....", String.valueOf(o);
long strid = (long) (listCustomListViewId
.getItemIdAtPosition((int) mylng));
Log.i("Item Id...#######", String.valueOf(strid));
/* switch on next 'ListItemDeleteUpdateActivity' activity */
Intent intent = new Intent(FeedsActivity.this,
VictoryDetailActivity.class);
intent.putExtra("customElements", o.toString());
startActivity(intent);
}
});
}
I have tried to get data into Object but unable to fetch.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the data associated with selected item
Object item = l.getItemAtPosition(position);
String myitem = item.toString();
edittxt.setText("Selected item is :"+ myitem); // You can Set EditText from Here.
/* switch on next 'VictoryDetailActivity' activity */
Intent intent = new Intent(FeedsActivity.this,VictoryDetailActivity.class);
intent.putExtra("customElements",myitem);
startActivity(intent);
}
Hope this helps you.
On DataSource you have probably an collection of something. myItemInt represent the corresponding item in your collection to the item clicked. Use that.
If you have some views inside the clicked view you need to get, use findViewbyId on myView, like this:
myView.findViewbyId(R.id.myEditTextControl) //demo, use yours
If this answer is not enough for you, post some code from your adapter and tell us more details about what you need to do.
To handle events when the items of ListView is selected. You must override onListItemClick() method. The medthod have 4 parameters:
#Override
protected void onListItemClickonListItemClick(ListView l, View v, int position, long id)
To get information about ItemSelected, you just call getItemAtPosition(position) method to return a object which contains data.
It should be onListItemClick()
I am using a list view containing check boxes. Could you please help me how to identify which check box has the user selected? Basically I want different functions to be performed when a checkbox is clicked or a list item is selected similar to Android alarm clock.
does this help you:
how to display a list of checkboxes using dynamic data
That is how I did it with dynamic data.
Basically, in your listView class override:
#override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
boolean isClicked = l.isItemChecked(position);
// do something
}
I am using a custom list view. I have tried to get the value of the row which the user clicked.
Can anybody tell me how to get the value?
Thanks
You may want to implement a new method in your class, specifically onItemClick:
[...]
private ListView lv;
[...]
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
String itemValue = lv.getItemAtPosition(position);
[... do something ...]
}
Then you can do whatever you want with with itemValue.
Hope this helps.