Reputation: 676
I want to get list item position from adapter when i touch on a list item. please suggest how can we do this. i was trying to get as follow:
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
int adapterIndex = listView.pointToPosition((int) motionEvent.getX(),(int) motionEvent.getY());
int firstViewItemIndex = listView.getFirstVisiblePosition();
int viewIndex = adapterIndex - firstViewItemIndex;
pos = listView.pointToPosition((int) motionEvent.getX(), (int) motionEvent.getY());
pos=pos-listView.getFirstVisiblePosition();
view.startDrag(null, shadowBuilder, view, 0);
return true;
}
}
but always got 0.
Upvotes: 0
Views: 950
Reputation: 4497
Try it:
MyListView.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long l) {
//action for position, for example one toast and show value of var "position"
}
});
Upvotes: 2
Reputation: 508
try below approch:
set list items positions in adapter views by setTag() and from onTouch method you can get item position by view.getTag() method.
Upvotes: 1
Reputation: 40503
In your getView(int position, View convertView, ViewGroup parent) { ..
method of the Adapter Class the position
is the current touched list item's position.
Upvotes: 0