Contains method in Java doesn't exist? - java

I am trying to make a java script for a mobile app check if a string contains specific characters.
if(fn.contains(" ")){
}
The fn variable is declared twice in the following:
private EditText fn;
final EditText fn = (EditText) findViewById(R.id.fn);
Not sure why this doesn't work. In android studio, the contains function highlights red as it doesn't exist. You can click here to see what I am talking about. Is there another method than this?

That is an EditText not a string you must do first:
var str = fn.getText().toString()
if(str.indexOf(" ") !== -1;){ . . . }

Related

Failed to get string from autocompletebubbletext

i'm using autocompletebubbletext library (https://github.com/FrederickRider/AutoCompleteBubbleText) which display the list of items to chose from in a list and allow in same time to chose the items from the editetxt..
My problem is as follow:
after the user choses a number of items(=Multiple inputs) .. i want to display a text as an output when clicked on a button (depending on the items chosen of course) as explained in this picture: (https://i.imgur.com/QQuzFvl.png)..
but i got stucked in getting the string of itemsChosen from the edittext
FIRST: i am not sure which return value to use!!
SECOND: i assumed i should use "checkedIds" and I've tried A lot of solution in internet , i've been trying different ideas all day, from what i have tried: ( Ps: i used a toast to see if the methods did work)
edittext.getText().toString() > nothing appears in Toast
i have tried to turn the setHash to String[]: then turning the String[] to one string like:
content=editText.getCheckeditems();//getcheckeditems returns checkedIds which is = new HashSet<String>()
String[] BLANA= content.toArray(new String[content.size()])
data= TextUtils.join(",",BLANA);
it didnt work, in Toast i got"[]"
For the MainActivity.Java (i have the same as here):
https://github.com/FrederickRider/AutoCompleteBubbleText/blob/master/samplelist/src/main/java/com/mycardboarddreams/autocompletebubbletext/samplelist/SampleActivity.java
For MultiSelectEditText.java (i Have same as here) :
https://github.com/FrederickRider/AutoCompleteBubbleText/blob/master/library/src/main/java/com/mycardboarddreams/autocompletebubbletext/MultiSelectEditText.java
WHAT is the solution? (to get a string so i can use it later)
PS: if there is another way(another library or methode) to get what i want to achieve in the first place , i would love to try it..
EDIT: THIS IS A CODE THAT LOOKS PROMISING BUT DIDN'T WORK!
in MultiSelectEditText.java
public String datachosen(){
String [] blan= checkedIds.toArray(new String[0]);
StringBuilder builder = new StringBuilder();
for (String string : blan) {
if (builder.length() > 0) {
builder.append(" ");
}
builder.append(string);
}
String DATATORETURN = builder.toString();
return DATATORETURN;
}
in MAINACTIVTY.JAVA
MultiSelectEditText editText = (MultiSelectEditText)findViewById(R.id.auto_text_complete);
content=editText.datachosen();
Toast.makeText(DecisionTree.this, content,
Toast.LENGTH_LONG).show(); // TOAST INCLUDED IN A BUTTON OF COURSE
OUTPUT: TOAST SHOWS NOTHING!
Solved it ..
i intialize the edit text before on create ..and defin it later after onCreate()..
and got string with the normal edittext.getText().toString(); method!
Simple but was hard to detect the problem!

How to add pre defined text in getText() from Textview and show the result

I have textview where user are asked to enter some information and that information is uploaded in Firebase Data Structure and then is Displayed on another activity
Here is the code I'm using to getText from Textview
etAuthor = (EditText) findViewById(com.nepalpolice.bookbazaar.R.id.editText1);
String bauthor = etAuthor.getText().toString();
and it does job pretty well.
and it is added to firebase.
But what if I want to add predefined Text like
Author:getText()
Here I have added author.
and This will be upudated on Database as well and will Displayed to user
instead of Consider Author is
J. K. Rowling
It will show
Author:J.K Rowling
Any help is appreciated.
Thanks in advance.
you can concatenate the desired string, in you case:
String bauthor ="Author:"+etAuthor.getText().toString();
String bauthor = "Auther : "+etAuthor.getText().toString(); //use this, '+' use for concatenate
#Bir Nepali, you have to just append while getting the text.
etAuthor = (EditText) findViewById(com.nepalpolice.bookbazaar.R.id.editText1);
String bauthor = "Author:" + etAuthor.getText().toString();
Now you can populate this bauhtor in the view.

Android : Get string for its number

I want to show sentences for its number.
Getting number with EditText, and sentences are in string.xml
Name of strings are
sen_(number)
ex: sen_1, sen_25
I tried to make the code to String, so I did like this.
(sentence_num is getString of EditText) (sen_1 is "Hello, world!")
String getTxtString = "getText(R.string.sen_" + sentence_num + ")";
TextView scrambled_sen = (TextView)findViewById(R.id.scrambled_sentence);
scrambled_sen.setText(getTxtString);
But it shows getText(R.string.sen_1), not "Hello, world!".
How can I make it show string with its number?
I want to put getTxtString for a java code, not a String.
You could use getResources().getIdentifier() to get the string but a better and non-complicated way is through a switch case or the if else method.
Something like
If(editText value equals something){
return getString(R.string.sen_1);
}
Minimizes the errors that you could cause through the first getIdentifier() method too.

Android Studio getText() and setText() not working with error: "can't resolve method "getText/setText"()"

So my app needs to read text from a text box with a tag of "inText" do stuff to it (that stuff works) then write the output to a box with the id of "outView". I've been doing this with setText() and getText().
setText() was for writing the output below is what I used:
(TextView)findViewById(R.id.outView.setText(textoutput));
getText() was for reading the input text then writing it to a variable and below is what I used:
String mEdit = (EditText)findViewById(R.id.inText.getText()).toString();
You're chaining the method at the wrong spot. Remove .getText() from R.id.inText and place it after the brackets like (same thing for TextView):
String mEdit = ((EditText)findViewById(R.id.inText)).getText().toString();
Though this is an uncommon way to do thing. Rather initialize the EditText first and then get the text, it's much clearer that way:
EditText mEdit = (EditText)findViewById(R.id.inText);
String mText = mEdit.getText().toString();
You set the getText() and setText() method in wrong place.
getText() and setText() are methods of TextView and EditText classes.
But here you used it as a method of id. That's why it's showing "Can't resolve method getText/setText()". As id has no such methods.
You can do the following.
((TextView) findViewById(R.id.outView)).setText(textoutput);
and
String mEdit = ((EditText) findViewById(R.id.inText)).getText().toString();
It's not working since you need to first set the TextView:
TextView tvOut = (TextView) findViewById(R.id.outView);
TextView tvIn = (TextView) findViewById(R.id.inText);
String out = tvOut.getText().toString();
String in = tvIn.setText(out);
If you use a field check the compiler didn't automatically set it as a view instead of EditText type.
Also the casts are redundant now so you better not use them if you don't have to

how to get value thats displayed in the Emulator?

I am writing a small android "Hello world" application and i would really like to write a small test case . i have written a small test case , have a look below :
public void testMyFirstTestTextView_labelText() {
final String expected = getContext().getResources().getString(R.string.hello_world);
final String actual = getContext().getText().toString();
assertEquals(expected, actual);
}
the expected variable gets instantiated with the value from strings.xml
but for the actual value , i am struggling to understand what methods or snippet i should write in order to get the value of the displayed string .
so how do i get the value of the text displayed in the emulator:
as of now the below statement is wrong:
final String actual = getContext().getText().toString();
i am really new to android , can somebody help me with this please .
if you check the last peice of code in the snippet :
assertEquals(expected, actual);
it checks(using jUnit i think) weather the value of expected is the same as actual.
once again to repeat my question in the above snippet , how do i get/retrive the value of the displayed text ? I.E. on the below line :
final String actual = getContext().getText().toString();
it should be yourTextViewObject.getText() not getContext().getText()
TextView textView = (TextView)findViewById(R.id.hello_world);//Assuming your id in the xml page is textView1
String actual = textView.getText().toString();
This gets the value of an entry hello_world from strings.xml.
String expected = getContext().getResources().getString(R.string.hello_world);
And This gets the value from the TextView.
String actual = textView.getText().toString();
From the android documentation's example
mFirstTestText = (TextView) mFirstTestActivity
.findViewById(R.id.my_first_test_text_view);
String actual = mFirstTestText.getText().toString();
You have a exactly the same example there at the Set Up Your Test Fixture part. Just analyse the TextView with something like this:
final String actual = label.getText().toString();
final String expected = getContext().getResources().getString(R.string.hello_world);
assertEquals(actual, expected)
If your textView is called label you can use something like this:
String s= label.getText().toString();

Categories

Resources