Android Alert Dialog Extract User Choice Radio Button?
- by kel196
Apologies for any coding ignorance, I am a beginner and am still learning!
I am creating a quiz app which reads questions off a SQL database and plots them as points on a google map. As the user clicks each point, an alert dialog appears with radio buttons to answer some question. My radiobuttons are in CustomItemizedOverlay file and when I click submit, I want to be able to send the user choice back to the database, save it and return to the user if their answer is correct.
My question is this, how do I pass the user choice out once the submit button has been clicked? I tried making a global variable to read what was selected to no avail. Any suggestions would be appreciated! If I need to post any other code, please ask and I will do so ASAP!
package uk.ac.ucl.cege.cegeg077.uceskkw;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.widget.Toast;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class CustomItemizedOverlay2 extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
public CustomItemizedOverlay2(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public CustomItemizedOverlay2(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
@Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
@Override
public int size() {
return mapOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mapOverlays.get(index);
// gets the snippet from ParseKMLAndDisplayOnMap and splits it back into
// a string.
final CharSequence allanswers[] = item.getSnippet().split("::");
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setIcon(R.drawable.easterbunnyegg);
dialog.setTitle(item.getTitle());
dialog.setSingleChoiceItems(allanswers, -1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(context,
"You have selected " + allanswers[whichButton],
Toast.LENGTH_SHORT).show();
}
});
dialog.setPositiveButton(R.string.button_submit,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
// int selectedPosition = ((AlertDialog)
// dialog).getListView().getCheckedItemPosition();
Toast.makeText(context, "hi!", Toast.LENGTH_SHORT)
.show();
}
});
dialog.setNegativeButton(R.string.button_close,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
// on cancel button action
}
});
AlertDialog question = dialog.create();
question.show();
return true;
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
this.populate();
}
}