This is my listview click event:
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
ItemDetails obj_itemDetails = (ItemDetails)o;
Toast.makeText(ListViewImagesActivity.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
}
});
And this is my button click event:
btnNxt = (Button) findViewById(R.id.btnNext);
btnNxt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//Here I need to get that position
});
Now I need to get the position of the clicked listview item..I have declared postion globally but that always gives me position 1.
Can anyone say me how do I get the positon ?
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();
});
I know I'm bit late with the answer, but what about this:
private OnClickListener myButtonClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
final int position = listView.getPositionForView(parentRow);
}
};
in this case you don't need to set tags and so on.
If you are using ViewHolder make sure you use setTag() like this.
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mLayoutInflater.inflate(R.layout.custom_row, parent, false);
holder.btnNxt = (Button) convertView.findViewById(R.id.btnNext);
holder.btnNxt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int position=(Integer)arg0.getTag();
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.btnNxt.setTag(position); // <---- setTag() here
return convertView;
}
private class ViewHolder {
Button btnNxt;
}
}
If you setTag() before your setOnClickListener() line, you will have problem when you scroll your listview.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With