Изменить текст TextView в PopupWindow не работает - PullRequest
2 голосов
/ 04 сентября 2011

У меня проблемы с динамическим изменением текста в TextView в PopupWindow в Android.Я ссылался на элемент TextView внутри PopupWindow, используя LayoutInflater и ViewGroup, но текст не обновляется.Любая помощь будет очень высоко ценится!:)

Мой код выглядит следующим образом:

private void popupWindow() {
        try {
            LayoutInflater inflater = (LayoutInflater) SwingSpeed.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.popup_recording,
                    (ViewGroup) findViewById(R.id.popup_element));
            pw = new PopupWindow(inflater.inflate(R.layout.popup_recording,
                    null, false), 480, 800, true);
            pw.showAtLocation(findViewById(R.id.swingspeed), Gravity.CENTER, 0,
                    0);
            recording_text = (TextView) layout
                    .findViewById(R.id.recording_text);
            recording_text.setText("Recording"); //Update the TextView  
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

1 Ответ

5 голосов
/ 04 сентября 2011

Проблема в том, что вы раздуваете макет два раза, вместо:

recording_text = (TextView) layout.findViewById(R.id.recording_text);

сделать это:

recording_text = (TextView) pw.getContentView().
                 findViewById(R.id.recording_text);

а также вам вообще не нужна эта строка:

View layout = inflater.inflate(R.layout.popup_recording,
                (ViewGroup) findViewById(R.id.popup_element));
...