简体   繁体   中英

Alert Dialog Box Not Showing Android in list Activity

I am facing a problem. I have a Class that extends listactivity and its code is like this :

public class QuestionListActivity extends ListActivity
{
//Members

private ImageButton bntRefresh;
private ImageButton bntSettings;
private ImageButton bntGetSurveys;


 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.surveylist);

    initialize();

    currentSurveys=new SurveyList();
 }

private void initialize()
{
    bntRefresh= (ImageButton) findViewById(R.id.ibtnRefresh);
    bntRefresh.setImageResource(R.drawable.refresh);
    bntRefresh.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        Toast.makeText(SurveyListActivity.this, "This will refresh the survey list.", Toast.LENGTH_SHORT).show();   
        }
    });



    bntSettings= (ImageButton) findViewById(R.id.ibtnSettings);
    bntSettings.setImageResource(R.drawable.settings);
    bntSettings.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(SurveyListActivity.this, "This will open the settings.", Toast.LENGTH_SHORT).show(); 
        }
    });

    bntGetSurveys= (ImageButton) findViewById(R.id.ibtnGetSurveys);
    bntGetSurveys.setImageResource(R.drawable.getsurvey);
    bntGetSurveys.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) 
        {
            AlertDialog getSurveyAlert = new AlertDialog.Builder(QuestionListActivity .this).create();
            getSurveyAlert.setTitle("Enter QR Code");
            getSurveyAlert.setMessage("Choose a source");
            getSurveyAlert.setButton("Camera", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            getSurveyAlert.setButton2("text", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
        }
    });
}

    class SurveyAdapter extends ArrayAdapter<Survey>
    {
      //-- code here for adapter
    }
}

In my initialize function you can see I have three imagebuttons and I have implemented onClicklisteners for them. For the Settings and refresh button I have shown a Toast, and it is working fine. For the Get Button I have shown a dialog box which further asks the user to do something, but the problem I am facing is that dialog box is not appearing and I don't have any idea why ?

You forgot to, write line

getSurveyAlert.show();

try this,

bntGetSurveys.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) 
        {
            AlertDialog getSurveyAlert = new AlertDialog.Builder(QuestionListActivity .this).create();
            getSurveyAlert.setTitle("Enter QR Code");
            getSurveyAlert.setMessage("Choose a source");
            getSurveyAlert.setButton("Camera", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            getSurveyAlert.setButton2("text", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
        getSurveyAlert.create().show();
}

call show() method

@Override
        public void onClick(View v) 
        {
            AlertDialog getSurveyAlert = new AlertDialog.Builder(QuestionListActivity .this).create();
            getSurveyAlert.setTitle("Enter QR Code");
            getSurveyAlert.setMessage("Choose a source");
            getSurveyAlert.setButton("Camera", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            getSurveyAlert.setButton2("text", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
getSurveyAlert.create().show();
        }
    });

AlertDialog getSurveyAlert = new AlertDialog.Builder(QuestionListActivity.this).create(); // use this line instead of this

AlertDialog getSurveyAlert = new AlertDialog.Builder(QuestionListActivity .this).show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM