无法在android.Intresting问题中显示警告对话框

时间:2015-03-24 12:28:57

标签: android android-alertdialog

创建列表视图点击列表将显示带有问题和按钮的单选按钮。单击按钮警告对话框应该显示。现在问题我实现了那个并且它工作正常。当我回到列表视图和agin单击一个列表,然后单击一个按钮alertdialog没有显示在android ..Dont知道该做什么..我在这里发布了代码`

notesbtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub




                 dialog = new Dialog(getParent());
                //dialog.show();
                //dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

                //dialog.setCanceledOnTouchOutside(true);
                //dialog.setContentView(R.layout.activity_popupwindow1);
                //dialog.setContentView(view);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View view = inflater
                        .inflate(R.layout.activity_popupwindow1,
                                null,false);
                dialog.setCanceledOnTouchOutside(false);
                dialog.setContentView(view);
                dialog.getWindow().setBackgroundDrawable(
                        new ColorDrawable(0));
                //dialog.getCurrentFocus();

                //  qPosition =McqActivity.listPosition;
                    //System.out.println("qqhhhhh"+qPosition);


                DisplayMetrics metrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(
                        metrics);
                int height = metrics.heightPixels;
                int width = metrics.widthPixels;

                dialog.getWindow().setLayout((int) (width / 1.1),
                        (int) (height / 1.3));
                System.out.println("vvvvv"+height);
                System.out.println("vvvvv"+width);

                Button btnclose = (Button) dialog.findViewById(R.id.popupclosebtn);
                Button btnupdate = (Button) dialog.findViewById(R.id.popupupdatebtn);

                btnclose.setTypeface(lighttypeface);
                btnupdate.setTypeface(lighttypeface);

                ImageView imageshow = (ImageView) dialog.findViewById(R.id.popupblueview);
                imageshow.setImageBitmap(bitmap);

                System.out.println("imagebitmap"+bitmap);
                popupfirsttext = (TextView) dialog.findViewById(R.id.popupfirsttext);
                popupfirsttext.setText(McqActivity.question);
                popupnotes = (TextView) dialog.findViewById(R.id.popupnotes);
                popupnotes.setText(McqActivity.notes);
                System.out.println("vivek"+note);
                System.out.println("popupnotes : "+popupfirsttext.getText().toString());


                // Display the dialog




                btnupdate.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                        Intent i = new Intent(
                                McqlistonitemclickactionActivity.this,
                                PendingtestActivity.class);
                        startActivity(i);
                        finish();
                    }
                });
                btnclose.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        dialog.dismiss();

                    }
                });
                try {
                    System.out.println("faulllllll");

                     dialog.getContext();
                     dialog.show();

                } catch (Exception e2) {
                    // TODO: handle exception
                    e2.printStackTrace();
                }

这是我获取警报对话框的代码。但这只能运行一次。

@SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mcqlist);
    db = new MySQLiteHelper(McqActivity.this);
    db.openDataBase();
    ParseObject.registerSubclass(MCQ.class);
    // mProgressDialog= (ProgressBar)findViewById(R.id.progBarforlogin);
    // final MCQ m=new MCQ();
    myActivity = McqActivity.this;
    mcqlistview = (MyListView) findViewById(R.id.mcqlistview);

    mcq_cur_page = "secondpage";
    // populateList();

    // System.out.println("listmcq"+list.size());
    /*
     * for (MCQ mcq:list){
     * 
     * String log = "question: "+mcq.getQuestion();
     * 
     * System.out.println("Question: "+ mcq.getQuestion());
     * //Log.d("Question: ", log); }
     */

    // doSomeWork();
    getdata();
    System.out.println("listview");

    mcqlistview
            .setonRefreshListener(new com.example.pgmed.MyListView.OnRefreshListener() {

                @Override
                public void onRefresh() {
                    // TODO Auto-generated method stub
                    System.out.println("first stage");
                    new GetDataTask().execute();
                }
            });

    mcqlistview
            .setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> adapterView,
                        View view, int position, long id) {
                    // TODO Auto-generated method stub

                    Toast.makeText(McqActivity.this, "Selected" + position,
                            Toast.LENGTH_SHORT).show();
                    if (position <= list.size()) {
                        System.out.println("mcq position" + position);
                        MCQ item = (MCQ) adapter.getItem(position - 1);
                        System.out.println("item" + (position - 1));
                        Intent i = new Intent(McqActivity.this,
                                McqlistonitemclickactionActivity.class);
                        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        listPosition = position - 1;
                        id_value = item.getObjectId();
                        System.out.println("idval" + id_value);
                        question = item.getQuestion();
                        System.out.println("question" + question);
                        option1 = item.getOption1();
                        System.out.println("option1" + option1);
                        option2 = item.getOption2();
                        System.out.println("option2" + option2);
                        option3 = item.getOption3();
                        System.out.println("option3" + option3);
                        option4 = item.getOption4();
                        System.out.println("option4" + option4);
                        notes = item.getNotes();
                        System.out.println("notes" + notes);
                        image = item.getImage();
                        System.out.println("image" + image);
                        answer = item.getAnswer();
                        System.out.println("answer" + answer);
                        answered = item.getAnswered();
                        // i.putExtra("id",item.getId());
                        replaceContentView("activity_mcq", i);

                        // setContentView(R.layout.activity_mcq);

                    }
                }
            });

}

protected void doSomeWork() {
    // TODO Auto-generated method stub
    new Thread(new Runnable() {
        public void run() {
            while (mProgressStatus < 100) {
                mProgressStatus += 1;
                // Update the progress bar
                mHandler.post(new Runnable() {
                    public void run() {
                        mProgressDialog.setProgress(mProgressStatus);

                    }
                });
                try {
                    // Display progress slowly
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}

public void replaceContentView(String id, Intent newintent) {
    // TODO Auto-generated method stub
    View view = getLocalActivityManager().startActivity(id,
            newintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
            .getDecorView();
    McqActivity.this.setContentView(view);
}

@Override
public void onBackPressed() {

    if (mcq_cur_page.equalsIgnoreCase("firstpage")) {

        Intent i = new Intent(McqActivity.this, McqActivity.class);
        // calls the method to replace View.

        // startActivity(i1);

        replaceContentView("Called", i);

    } else {
        /*
         * AlertDialog.Builder alertDialog = new
         * AlertDialog.Builder(((Activity)
         * getApplicationContext()).getParent()); // Setting Dialog Title
         * alertDialog.setTitle("PGMED"); // Setting Dialog Message
         * alertDialog.setMessage("Do u want exit from App"); // Setting
         * Icon to Dialog // alertDialog.setIcon(R.drawable.save); //
         * Setting Positive "Yes" Button
         * alertDialog.setPositiveButton("Yes", new
         * DialogInterface.OnClickListener() { public void
         * onClick(DialogInterface dialog, int which) { System.exit(0); //
         * User pressed YES button. Write Logic Here } });
         * 
         * alertDialog.setNegativeButton("No", new
         * DialogInterface.OnClickListener() { public void
         * onClick(DialogInterface dialog, int which) {
         * 
         * // User pressed YES button. Write Logic Here } }); // Showing
         * Alert Message alertDialog.show();
         */
        // if( !exiting )
        new ExitDialog(this).show();
        // else
        // super.onBackPressed();

        // finish();
    }
}

class ExitDialog extends AlertDialog {
    @SuppressWarnings("deprecation")
    protected ExitDialog(Context context) {
        super(context);

        setTitle("Exit?");
        setMessage("Are you sure you want to exit?");
        setButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                exiting = true;
                System.out.println("clicked");
                // System.exit(0);

                Intent homeIntent = new Intent(Intent.ACTION_MAIN);
                homeIntent.addCategory(Intent.CATEGORY_HOME);
                homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(homeIntent);
            }

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

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                exiting = false;
            }
        });
        // setButton(BUTTON_POSITIVE, "Yes", (OnClickListener) this);
        // setButton( BUTTON_NEGATIVE, "No", this );
    }

}

private class GetDataTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        System.out.println("mcqactivity");
        // Simulates a background job.
        ParseQuery<MCQ> query3 = new ParseQuery<MCQ>("mcq");

        SimpleDateFormat format = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        try {
            updatedat = db.getMaxDatemcq();
            createdat = db.getMaxDatemcq1();
            Date up_date = format.parse(updatedat);
            Date create_date = format.parse(createdat);
            System.out.println("update" + up_date);
            query3.whereGreaterThan("updatedAt", up_date);
            query3.whereGreaterThan("createdAt", create_date);
            System.out.println("data s from server" + up_date);
            System.out.println("data s from" + create_date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {

            list1 = new ArrayList<MCQ>();
            list1 = query3.find();
            System.out.println("data s from server list" + list1.size());

        } catch (com.parse.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;

    }

    @Override
    protected void onPreExecute() {
        // SplashScreen.this.progressDialog =
        // ProgressDialog.show(SplashScreen.this, "",
        // "Loading...", true);

        // pd.show();
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Void... values) {

        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(Void result) {

        // Call onRefreshComplete when the list has been refreshed.
        System.out.println("facts list" + list1.size());
        if (list1.size() > 0) {
            for (MCQ mcq : list1) {

                // titles.add((String) todo.get("question"));
                question = (String) mcq.get("question");
                System.out.println("headline" + question);
                option1 = (String) mcq.get("option1");
                option2 = (String) mcq.get("option2");
                option3 = (String) mcq.get("option3");
                option4 = (String) mcq.get("option4");
                answer = (String) mcq.get("answer");
                notes = (String) mcq.get("notes");
                imgnotes = (String) mcq.get("imagenotes");
                answered = (String) mcq.get("answered");
                // createdat=(String)mcq.get("createdAt");
                // updatedat=(String)facts.get("updatedAt");
                // facts.getUpdatedAt();
                // System.out.println("updatedat"+updatedat);
                // facts.getCreatedAt();

                // SimpleDateFormat sdf = new
                // SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
                // String strDate = sdf.format(updatedate);
                id_value = (String) mcq.getObjectId();

                db.add_update_mcq(mcq);

            }

            /*
             * list1 = new ArrayList<MCQ>();
             * //db.updateMCQanswered(id_value); list1 = db.getAllMCQ();
             */
            System.out.println("list" + list1.size());
            // setdata();
            getdata();

        }

        mcqlistview.onRefreshComplete();

    }
}

/*
 * public void setdata() { // TODO Auto-generated method stub adapter = new
 * McqListviewadapterActivity( McqActivity.this, list1);
 * 
 * mcqlistview.setAdapter(adapter); adapter.notifyDataSetChanged(); }
 */
public void getdata() {
    // TODO Auto-generated method stub

    list = new ArrayList<MCQ>();
    list = db.getAllMCQ();

    adapter = new McqListviewadapterActivity(McqActivity.this, list);
    mcqlistview.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}

0 个答案:

没有答案