Android: alertDialog не работает - PullRequest
       4

Android: alertDialog не работает

1 голос
/ 08 августа 2011

Я пытаюсь добавить диалоговое окно с предупреждением по нажатию кнопки, но кое-что, как это не работает ... но тост, который я добавил, работает отлично.Может кто-нибудь, пожалуйста, помогите мне.Я добавил контекст вместо «this» при создании объекта [new AlertDialog.Builder (context) .create ();] как когда я добавлял это, он выдавал мне ошибку "Конструктор AlertDialog.Builder (new View.OnClickListener () {}) не определен"

       public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        Service service = (Service) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child_layout, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
        Drawable d = convertView.findViewById(R.id.submit).getBackground();  
        PorterDuffColorFilter filter = new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);  
        d.setColorFilter(filter); 
        tv.setText("   " + service.getName());
        this.submitButton = (Button)convertView.findViewById(R.id.submit);
        this.submitButton.setText("Activate");
        this.submitButton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                Toast.makeText(context, "Service Activation Request Send", Toast.LENGTH_LONG).show();
                AlertDialog alertDialog = new AlertDialog.Builder(context).create();
                alertDialog.setTitle("Alert 1");
                alertDialog.setMessage("This is an alert");
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int which) {
                    return;
                } }); 
                }
              });

        //this.submitButton.setPadding(20, 0, 0, 0);

        // Depending upon the child type, set the imageTextView01
        tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        if (service instanceof DataService) {
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.data, 0, 0, 0);
        } else if (service instanceof VoiceService) {
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.voice, 0, 0, 0);
        } else if (service instanceof SmsService) {
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.sms, 0, 0, 0);
        }
        return convertView;
        }

Ответы [ 2 ]

3 голосов
/ 08 августа 2011

Вызовите метод show() для alertDialog.

0 голосов
/ 08 августа 2011

Вы вызываете create () перед настройкой любого из полей, а также не вызываете show ()

В документации Android говорится, что лучше определить создание диалогов в методе обратного вызова onCreateDialog (int) вашей активности

http://developer.android.com/guide/topics/ui/dialogs.html

Вы можете сделать это, а затем показать свой диалог в кнопке onClickListener, вызвав showDialog (int) с соответствующим Id

...