Kristo Kristo - 3 months ago 16
Android Question

Android Listview with Button get Item position

I have a listview with a button in it. is there any possible way to check which button was clicked and in which position of the listview ?

enter image description here

here is my code.

final RelativeLayout layoutFooter2 = (RelativeLayout) getLayoutInflater().inflate(R.layout.foodlist, null);
final Button btnBack = (Button) layoutFooter2.findViewById(R.id.Back);
// buttonadd
final Button btnadd = (Button) layoutFooter2.findViewById(R.id.btnaddd);
// buttondelete
final ListView listView = getListView();
final TextView txvSum = (TextView) layoutFooter.findViewById(R.id.Sum);
txvSum.setText("");
listView.invalidateViews();
listView.setTextFilterEnabled(true);
listView.addFooterView(layoutFooter);
listView.setAdapter(getListAdapter());
// changes have been done here in order to fit the buttons in textview55+ changes to large/foodlist.
setListAdapter(new ArrayAdapter<Object>(this, R.layout.foodlist, R.id.textview55, ListItemSalads));
getWindow().getDecorView().setBackgroundColor(Color.BLACK);

btnadd.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v)
{

Toast.makeText(ActivityFoodsSalads.this, "Button Clicked", Toast.LENGTH_SHORT).show();

}
});


xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/kati"
>

<TextView
android:id="@+id/textview55"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/listviewborder"
android:textColor="#FFFFFF"
android:padding="12dp"
android:textSize="24sp" >
</TextView>


<Button
android:id="@+id/btnaddd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="112dp"
android:alpha="1"
android:background="#3b3974"
android:focusable="false"
android:focusableInTouchMode="false"
android:minWidth="90dp"
android:text="+"
android:textColor="#f1ecef" />




Any advice is much appriciated.Cheers.

Answer

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