Search code examples
androidsharedpreferencesautocompletetextviewsettext

AutocompleteTextView setText or setHint not working


So in my first activity i'm getting the current location as currentLocation through reverse geocoding and i use sharedPreference to pass the value to the next activity (second.java/class). This class consist of an autocompleteTextView whose values are stored in an array.

Now what i want is when the user goes from the first activity to the second activity the autocompleteTextView should contain the location obtained from the first class as a hint or a Text

So this is my firstclass.java

Toast.makeText(this,currentPlace, 1000).show(); //works
SharedPreferences preferences = getSharedPreferences("myPrefss",  getApplicationContext().MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("CurrentPlace", currentPlace);
editor.commit();

and my secondclass.java

SharedPreferences preferences = getSharedPreferences("myPrefss",  getApplicationContext().MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
String curlocation;
from = (AutoCompleteTextView) findViewById(R.id.et_login_form);
curlocation = editor.putString("CurrentPlace","");
from.setText(curlocation); //doesn't work
from.setHint(curlocation); //doesn't work

and this is my XML

        <AutoCompleteTextView
            android:id="@+id/et_login_form"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="5dp"
            android:inputType="textMultiLine"
            android:textColor="@android:color/black" />

and i've tried setting the text in xml throught

android:Text= "sample text"
android:Hint= "sample text"

which shows up in the graphical view but not in the MobilePhone


Solution

  • Try to use

    curlocation = preferences.getString("CurrentPlace",""); 
    

    instead of

    curlocation = editor.putString("CurrentPlace","");
    

    editor only used for store value in sharedPreference. but for getting you only user instance of sharedPreference.