autoCompleteTextView shows results only after deleting a letter - java

I created a layout that has an autoCompleteTextView in it that should show some names of books.
I noticed that it show no results in some cases unless I delete one letter and then it shows the correct results.
For example, I have an array string of books that has the word Potter in them.
When I type Potter, it shows no results:
However, if I then delete one letter it suddenly shows:
The code im using to populate the list is:
atv_Search.addTextChangedListener( new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 0) {
SOME CODE TO GET NAMES
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
Set<String> set = new LinkedHashSet<>( autoBookNames );
autoBookNames.clear();
autoBookNames.addAll( set );
// UNTIL THIS POINT IT ALL WORKS GOOD AND I SEE THAT authBookNames IS NOT EMPTY, YET NOT SHOWING UNTIL DELETE
listAutoAdapter = new ArrayAdapter<String>( getApplicationContext(), R.layout.item_auto_search, autoBookNames );
atv_Search.setThreshold( 1 );
atv_Search.setAdapter( listAutoAdapter );
}
} );
As I wrote in the code when I debugged it, I did get all the results even before the deletion but it didn't show it for some reason.
Any reason for this? I mean most users won't think to delete a letter to show the results.
Thank you

try==>
listAutoAdapter = new ArrayAdapter<String>( getApplicationContext(), R.layout.item_auto_search, autoBookNames );
atv_Search.setAdapter( listAutoAdapter );
atv_Search.addTextChangedListener( new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
listAutoAdapter.getFilter().filter(s);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
} );
let me know if this works

Do you think that your compare strings' index in "SOME CODE TO GET NAMES" is wrong (such as last index is smaller than the correct one 1 character)

Move
listAutoAdapter = new ArrayAdapter<String>( getApplicationContext(), R.layout.item_auto_search, autoBookNames );
atv_Search.setThreshold( 1 );
atv_Search.setAdapter( listAutoAdapter );
into onTextchanged and try once, Ben!

Related

How to get the selected cursor position in EditText?

There is an EditText, what I want is when I select it ,I should get the cursor position. The maximum length of the edittext is 13.
Also in my edittext, the first three character's type is text and the other character's type is number. If the first three characters are selected, the input type of keyboard should show text. And if any other characters are selected ,then the input type should be number. How to do this?
I think this code can help you ❤ :
Class Code :
edittext = (EditText) findViewById(R.id.edtxt);
edittext.setInputType(InputType.TYPE_CLASS_TEXT);
edittext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(final CharSequence s, int start, int before, int count) {
if (edittext.getText().length() < 3 ) {
edittext.setInputType(InputType.TYPE_CLASS_TEXT);
}
else if (edittext.getText().length() == 3 ) {
edittext.setInputType(InputType.TYPE_CLASS_NUMBER);
}}
#Override
public void afterTextChanged(Editable s) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
});}}
Xml Code :
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:maxLength="13"
android:id="#+id/edtxt"
android:ems="10"/>

Calculation with textWatcher android

In my project I have 7 EditText field.
User will only need to fill their own value in first two EditText.
I need a series calculation like this...
(after user input) subtract ed1-ed2 the result automatically show in ed3 field.
multiply ed3*2 = result show in ed4 field automatically.
multiply ed3*3= result will show in ed5 field.
multiply ed3*4=result will show in ed6 field.
Finally sum of ed4+ed5+ed6= result will show in ed7 field automatically.
So how to do it any one please help.
Thank you in Advance.
All EditText use a same TextWatcher. A simple solution like this.
EditText et1;
EditText et2;
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int num1 = Integer.valueOf(et1.getText().toString()); // Make sure you get a number
int num2 = Integer.valueOf(et2.getText().toString());
// do your calculation
}
#Override
public void afterTextChanged(Editable s) {
}
};
et1.addTextChangedListener(textWatcher);
et2.addTextChangedListener(textWatcher);

Getting EditableText from EditText in RecyclerView

