I have a ListActivity with my customized adapter and inside each of the view, it may have some buttons, in which I need to implement OnClickListener
. I need to
First Solution:
You can call start activity inside your adapter like this:
public class YourAdapter extends Adapter {
private Context context;
public YourAdapter(Context context) {
this.context = context;
}
public View getView(...){
View v;
v.setOnClickListener(new OnClickListener() {
void onClick() {
context.startActivity(...);
}
});
}
}
Second Solution:
You can call onClickListener
of your button out of the YourAdapter
class. Follow these steps:
Craete an interface like this:
public YourInterface{
public void yourMethod(args...);
}
Then inside your adapter:
public YourAdapter extends BaseAdapter{
private YourInterface listener;
public YourAdapter (Context context, YourInterface listener){
this.listener = listener;
this.context = context;
}
public View getView(...){
View v;
v.setOnClickListener(new OnClickListener() {
void onClick() {
listener.yourMethod(args);
}
});
}
And where you initiate yourAdapter will be like this:
YourAdapter adapter = new YourAdapter(getContext(), (args) -> {
startActivity(...);
});
This link can be useful for you.
Simple way to start activity in Adopter's button onClickListener:
Intent myIntent = new Intent(view.getContext(),Event_Member_list.class); myIntent.putExtra("intVariableName", eventsList.get(position).getEvent_id());
view.getContext().startActivity(myIntent);