I have a problem with setText(). Although there were many questions about this issue, no single answer solved my problem..
I have one city name saved with SharedPreferences. I get city successfully from SharedPreferences. I want to do the following textview.setText(city) [of course, firstly I determine the textview with findbyId-proper, existing id ]. This is done in onResume() method.
Than I use getText() and System.out.println(getText()) shows exactly the right, saved city. The UI shows the text which was defined at the XML layout of the activity.
I have no idea what to do and how is this even possible. I do not get any errors.
Please, help.
In onResume() method right after super.onResume(); :
city=settings.getString("savedLocation", "null");
textView.setText(city);
System.out.println(city + " " textView.getText());
textView.getText() returns not text string, but object. You should use textView().getText().toString() to retrieve text from a text view
Related
FireStore Array
I already retrieve the vaccine name and the number of count but the problem is the SPACE is also replace or being removed.
Retrived
I WANT TO OUTPUT THE ITEM LIKE THIS
WITH SPACE SAME VALUE IN THE FIRE STORE
I ALREADY GET IT USING THIS CODE BELOW IT I HOPE YOU GET THE POINT OF THE CODE BELOW CLICK THE LINKED TEXT TO SEE IT
IMAGE FOR THE CODE
Basically I used the Character.isDigit, isAlphabetic, isWhiteSpace
then save it to stringbuffer. then save it to my textview
I assigned my buffer variable for text to get also the whitespace.
I have a dynamic arraylist that contains String values. I want to match one of its values with AutocompleteText whatever the user has typed/entered. In case the value is not matching with any of the Arraylist values I would like to clear Autocomplete Text.
Couple of Ways I have tried.
Match the typed or user selected value from autocomplete text with list directly without any loop using "contains" method. Didn't achieve the expected result.
Store the values in String[] array from list and loop through it and match it with user input/selection from Autocompletetext. Didn't achieve the result this way too.
Please provide any ideas on how to clear text from autocompletetextview if value is not found?
You did not specify any language that's why i will answer in kotlin
Override onKeyDown method and let's say you will check when enter pressed check if given keycode value equals to KeyEvent.KEYCODE_ENTER
In if block get your text and use an algorithm like below
val arr = intArrayOf(1,2,3) if (textFromTextView !in arr) it means your typed text not in your array and if this condition is true
use autoCompleteTextView.replaceText("") and i hope it will work for you.I never tried but i searched for you.
How to set the string values on spinner prompt?
The process how I am doing is:
I am able to retrieve the json data(it has only one single value), so I am getting the single text with the help of String.
Later, I am trying to set the string value in Spinner prompt
SP_gender is called as Spinner here. In String gender1, I have texts called as "male"
String gender1 = i.getStringExtra("bundle_CusGender");
SP_gender.setPrompt(gender1);
System.out.println("Check bundle_CusGender = : " + gender1);
When I try to print this, I am getting a value as male
System.out: Check bundle_CusGender = : male
How should I set the single text in spinner Android?
Firstly the setPrompt() methods documentation states:
Sets the prompt to display when the dialog is shown.
Which is pretty vague but internally it calls the setPromptText() method which sets the description on the popup and has the following documentation.
Set hint text to be displayed to the user. This should provide
a description of the choice being made.
So if I am understanding your question correctly you want to set the spinners selected item. You should be using either setSelection(int) or setSelection(int, boolean)
For those to work you would need to give your spinner an adapter if you haven't already. If you don't know how you should check out this answer which explains in more detail.
I have an app widget that does some stuff in AsyncTask's doInBackground() method and the final steps are to get the list of store names, that looks like this:
Store storeObject=store.getStores().getItems().get(0).getStore();
String name= storeObject.getName();
//name of the store
return name;
Right now I am getting the name at 0th position.
Now I want all the names of the stores (that are 27 in my code being fetched) in my widget text view one by one when you click a next button.
So how should I pass this name in my remote views back in onPostExecute() of AsyncTask.
updateViews.setTextViewText(R.id.text_view, name);
updateViews.setOnClickPendingIntent(R.id.next, pendingIntent);
Where should I use a for loop? in doInBackground or onPostExcecute or both? How can I use loop in remoteviews and update it? Please please help. I am a newbie.
I got it. Its a list so I generated an index that's randomly updated, no need of loop.
In my onCreate() I have:
if(sharedPref.getString("name",null)==""){EditText.setText("Something");}
else{EditText.setText(sharedPref.getString("name",null));}
And then in my onStop() I have:
sharedPrefEditor.putString("name",EditText.getText().toString());
The EditText shows only the hint when I first install and run it. It does seems to display the correct text when it's started later, however.
Don't use == to compare content of Strings. Use equals() instead :
if(sharedPref.getString("name",null).equals("")){
EditText.setText("Something");
}
Why should I use equals instead of ==
First of all you are not using correctly the shared preferences. You are typing null as second parameter which is the value you will get if the key you typed as first parameter is not defined. This is not a problem itself but then in your if sentence you are comparing it to an empty string.
So first of all use equals instead of ==and then if you want to receive an empty string if the key is not defined type an empty string as second parameter of SharedPreferences.gerString method.
Hope it helps ;)
If "Something" is to be used if preference not set, then:
EditText.setText(sharedPref.getString("name","Something"));
getString() will automatically return "Something" if preference is not set. No need of extra code.
String strName = sharedPref.getString("name","");
if(!strName.isEmpty()){
EditText.setText(strName);
}else{
EditText.setText("Something");
}
The operator == can not be used to compare strings in Java.
if(string1.equals(string2){
}
Also, you set a value to SharedPreferences, but you never commit the changes.
sharedPrefEditor.commit();
OnCreate
String prefsVal = sharedPref.getString("name", null);
if(prefsVal.equals(null)){ //If default value was returned
EditText.setText("Something");
}else{
EditText.setText(prefsVal);
}
OR
The following will turn your posted code into a single line solution. The second parameter passed to SharedPref is the default value to be returned if no vale was found for the given key.
EditText.setText(sharedPref.getString("name", "Something");
OnStop
sharedPrefEditor.putString("name", EditText.getText().toString());
sharedPrefEditoy.commit();