I've recycler view with 0 items, I've added an option to manually add the items, my stracture is simple :
RV_Item.xml (contains EditText).
MyItem, which is an Object for RV ( contains private String Text; ).
MainActivity.java, where the stuff happen.
// My List<Object>
List<MyItem> Items = new ArrayList<>();
// For Adding, I've added FAB-Button, When Clicked, it does the following :
Items.add(new MyItem());
CheckForEmptyItems();
mAdapter.notifyItemInserted(0);
Now, When the user click the save button, i want to take all the edittext in all the items he had added, i'm doing in the following way :
for(MyItem items : Items){
Log.i("PrintingInfo", items.getText() );
}
The problem is, i'm not getting the text he entered in all EditText fields, and it's returning Null in all of them, What's the issue in this ?
So, i don't know why always i know the answer after posting, but here's how you gonna know what the user typed :
in your Adapter Class, in onBindViewHolder method, add textlistener for the EditText, here's an example :
holder.MyEditText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
itemList.get(position).setText(s.toString());
}
});
Hope that helps you!

How to extract String using AutoCompleteTextView?

I know that there are multiple posts in Stackoverflow addressing this query. However, for some reason I am still failing to extract the string from AutoCompleteTextView. I tried using the onItemClickListener for this purpose. I am unable to identify where I am going wrong.
The Code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
addPurchaseItemName = (AutoCompleteTextView) findViewById(R.id.addPurchaseProductName);
vivzHelper = new VivzDatabaseAdapter(this);
String[] autoCompleteName = vivzHelper.getInventoryNameFilterBySupplierName(vivzHelper.getSupplierID(param1));
ArrayAdapter<String> NameAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, autoCompleteName);
addPurchaseItemName.setThreshold(1);// starts working from first char
addPurchaseItemName.setAdapter(NameAdapter);
addPurchaseItemName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
itemName = String.valueOf(arg0.getItemAtPosition(arg2));
}
});
}
I want to assign the value of the extracted string to itemName which is initialized at the beginning of the activity. Can some one point out where am I going wrong? I have surfed multiple resources. Maybe, I am missing something.
Note :
This code was already posted to address a an issue on IllegalArgumentException in StackOverFlow a couple of days ago. Since, the topic of that question doesn't point more specifically the problem posted here, I thought that posting a new question will make sense. Hence I hope, my question won't be down-voted or edited as duplicate
Update 01 : #Deividi Cavarzan forgot including the below line of code when editing this question
ArrayAdapter<String> NameAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, autoCompleteName);
Update 02 : Declaring the itemName
public class AddPurchase extends ActionBarActivity {
AutoCompleteTextView addPurchaseItemName;
String itemName;
try to get the itemName on addTextChangedListener callback
addPurchaseItemName.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
itemName = s.toString();
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
if you just want to get the selected item string from auto complete, then
itemName = addPurchaseItemName.getText();
or even better-
itemName = addPurchaseItemName.getText().toString().trim();
Updated answer
instead of setting onItemClickListener , set OnItemSelectedListener , this should definitely work.
addPurchaseItemName.setOnSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected (AdapterView<?> parent, View view, int position, long id) {
//... your stuff
itemName = addPurchaseItemName.getText().toString().trim();
Toast.makeText(getApplicationContext(), "selected value - "+itemName, Toast.LENGTH_SHORT);
}
});

Searching partial word in a arraylist on android

Im having a lot of problems with searching for part of a word, and then making it find the full result afterwards.
Like if im searching "droid", it looks through my list for anything that contains "droid", if you look at my arraylist underneath it should find "Android"
Arraylist code:
private List<String> getModel() {
List<String> list = new ArrayList<String>();
list.add("Linux");
list.add("Windows7");
list.add("Suse");
list.add("Eclipse");
list.add("Ubuntu");
list.add("Solaris");
list.add("Android");
list.add("iPhone");
return list;
}
Here's the rest of my code:
public class idchart extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.idchart);
/*Resources res = getResources();
ListView listView=(ListView)findViewById(R.id.list);*/
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
getModel());
setListAdapter(adapter);
EditText filterEditText = (EditText) findViewById(R.id.srcBox);
filterEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
adapter.getFilter().filter(s.toString());
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
As suggested, using Regex, as far I know you would have to iterate the list and try to match every entry.
If you want to avoid iteration, you can use a inverted index approach for each character (like NGram) of the word. If you want to search text starting with what the user sends, you can do it with Java Map (Commons MultiMap). If you want to search by "droid", so you would have to index the position of each character and retrieve the relative results. I don't recommend doing that without a Library like Lucene.

Categories

Resources