am getting an error in my GridView, am displaying a grid view from strings. I want to show an alert dialog when i select any row of the grid view. But My problem is when am scrolling the grid view it is showing the Alert Dialog. Here is my code.
grid = (GridView) findViewById(R.id.grid);
grid.setAdapter(/*Adapter class*/);
grid.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
final AlertDialog alertDialog = new AlertDialog.Builder(ApprovePunches.this).create();
alertDialog.setTitle("Approve...");
alertDialog.setMessage("Are you sure?");
alertDialog.setButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
}
});
alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
}
});
alertDialog.setIcon(android.R.drawable.alert_dark_frame);
alertDialog.show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Can anyone please help me in doing this. Thank you.
This is because you are trying to show a Alert Dialog in onItemSelected Listener. Your onItemSelectedListener gets called when ever there is a change in your View.(Eg. During Scroll event ).
If you want to display a Dialog based on user action, you can use setOnItemClickListener like this,
grid.setOnItemClickListener(new OnItemClickListener() {