Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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>) 

How can I get this correctly?

like image 729
Gayan Kalanamith Avatar asked Mar 05 '13 16:03

Gayan Kalanamith


People also ask

How pass data from activity to class in Java Android?

String value = A. _utfValue ; Option 3: You can use SharedPreference to save the value and get it from other class. Option 4: You can use a static method with some return value and fetch the method in your java class through class name.

How can we pass data from one activity to another using Android intent?

Using Intents This example demonstrate about How to send data from one activity to another in Android using intent. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


1 Answers

call like this:

ButtonClickBySani(R.id.btnsehrabandi, sehrabandiActivity.class); 

Definition:

private void ButtonClickBySani(int ButtonId, final Class<? extends Activity> ActivityToOpen) {     Button btn;     // Locate the button in activity_main.xml     btn = (Button) findViewById(ButtonId);      // Capture button clicks     btn.setOnClickListener(new OnClickListener() {         public void onClick(View arg0) {             startActivity(new Intent(getBaseContext(), ActivityToOpen));             // Start NewActivity.class             //Intent myIntent = new Intent(getBaseContext(), ActivityToOpen);            // startActivity(myIntent);         }     }); } 
like image 139
Delickate Avatar answered Sep 19 '22 17:09

Delickate