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 implement the OnClickListener in the adapter. However, I dont know how to call the function like startActivity() or setResult(). since the adapter doesnt extend to Activity.
so what is the best way to solve this problem ? thanks
Just pass in the current Context to the Adapter constructor and store it as a field. Then inside the onClick you can use that context to call startActivity().
pseudo-code
public class MyAdapter extends Adapter {
private Context context;
public MyAdapter(Context context) {
this.context = context;
}
public View getView(...){
View v;
v.setOnClickListener(new OnClickListener() {
void onClick() {
context.startActivity(...);
}
});
}
}
When implementing the onClickListener, you can use v.getContext.startActivity.
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.getContext().startActivity(PUT_YOUR_INTENT_HERE);
}
});
public class MyAdapter extends Adapter {
private Context context;
public MyAdapter(Context context) {
this.context = context;
}
public View getView(...){
View v;
v.setOnClickListener(new OnClickListener() {
void onClick() {
Intent intent= new Intent(context, goto.class);
intent.putextra("your_extra","your_class_value");
context.startActivity(intent);
}
});
}
}
For newer versions of sdk you have to set flag activity task.
public void onClick(View v)
{
Intent myactivity = new Intent(context.getApplicationContext(), OtherActivity.class);
myactivity.addFlags(FLAG_ACTIVITY_NEW_TASK);
context.getApplicationContext().startActivity(myactivity);
}
callback from adapter to activity can be done using registering listener in form of interface: Make an interface:
public MyInterface{
public void yourmethod(//incase needs parameters );
}
In Adapter Let's Say MyAdapter:
public MyAdapter extends BaseAdapter{
private MyInterface listener;
MyAdapter(Context context){
try {
this. listener = (( MyInterface ) context);
} catch (ClassCastException e) {
throw new ClassCastException("Activity must implement MyInterface");
}
//do this where u need to fire listener l
try {
listener . yourmethod ();
} catch (ClassCastException exception) {
// do something
}
In Activity Implement your method:
MyActivity extends AppCompatActivity implements MyInterface{
yourmethod(){
//do whatever you want
}
}
If you want to redirect on url instead of activity from your adapter class then pass context of with startactivity.
btnInstall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(link));
intent.setData(Uri.parse(link));
context.startActivity(intent);
}
});
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);
来源:https://stackoverflow.com/questions/4197135/how-to-start-activity-in-adapter