Get Listview Item Position on Button Click

Get listview item position on button click

do you execute this

    btnNxt = (Button) findViewById(R.id.btnNext);
btnNxt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//Here I need to get that position
});

inside the getView method?
if so it's very easy

     btnNxt = (Button) findViewById(R.id.btnNext);
btnNxt.setTag(position);
btnNxt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int position=(Integer)arg0.getTag();
});

Android Listview with Button get Item position

Try setting a click listener in your Adapter's getView method

NOTE: since you did not post your foodlist.xml, please change "R.id.buttonId" to reference the ID value you set for the button's android:id attribute in your foodlist.xml

// changes have been done here in order to fit the buttons in textview55+ changes to large/foodlist.
setListAdapter(new ArrayAdapter<ListItemSalads>(this, R.layout.foodlist, R.id.textview55, ListItemSalads) {
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View inflatedView = super.getView(position, convertView, parent);

// set a click listener
// TODO change "R.id.buttonId" to reference the ID value you set for the button's android:id attribute in foodlist.xml
inflatedView.findViewById(R.id.buttonId).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Button 1 clicked for row position=" + position, Toast.LENGTH_SHORT).show();
}
});
return inflatedView;

}
});
getWindow().getDecorView().setBackgroundColor(Color.BLACK);
//btnadd.setOnClickListener is not needed anymore

How to get position of a button in a listview?

Assuming the button is within the listview.
First define a click listener on the button in the getView function of the adapter:

likebutton.setOnClickListener(likeButtonClickListener);

Then define the onclick listener:

private OnClickListener likeButtonClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
final int position = listView.getPositionForView(parentRow);
}
};

There is also another way by setting the tag in the getView function:

likebutton.setTag(position);
likebutton.setOnClickListener(likeButtonClickListener);

In the button click listener, we will get the tag and find the position value.

private OnClickListener likeButtonClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
int position = (Integer) v.getTag();
}
};

Edit: to explain the solution of the link i mentioned in comments.
He declares a final boolean variable mHighlightedPositions which provides info if the highlighted position in the listview is selected or not.
If this variable returns true the line in the listview is selected, so what you can do is.

Declare a variable "NUM_OF_ITEMS" as integer and the "mHighlightedPositions" as a boolean in your adapter:

private int NUM_OF_ITEMS;
private final boolean[] mHighlightedPositions = new boolean[NUM_OF_ITEMS];

With this you can define if a item in the listview is selected or not and apply the background/color to your button like he did:

...
if(mHighlightedPositions[position]) {
holder.likebutton.setBackgroundResource(R.drawable.icon_yellow_star_large);
}else {
holder.likebutton.setBackgroundResource(0);
}
...

If you want to follow his example you also need to declare a ViewHolder.

holder = new ViewHolder();

The position of the items will be returned by the position variable:

int position = (Integer)view.getTag();

See below example from the link:

@Override
public void onClick(View view) {
int position = (Integer)view.getTag(); // gets current position
Log.d(TAG, "Button row pos click: " + position);
...
...
if(mHighlightedPositions[position]) { // current position is given
button.setBackgroundResource(0);
mHighlightedPositions[position] = false;
}else {
button.setBackgroundResource(R.drawable.icon_yellow_star_large);
mHighlightedPositions[position] = true;
}
}

getting item position of the button click inside a listview using cursorAdapter

To be able to use v.getTag() you need two things. In newView you need to do

calendarListView.setTag(rowView);

and in bindView you need to insert the correct data into the holder also:

rowView.position = cursor.getPosition(); // (same as in newView)

For all of this to work together, you also need to make sure that the view you are doing getTag() on is the view that you did setTag on.

In your case that would mean doing this in newView:

rowView.mTitle.setTag(rowView);
rowView.mButton.setTag(rowView);

It would probably be simpler to not use RowViewHolder at all. Instead do this:

In bindView:

int pos = cursor.getPosition();
title.setTag(pos);
button.setTag(pos);

And then in your OnClickListeners do

Integer pos = (Integer) v.getTag();

Good luck!

Get the position after click on button in a list view

Ok I found, I pass my position to the ViewHolder

and I get the ViewHolder from the tag of the view or parrent view by a recursive function.

Is there any different method?

