Wątek przeniesiony 2017-12-10 16:06 z Java przez Shalom.

ImageView w layoucie preferencji nie zmienia się

0

Pozwolę sobie zacytować siebie za stackoverflow ;)

"Hello I created PreferenceActivity class that loads all preferences from R.xml.preferences and shows them on the list R.layout.preference_list (the simplest layout with the list in it only).
The thing is one of the preference uses specific layout like

    <EditTextPreference
        android:title="@string/auth_title"
        android:summary="@string/auth_summary"
        android:key="pass"
        android:defaultValue="1"
        android:layout="@layout/preference_auth"
        android:hint="Enter password"/>

where the key thing here in this layout is

<ImageView
    android:id="@+id/iconSelected"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="5dp"
    android:paddingLeft="10dp"
    android:layout_alignParentRight="true"
    android:src="@drawable/ic_action_no" />

and it describes the icon on the right side. Left side is clickable preference which opens EditText area.

SCREEN: https://s33.postimg.org/ubkp100nz/bbb.png

Now I want to programatically change the source of image that is shown on screen when the app starts (actually when EditText matches to specific string but let's keep simple here as much as it is possible).
EditTextPreference editPref;
ImageView icon;

        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);

            addPreferencesFromResource(R.xml.preferences);
            setContentView(R.layout.preference_list);

            editPref = (EditTextPreference) getPreferenceScreen().findPreference("pass");

            icon = (ImageView)getLayoutInflater().inflate(editPref.getLayoutResource(), null).findViewById(R.id.iconSelected);
            icon.setImageResource(R.drawable.ic_action_yes);

        }

And this code doesn't work. The image seems to be intact. Where is the problem?"

0

Zapnij się debuggerem i zobacz czy przypadkiem ustawienia nie bindują Ci się za późno względem onCreate()

0

Dodalem invalidate() zarówno na onCreate jak i onResume i nadal zmiana nie działa.

0

robisz inflacje obiektu

        icon = (ImageView)getLayoutInflater().inflate(editPref.getLayoutResource(), null).findViewById(R.id.iconSelected);
        icon.setImageResource(R.drawable.ic_action_yes);

i nigdzie tego nie doczepiasz do rootView, wiec podmieniasz ikone w viewGroup ktory nie jest podpiety do aktywnosci

setContentView tym sie zajmuje,
wystarczy ze zmienisz:

        icon = (ImageView)getLayoutInflater().inflate(editPref.getLayoutResource(), null).findViewById(R.id.iconSelected);

na:

(ImageView) findViewById(R.id.iconSelected);
icon.setImageResource(R.drawable.ic_action_yes);

a jezeli chcesz robic dynamicznie inflacje to musisz zrobic pozniej addView na rootView albo replaceView

1 użytkowników online, w tym zalogowanych: 0, gości: 1