Jason Jason - 17 days ago 7
Java Question

Set onClickListener via MainActivity on an item in a ListAdapter

How can I create a custom setOnClickListener for my class?

I currently have a listAdapter with items (Made a Item Class). Whenever I click on a item in that list, I want to activate the the functionality that I described in my MainActivity. I would like to set onClickListeners (custom onClickListeners) on classes that I have in my list adapter. How can I do so?

If you did not understand me, or would like to help, I zipped the project and put it on my Google Drive

I've looked through dozens of answer, but I just couldn't find the one that was working.. Please help, as I've invested way to much time in this.

Item item = new Item().setOnClickListener(new ItemListAdapter.ClickListener() {
@Override
public void onClick() {
Toast.makeText(MainActivity.this, "Clicked me", Toast.LENGTH_SHORT).show();
}
});


EDIT: Because I would like to later turn it into a library, it would be useful for me and the user that you can set a onclick on a item rather than the listview, which I want to hide from the user eventually

Answer

From my understanding you are looking for some thing like this , Change the class like this ,

public class Item {

    private int id;
    private ItemListAdapter.ClickListener mClickListener;

    public Item(int id) {
        this.id = id;
    }


    public Item(int id, @NonNull ItemListAdapter.ClickListener clickListener) {
        this.id = id;
        this.mClickListener = clickListener;
    }


    public Item setOnClickListener(@NonNull ItemListAdapter.ClickListener clickListener) {
        this.mClickListener = clickListener;
        return this;
    }


    public ItemListAdapter.ClickListener getClickListener() {
        return this.mClickListener;
    }

}

Adapter class

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;

public class ItemListAdapter extends ArrayAdapter<Item> {


    public ItemListAdapter(Context context, int resource) {
        super(context, resource, SettingsStorage.getInstance().getItems());
    }

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


        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item, parent, false);
        }

        final Item item = getItem(position);
        LinearLayout linearLayout = (LinearLayout) convertView.findViewById(R.id.item);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.getClickListener() != null) {
                    item.getClickListener().onClick();
                }
            }
        });

        return convertView;
    }

    public interface ClickListener {
        void onClick();
    }
}

and you can set the listener like this ,

Item item1 = new Item(1);
        item1.setOnClickListener(new ItemListAdapter.ClickListener() {
            @Override
            public void onClick() {
                Toast.makeText(MainActivity.this, "It works", Toast.LENGTH_SHORT).show();
            }
        });
        Item item2 = new Item(2, new ItemListAdapter.ClickListener() {
            @Override
            public void onClick() {

            }
        });