I am working on an app that will display a random joke when the user clicks on the "joke" button on the top of the page. When the user clicks this button, a dialog will appear that will display the title of the joke, the setup, and a punchline button to display another dialog, that will display the punchline of the joke. When I create my dialogs, I am trying to figure out how to set the title and message to the values of the arrays that are declared in the MainActivity.java file. So far, I was able to figure out the code to switch back and forth between the 2 dialogs and exit them at any time. However, I am stuck on how to retrieve the array values in the dialog java files from the MainActivity java file. I have tried to add code to the buttonShowDialog() and onOKClick methods in the MainActivity file, but this doesn't make the dialog files recognize the array values. I am thinking this is an OOP problem, but I am stuck. Any suggestions would be appreciated.
Main Activity.java
package edu.psu.jjb24.csjokes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.MenuInflater;
import android.view.MenuItem;
import java.util.Random;
public class MainActivity<currentJoke> extends AppCompatActivity implements DisplaySetupDialog.OKClickListener,
DisplayPunchlineDialog.OKClickListener{
String[] joke_title;
String[] joke_setup;
String[] joke_punchline;
int currentJoke = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set the action bar
Toolbar myToolbar = findViewById(R.id.toolbar);
setSupportActionBar(myToolbar);
joke_title = getResources().getStringArray(R.array.JokeTitle);
joke_setup = getResources().getStringArray(R.array.JokeSetup);
joke_punchline = getResources().getStringArray(R.array.JokePunchline);
}
public int getCurrentJoke(String[] array){
int random = new Random().nextInt(array.length);
currentJoke = random;
return currentJoke;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_joke:
buttonShowDialog();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void buttonShowDialog() {
DisplaySetupDialog setupDialog = new DisplaySetupDialog();
setupDialog.show(getSupportFragmentManager(), "setupDialog");
}
public void onOKClick() {
DisplayPunchlineDialog punchlineDialog = new DisplayPunchlineDialog();
punchlineDialog.show(getSupportFragmentManager(), "punchlineDialog");
}
public void onOKClick2() {
buttonShowDialog();
}
}
DisplaySetupDialog.java
package edu.psu.jjb24.csjokes;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.DialogFragment;
public class DisplaySetupDialog extends DialogFragment{
public interface OKClickListener {
public void onOKClick();
}
private OKClickListener mListener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder setup = new AlertDialog.Builder(getActivity());
setup.setTitle("joke_title").setMessage("joke_setup")
.setPositiveButton("Punchline", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
mListener.onOKClick();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return setup.create();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener = (OKClickListener) context;
}
catch (ClassCastException e) {
throw new ClassCastException(context.toString() + "must implement OKClickListener");
}
}
}
DiplayPunchlineDialog.java
import android.os.Bundle;
import androidx.fragment.app.DialogFragment;
public class DisplayPunchlineDialog extends DialogFragment {
public interface OKClickListener {
public void onOKClick2();
}
private DisplayPunchlineDialog.OKClickListener mListener2;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder setup = new AlertDialog.Builder(getActivity());
setup.setTitle("joke_title").setMessage("joke punchline")
.setPositiveButton("Setup", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
mListener2.onOKClick2();
}
})
.setNegativeButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return setup.create();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener2 = (DisplayPunchlineDialog.OKClickListener) context;
}
catch (ClassCastException e) {
throw new ClassCastException(context.toString() + "must implement OKClickListener");
}
}
}