I’m trying to code a application for andriod (using eclipse) in which when a imagebutton is presses a alertdialouge comes up with a random string from and array, each time the button is pressed i would like it to change the string from the array. I have coded a alertdialouge and some code which gets a random string but it does it to a text-view instead of a alert dialouge. Can you please have a look at my code and tell me what i need to change?
package kevin.erica.box;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Random;
public class TheKevinAndEricaBoxActivity extends Activity {
/** Called when the activity is first created. */
private String[] myString;
private String list;
private static final Random rgenerator = new Random();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
myString = res.getStringArray(R.array.myArray);
list = myString[rgenerator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(list);
}
public void kevin(View view)
{
new AlertDialog.Builder(this).setTitle("The Box").setMessage(getResources().getText(R.string.list)).setNeutralButton("Close", null).show(); }
}
As I understand, you need to display a randomly selected text string from an array whenever a particular ImageButton is pressed.
Try the following code: