Perhaps the title of question isnt well created, but i couldnt understand what else I could put in there.
I wanted to understand, why the first one out of the two below cases works, and how can I get the second one to work as well, given the overall implementation is same.
From a code point of view, what principal etc is causing the first to work as is, i.e. broadening the scope of variables that can be used, so I can learn more about that.
I want to access the 'obj' variable in side the onclick listener.
Case 1
public void onBindViewHolder(MyAdapater.ViewHolder viewHolder, final int position) {
MyObject obj= memberVariable.get(position);
viewHolder.getButton().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// use obj because I can access it
}
});
}
Case 2
public void onBindViewHolder(MyAdapater.ViewHolder viewHolder, final int position) {
MyObject obj= memberVariable.get(position);
viewHolder.getButton().setOnClickListener(this::approveItem);
}
private void approveItem(View view) {
// can't access the obj object now.
}
My onclicklistener will only take an interface with an onClick method, that takes a single parameter. So can't pass obj as part of that.
{}blocks.