AutoCompleteTextView не работает после изменения локали - PullRequest
0 голосов
/ 29 июня 2018

Здесь «AutoCompleteTextView» загружает список стран из «Locale» со следующим кодом.

   public static ArrayList getCountryList()
        {
            Locale[] locale = Locale.getAvailableLocales();
            ArrayList<String>countries = new ArrayList<String>();
            String country = "";
            for( Locale loc : locale ){
                country = loc.getDisplayCountry();
                if( country.length() > 0 && !countries.contains(country) ){

                    countries.add( country );
                }
            }

            if(Build.VERSION.SDK_INT<=Build.VERSION_CODES.LOLLIPOP){
                countries.add("Sri Lanka");
            }
            Collections.sort(countries, String.CASE_INSENSITIVE_ORDER);

            return countries;

        }



@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    countryAutoTextView.setAdapter(new ArrayAdapter<String>(this, R.layout.country_list_spinner_loader, getCountryList()));

    changeLanguageButton.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
            Locale locale = new Locale(language);
            Resources res = getResources();
            DisplayMetrics dm = res.getDisplayMetrics();
            Configuration conf = res.getConfiguration();
            conf.locale = locale;
            res.updateConfiguration(conf, dm);
            finish();
            Intent refresh = new Intent(this, MainActivity.class);
            startActivity(refresh);
      }
   });   

}

country_list_spinner_loader.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/location_text_view"
    android:maxLines="5"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:gravity="start"
    android:ellipsize="marquee"
    android:textColor="@color/colorBlack"
    android:textSize="14sp"/>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/first_Screen_greeting_layout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/greeting_layout_height"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="#80000000"
        android:orientation="horizontal"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:layout_marginTop="2dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:src="@android:drawable/ic_menu_search"
                android:layout_gravity="center"/>

            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusableInTouchMode="true">


                <AutoCompleteTextView
                    android:id="@+id/main_activity_country_auto"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/auto_text_height"
                    android:focusableInTouchMode="true"
                    android:textColor="@color/colorWhite"
                    android:textSize="@dimen/country_auto_text_size"
                    android:theme="@style/Autocomplete" />

            </android.support.design.widget.TextInputLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="@dimen/time_image_view_left_margin"
            android:layout_height="wrap_content"
            android:orientation="vertical">

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

Для английского языка он отлично работает для всех версий Android. Но после изменения языка «Locale», особенно «Sinhala» и «Hindi» в соответствии с предпочтениями пользователя, «AutoCompleteTextView» не загружает страны для Android Nougat (24) в реальных устройствах, а также в эмуляторах.

Наконец, я наблюдаю эту проблему с помощью Android 'Log cat', когда на устройстве с отладочным геймплеем установлена ​​Android Nougat. То есть, когда языком «Locale» является английский, «Log cat» отображает следующее и «Auto complete text view» загружается правильно.

07-03 15:20:01.116 17445-17445/com.sample D/AbsListView: Get MotionRecognitionManager
07-03 15:20:01.119 17445-17445/com.sample D/MotionRecognitionManager: mSContextService = com.samsung.android.hardware.context.ISemContextService$Stub$Proxy@5ba90df
07-03 15:20:01.121 17445-17445/com.sample D/MotionRecognitionManager: motionService = com.samsung.android.gesture.IMotionRecognitionService$Stub$Proxy@40dd12c
    motionService = com.samsung.android.gesture.IMotionRecognitionService$Stub$Proxy@40dd12c
07-03 15:20:01.127 17445-17445/com.sample D/TextView: setTypeface with style : 0
07-03 15:20:01.137 17445-17445/com.sample D/ViewRootImpl@ce15bfb[PopupWindow:333598a]: ThreadedRenderer.create() translucent=true
07-03 15:20:01.143 17445-17445/com.samplea D/InputTransport: Input channel constructed: fd=77
07-03 15:20:01.144 17445-17445/com.samplea D/ViewRootImpl@ce15bfb[PopupWindow:333598a]: setView = android.widget.PopupWindow$PopupDecorView{51cd418 V.E...... ......I. 0,0-0,0} touchMode=true
07-03 15:20:01.147 17445-17445/com.sample D/ViewRootImpl@ce15bfb[PopupWindow:333598a]: dispatchAttachedToWindow
07-03 15:20:01.177 17445-17445/com.sample D/ViewRootImpl@ce15bfb[PopupWindow:333598a]: Relayout returned: oldFrame=[0,0][0,0] newFrame=[0,664][1440,820] result=0x27 surface={isValid=true 491593586176} surfaceGenerationChanged=true
    mHardwareRenderer.initialize() mSurface={isValid=true 491593586176} hwInitialized=true
07-03 15:20:01.180 17445-17575/com.sample D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000,  [1696x412]-format:1
07-03 15:20:01.181 17445-17445/com.sample D/AbsListView:  onsize change 
07-03 15:20:01.185 17445-17445/com.sample D/TextView: setTypeface with style : 0
07-03 15:20:01.207 17445-17445/com.sample D/ViewRootImpl@ce15bfb[PopupWindow:333598a]: Relayout returned: oldFrame=[0,664][1440,820] newFrame=[0,664][1440,820] result=0x1 surface={isValid=true 491593586176} surfaceGenerationChanged=false
07-03 15:20:01.218 17445-17445/com.sample D/ViewRootImpl@ce15bfb[PopupWindow:333598a]: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1

Но, когда языком «Locale» является «Sinhala» или «Hindi», над «Logcat» строки не отображаются и ничего не загружается в «Auto complete text view».

1 Ответ

0 голосов
/ 04 июля 2018

Для загрузки списка стран для AutoCompleteTextView.

final ArrayAdapter<String>adapter = new ArrayAdapter<String>(this, R.layout.country_list_spinner_loader, DataLists.getCountryList());
        countryAutoTextView.setAdapter(adapter);

        adapter.registerDataSetObserver(new DataSetObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                Log.d("MAIN_COUNTRY_ADAPTER", "dataset changed");
                Object item = adapter.getItem(0);

                Log.d("MAIN_COUNTRY_ADAPTER", "item.toString "+ item.toString());
            }
        });

Вместо следующего метода я использовал жестко закодированные страны для списка.

public static ArrayList getCountryList()
    {
        Locale[] locale = Locale.getAvailableLocales();
        ArrayList<String>countries = new ArrayList<String>();
        String country = "";
        for( Locale loc : locale ){
            country = loc.getDisplayCountry();
           if( country.length() > 0 && !countries.contains(country) ){

               countries.add( country );            
           }
        }

       if(Build.VERSION.SDK_INT<=Build.VERSION_CODES.LOLLIPOP){
            countries.add("Sri Lanka");
        }
       Collections.sort(countries, String.CASE_INSENSITIVE_ORDER);

       Log.d("DATA_LIST_CLASS", "COUNTRIES_LIST_SIZE_IS: " + countries.size() + countries.get(0) + countries.get(1));

        return countries;
    }

Потому что это также использовало 'Locale' для загрузки стран. Это проблема для изменения языка устройства и загрузки 'AutoCompleteTextView'.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...