Kalanamith Kalanamith - 5 months ago 10
Android Question

Android how to pass an Activity.class as an argument for a function

I recently moved to Android from python and am stuck here.

This is my class declaration to create a common function for an Alert Dialog which accepts necessary parameters

public static AlertDialog.Builder getAlertDialog(String strArray[],
String strTitle, Activity v) {

return new AlertDialog.Builder(v)
.setTitle(strTitle).setItems(strArray,
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
}


But I cannot call this function via this piece of code which gives me an error

getAlertDialog(strArray, strTitle, MakeCall.class).show();


The error is

the method getAlertDialog(String[], String, Activity) in the type MakeCallAlertDialog is not applicable for the arguments (String[], String, Class<TestActivity>)


Can anyone tell me how to get this correctly? Thank you in advance.

Sam Sam
Answer

If you just want to pass a reference to your Activity use: MakeCall.this (or maybe just this.)

Comments