Hi I'm very new to android and I'm trying to do the following. Provide some word completion for the sentence.
Here is the code I have for it:
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
// In the onCreate method
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.actv_country);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES);
textView.setAdapter(adapter);
where it automatically pop up when user types Belgium. But the problem I face is, when the user type this sentence:
Belgium France
for the first word Belgium the pop up comes, when I type France autocompletion doesnt work. Why?
Where I'm making the mistake?
Thanks in advance.
You should use MultiAutoCompleteTextView for multiple words
Here is a good example of that
Comma is the default separator for MultiAutoComplete, you can set it to space delimiter, please have a look at this post to achieve that.
Have a look at this post to for space delimiter.
You are searching for "Belgium France", but you don't have this item in your list (in one String object). You will have to create custom Adapter which implements Filterable with custom Filter.
Related
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!
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.
I have a string with the name of my button. Say it is called String A.
String A = myButtonName;
Now, if I want to remove the button by doing:
layout.removeView(myButtonName);
This would work, but, I can't do that on a string.
How can I do it on my string?
Like this, right now I am getting an error, since it is a string:
layout.removeView(A);
How can I remove a view with a string which corresponds to a view?
Theoretically, I want to typecast my string to a ViewGroup
Simplified question:
I have a string. That string is also the variable name of my button.
Can I remove the button using the string?
Why won't you do it this way??
View namebar = view.findViewById(R.id.namebar);
((ViewGroup) namebar.getParent()).removeView(namebar);
You cannot directly cast String to View or ViewGroups but you can try this method:
Check all button name and compare it with you button name . Use
button.getText() to get the text of button and then compare it with
your button name "myButton".
If a match found, remove that button using your removeView(Button).
Else, ignore.
Firstly, I couldn't understand that why you have to reversely to find a view from a string. Still, whatever condition you are in, I am answering your question.
I read your answer too, it might give you desired output, but I feel the approach is wrong.
So, you have a String having the button's id name. So, you can get it's resource_identifier(int) from the string. like
String btnName = "btn_test";
int id = getResources().getIdentifier(btnName, "id", getPackageName());
Button btnTest = (Button)findViewById(id);
So, once you got a btnTest holding the View, you can remove it. With this method you won't need to hardcode anything. Hope this helps..
I got it! So, because I have only a string, I can just check to see if that string equals the object:
if(myButtonName.equals("Blue")){
View myView = (View) findViewById(R.id.Blue);
((ViewManager)v.getParent()).removeView(v);
}
Then I can manually remove the object itself. Because, as Sai mentioned, it is impossible to remove a view with a string object; I just removed the view after checking it through an if statement, and replacing the object.
You can also use switch:
switch(hello) {
case "Blue":
View myView = (View) findViewById(R.id.Blue);
((ViewManager) v.getParent()).removeView(v);
break;
I have about 500 sentences in which I would like to compile a set of ngrams. I am having trouble removing the stop words. I tried adding the lucene StandardFilter and StopFilter but I still have the same problem. Here is my code:
for(String curS: Sentences)
{
reader = new StringReader(curS);
tokenizer = new StandardTokenizer(Version.LUCENE_36, reader);
tokenizer = new StandardFilter(Version.LUCENE_36, tokenizer);
tokenizer = new StopFilter(Version.LUCENE_36, tokenizer, stopWords);
tokenizer = new ShingleFilter(tokenizer, 2, 3);
charTermAttribute = tokenizer.addAttribute(CharTermAttribute.class);
while(tokenizer.incrementToken())
{
curNGram = charTermAttribute.toString().toString();
nGrams.add(curNGram); //store each token into an ArrayList
}
}
For example, the first phrase I am testing is: "For every person that listens to". In this example curNGram is set to "For" which is a stop word in my list stopWords. Also, in this example "every" is a stop word and so "person" should be the first ngram.
Why are stop words being added to my list when I am using the StopFiler?
All help is appreciated!
What you've posted looks okay to me, so I suspect that stopWords isn't providing the information you want to the filter.
Try something like:
//Let's say we read the stop words into an array list (A simple array, or any list implementation should be fine)
List<String> words = new ArrayList();
//Read the file into words.
Set stopWords = StopFilter.makeStopSet(Version.LUCENE_36, words, true);
Assuming the list you of stopwords you generated (the one I've named 'words') looks like you think it does, this should put them into a format usable to the StopFilter.
Were you already generating stopWords like that?
All- I have an app in which the user enters the names of players in a game. He/she can enter 2-4 players. The app takes the names and puts them into a spinner. When the user enters 4 players it works great but when they enter only 2 or 3 players, the spinner has 2 or 1 (respectively) empty spaces. How can I make it so when the user enters a number of players less than 4, only that number of names appears in the spinner (no empty spaces). Here is the code I am using:
String[] items = new String[] {"No Owner", message, message2, message3, message4};
Spinner spinner = (Spinner) findViewById(R.id.owner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
message= Player 1,
message2= Player2,
etc.
Sample code welcome, and thanks for your time.
EDIT:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String message2 = intent.getStringExtra(MainActivity.ANOTHER_MESSAGE);
String message3 = intent.getStringExtra(MainActivity.YET_ANOTHER);
String message4 = intent.getStringExtra(MainActivity.AND_ANOTHER);
setContentView(R.layout.next_main);
You could check which messages are empty and then modify your items array based on that information. The goal being to pass an array to your ArrayAdapter with no extra spaces in it
Edit:
List<String> playersList = new ArrayList() ;
if(!message.equals("")){
playersList.add(message);
}
etc..
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, playersList);
Since you're importing your player names into a string, I would run string compare to see if each string matches the default value, if there is no player filling that slot. In other words, if
message.compareTo("")
returns 0, don't include that in items, which would be best used as an ArrayAdapter. You can do this through a simple if block.
Example code:
ArrayAdapter items = new ArrayAdapter<String>(this, int textViewResourceId);
if (message.compareTo("") != 0) {
items.add(message);
}
if (message2.compareTo("") != 0) {
items.add(message2);
}
....
And you would keep going with the rest of your items, using the resulting array (which you can pull out using toString()) to generate the Spinner.
EDIT: Fixed constructor code.
EDIT 2: Fixed textViewResourceId in the constructor.