1

So I am trying to add a Button some functionality with an onClickListener but I can't seem to get it to work.
My button is declared in the XML of the list item and my list is implemented within a Fragment.
I have done some research to find some answers and every solution I tried it either failed or it was too complicated. I am kind of starting so sorry for my ignorance.

Therefore my question is how should I get an onClickListener for my Button in these conditions. Is there something that makes the effect of the Listener not work in a list?
2
  • 1
    Please post your code. Commented Mar 1, 2015 at 19:27
  • Post your activity and the list item code. Commented Mar 1, 2015 at 19:29

2 Answers 2

2

in your adpater class find the button by id then in same class write button.setOnClickListner. it would work

If you want the click even in your frament class the you have to write the interface in adapter class and extend your fragment by that interface then use the interface method for click

if you need click in fragment class with index/position then use the belo sample code

//Inside adpater class
public interface OnEditLocationListener {
        public void onDeleteClick(int position);

    }

    private OnEditLocationListener listener;

    public void setOnEditLocationListener(OnEditLocationListener listener) {
        this.listener = listener;
    }

    //Inside getView method
    holder.imv_delete.setTag(position);
        holder.imv_delete.setOnClickListener(null);
        holder.imv_delete.setOnClickListener(new OnClickListener() {

            @Override
            public void onDeleteClick(View view) {
                if (listener != null) {
                    listener.onDeleteClick(position);
                }
            }
        });



//In Activity
        yourclass extends yourchoice implements adapter.OnEditLocationListener

        //inside onCreate/onCreateview

        adapter.setOnClickListener(getActivity/this);

        //Button click
    @Override
    public void onDeleteClick(int pos) {
        Toast.makeText(getActivity(), "clicked @ " + pos, Toast.LENGTH_SHORT).show();


    }
Sign up to request clarification or add additional context in comments.

1 Comment

@abforce You were right so I updated my code. please check it. Now you can use current row index or anything else b passing it in onDeleteClick method in adapter
0

Hello ) First you must create your custom adapter. For example :

public class EatListAdapter extends BaseAdapter {

    private ArrayList<EatTime> eatTimes;
    private Activity activity;

    public EatListAdapter(Activity activity, ArrayList<EatTime> items) {
        this.eatTimes = items;
        this.activity = activity;
    }

    @Override
    public int getCount() {
        return eatTimes.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

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

        View v;
        LayoutInflater inflater = (LayoutInflater) activity.getApplicationContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        v = inflater.inflate(R.layout.list_item, null);

        Button delete = (Button) v.findViewById(R.id.button_delete);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            //your ON CLICK CODE

        });

        return v;
    }
}

Than set adapter

ListView eatingList = (ListView) findViewById(R.id.eatingList);
adapter = new EatListAdapter(this, eatArrayList);
eatingList.setAdapter(adapter);

1 Comment

This worked pretty well and I finally understood how I should use lists. I was able to implement this pretty easily. Thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.