Devil Raily Devil Raily - 20 days ago 4
Android Question

How do I know which button was clicked in ListView

I have created a custom ListView adapter. In the

getView
, I have another override method
onClick
. My problem is how do I know which button in the ListView got clicked?

item_category

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/specialCatItemName"
android:textColor="#000"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:text="View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnSpecialView"
android:textAllCaps="false"
android:padding="3dp"
android:background="#CCBA56"
android:textColor="#fff"
android:minHeight="40dp"
android:layout_alignBaseline="@+id/btnSpecialImages"
android:layout_alignBottom="@+id/btnSpecialImages"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="15dp"/>

<Button
android:text="Images"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnSpecialImages"
android:textAllCaps="false"
android:paddingTop="2dp"
android:paddingRight="2dp"
android:paddingBottom="2dp"
android:paddingLeft="2dp"
android:background="#011E4D"
android:textColor="#fff"
android:minHeight="40dp"
android:layout_marginTop="11dp"
android:layout_below="@+id/specialCatItemName"
android:layout_toRightOf="@+id/btnSpecialView"
android:layout_toEndOf="@+id/btnSpecialView"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginBottom="15dp"/>

</RelativeLayout>


Cursor Adapter

class categoryCursorAdaptor extends BaseAdapter {

Context context;
String[] data;
private static LayoutInflater inflater = null;
int gPosition;
String row;

public categoryCursorAdaptor(Context context, String[] data) {
this.context = context;
this.data = data;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return data.length;
}

@Override
public Object getItem(int position) {
return data[position];
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
this.gPosition = position;
this.row = data[position];
if (vi == null) vi = inflater.inflate(R.layout.item_category, null);
TextView text = (TextView) vi.findViewById(R.id.specialCatItemName);
text.setText(data[position]);

Button Button1= (Button) vi.findViewById(R.id.btnSpecialView);
Button1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
//How do I know which button is got clicked?
}
});
return vi;
}
}

Answer

@Devil Raily You can use setTag and getTag method to get the position. Use code like this

Button Button1= (Button) vi.findViewById(R.id.btnSpecialView);
Button1.setTag(position);
Button1.setOnClickListener(new View.OnClickListener(){
            @Override 
            public void onClick(View view) {
               //How do I know which button is got clicked? 
                int position = (Integer)view.getTag();
            } 
        });
Comments