public static class ViewHolder
{
private TextView text01;
public int position;
}
public ViewHolder getViewHolder(View v)
{
if(v.getTag() == null)
{
return getViewHolder((View)v.getParent());
}
return (ViewHolder)v.getTag();
}
public void onClick(View v) {

ViewHolder vh = getViewHolder(v);
vh.position // Here I get position
}

how to get a list item position by clicking the button inside it?

ListView with Clickable Buttons!!!

Well...., here's the rough method to solve my problem SO FAR....

item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView android:id="@+id/showTv"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:textSize="24dip"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<RelativeLayout android:id="@+id/jjjj"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<Button android:id="@+id/gointoBt"
android:focusable="false"
android:layout_alignParentRight="true"
android:text="abc"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<Button android:id="@+id/chooseBt"
android:layout_toLeftOf="@id/gointoBt"
android:layout_marginRight="10dip"
android:text="text"
android:focusable="false"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</RelativeLayout>

MySimpleAdapter:

import ........;

public class MySimpleAdapter extends SimpleAdapter {

private final Context context;
private List<Map<String, Object>> data;
private int resource;
private String[] from;
private int[] to;

public MySimpleAdapter(Context context,List<? extends Map<String, ?>> data, int resource, String[] from,
int[] to) {
super(context, data, resource, from, to);
this.context=context;
this.data=(List<Map<String, Object>>) data;
this.resource=resource;
this.from=from;
this.to=to;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = ((Activity)context).getLayoutInflater();
View rowView = inflater.inflate(resource, null, true);
Map<String, Object> medMap=data.get(position);
final TextView[] showTv=new TextView[from.length];

for (int i = 0; i < from.length; i++) {
showTv[i]=(TextView)rowView.findViewById(to[i]);
showTv[i].setText(""+medMap.get(from[i]));
}
Button btn=(Button)rowView.findViewById(R.id.gointoBt);
Button.OnClickListener mOkOnClickListener = new Button.OnClickListener()
{
public void onClick(View v) {
Log.v("ttttttt", ""+showTv[0].getText());
Toast.makeText(context,""+showTv[0].getText(), Toast.LENGTH_LONG).show();
}
};
btn.setOnClickListener(mOkOnClickListener);

Button btn2=(Button)rowView.findViewById(R.id.chooseBt);
Button.OnClickListener mOkOnClickListener2 = new Button.OnClickListener()
{
public void onClick(View v) {
Log.v("hhhhhhh", ""+showTv[0].getText());
Toast.makeText(context,"abc"+showTv[0].getText(), Toast.LENGTH_LONG).show();
}
};
btn2.setOnClickListener(mOkOnClickListener2);
return rowView;
}
}

Activty:

import .......;

public class ActivityMain extends Activity {

ListView listview;
List<Map<String,Object>> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("My work");
prepareData();
listview =new ListView(this);
MySimpleAdapter adapter=new MySimpleAdapter(this,data,R.layout.item,new String[] {"uu"},new int[]{R.id.showTv});

listview.setAdapter(adapter);
setContentView(listview);

}
private void prepareData(){
data=new ArrayList<Map<String,Object>>();
Map<String,Object> item;
item=new HashMap<String,Object>();
item.put("uu", "hello");
data.add(item);
item=new HashMap<String,Object>();
item.put("uu", "myyou");
data.add(item);
item=new HashMap<String,Object>();
item.put("uu", "piero");
data.add(item);
}

}

Thanks for that Man that is so kind to provide this AWESOME yet Mightiest tutorial....
which anyone here couldn't give to the noobs....

Get ListView item position by uri / by the data it contains

ListView does not support updating a single position.

You must update the adapter with the new data (even if you change a single position) and the, invoke mAdapter.notifyDataSetChanged()

If you get the View by its position (via listView.getChildAt()) will work. However, if you scroll up and down the list, that view will display the old data again because the adapter is not aware of the change (and it is the adapter which update the view contect view getView()).

When you invoke notifyDataSetChanged(), you are telling to the adapter that your data set has new info and the ListView/Adapter will re-draw the visible items (it won't re-draw whole list at once.. only the visible items).

You may want to consider to change to RecyclerView in the future. The BaseAdapter used in a RecyclerView support actions such as add/remove/update a single position.



Related Topics



Leave a reply



Submit