Android Listview Searching to the specific position - android

now i am making a program in android using edittext and listview. I want to search the listview item using edittext above. After populate data to listview, when user type text in edittext, the listview will scroll to the position start with that text. Example: i have item: apple, application, book, boy, car, cat, cash..... when i type b in edittext then listview will scroll to book. I use the listview.setSelection(position), but because the amount of my data is over 30,000 , so when i use the following code it is slow to find data.
Are there any solutions or any other method to do this?
Here is my code:
YOUR_EDITTEXT.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
//LOGIC MAY DIFFER ACCORDING TO YOUR REQUIREMENT..
int POSITION = 0;
for(int i =0;i<list.size();i++) {
if(list.get(i).startsWith(s.toString()))
{
POSITION = i;
break;
}
}
listview.smoothScrollToPosition(POSITION);
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
});

You could take a different approach and load your list data with an AutoCompleteTextView. This would provide your required functionality and it's designed to handle large data sets.
Example:
https://developers.google.com/places/training/autocomplete-android

Is your list sorted? If it is, you can use binary sort on the datasource.

Related

Android: Search from listView with editText and custom tags

I have a program which stores items to database, and I want to make a search view for those items. Database items have a name and a set of tags linked to them (These tags, to be exact: https://android-arsenal.com/details/1/2566). In the search view, I want to list all the items in the db and within the same view, filter the results by name, using input in editText, and by the tags, by showing the available tags as a tag cloud. Does anyone know an example or tutorial where similar search structure would have been implemented?
You may into this tutorial:
http://www.androidhive.info/2012/09/android-adding-search-functionality-to-listview/
You can use filters on the listview as shown in the above example. You can implement your own filters for custom functionality. Use of onTextChangedListener depends upon your use case.
This is code snippet of how to implement simple search function on edittext text change. Hope you get an idea
searchInput.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
if (!s.toString().isEmpty()) {
CenterRepository.getInstance().getSearchResultCollection().getListOfSearchResult().clear();
//Iterate over reference models
for (ReferenceModel model :
CenterRepository.getInstance().getXRefModelCollection().getListOfReferenceModels()) {
//if serach key is present in model name or model number
if (model.getModelName().toLowerCase().contains(s.toString()) || model.getModelNumber().toLowerCase().contains(s.toString())) {
//add to search collection
CenterRepository.getInstance().getSearchResultCollection().addToSearchResultsCollection(model);
}
}
//display search results
renderListData(true);
} else {
//display all models
renderListData(false);
}
}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});

How to lose focus from auto complete text view and gain when focus when user tap on it in android?

I am developing an application with auto-complete text-view. My very first page contains auto-complete text-view which is working as SEARCH field. The moment my application launches, keyboard is also displaying and which hides my other contents in my first page.
I am sure that this will make user to feel bad. I need to overcome this. I need to lose focus of auto-complete text view initially and when user tap on it, keyboard should appear.
This is the listener code of my autocomplete text view.
AutoCompleteTextView jsearch;
jsearch = (AutoCompleteTextView) findViewById(R.id.asearch);
jsearch.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable editable) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
String newText = s.toString();
if(validNet()) {
new getJsonCategory().execute(newText);
mProgressBar.setVisibility(View.VISIBLE);
} else alertBox();
}
});
Two ways you can do.
Step 1:on launching the activity you can stop the popup of key board by the following code paste after oncreate:
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Step 2:
In code make Autocompletetextview .setFocusable(false);
Let me know is it use full or on.so that i can help you more

Android search list invisible by default? Can it be done?

Hi guys I have a question:
In short, I have a listview, with a searchbar on top of it, I am also able to start my other activities after filtering the results etc; basically everything works fine and sweet.
My question is this:
When launching the main activity the listview is visible (obviously enough). Is there a way to make the listview invisible and only after typing in the searchbar to make the results of the listview become visible? Something like an in app search thing; or am I just imagining things?
I hope that I am not too confusing with what I wrote above.
I am looking forward to your replies and I thank you in advance.
Go this way:
set your listview visibility to gone or invisible from your xml file:
android:visibility="gone"
Now apply textwatcher on searchbar edittext
edittext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
//put your search logic here and populate listview.
//after populating listview set its visibility to visible
listView.setVisibility(View.VISIBLE);
//and set listview visibility to GONE again when user erase all text from search edittext
if(s.length() == 0){
listView.setVisibility(View.GONE);
} else {
listView.setVisibility(View.VISIBLE);
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});

Android Listview Searching

I am making a program in android using EditText and ListView. I want to search the ListView item using EditText above. After populate data to ListView, when user type text in EditText, the ListView will scroll to the position start with that text. Example: I have item: [apple, application, book, boy, car, cat, cash.....] when I type b in EditText then ListView will scroll to book. I want to use the listview.setSelection(position), but I don't know how can I get the position from my EditText search. I use the code below, it work well, but it seem slow when we search in EditText. How can I do this and run smoothly?
Thanks in advances.
I use following code:
YOUR_EDITTEXT.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
//LOGIC MAY DIFFER ACCORDING TO YOUR REQUIREMENT..
int POSITION = 0;
for(int i =0;i<list.size();i++) {
if(list.get(i).startsWith(s.toString()))
{
POSITION = i;
break;
}
}
listview.smoothScrollToPosition(POSITION);
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
});
As you have 20,000 list entries, what you currently do is bound to be slow, as you do up to 20,000 startsWith() operations, everytime the user changes the text.
So what can you do:
Simplest solution:
Do NOT select an entry while the user types. Let him finish and click a search button and only then do what you currently do for every text change.
Database solution:
If you have your entries in a database, make a query to get the position. This is much much faster than searching in code. If you don't, can you put it into a database? Is it worth the effect or would the 'Simplest Solution' be good enough?
Try to be smart:
You can try to collect information while the user types, and use that for your advantage. Say the user begins to type Hello. When H is entered you find the first item that starts with H, say at position 1000. Now when the user types e, you don't have to start from the beginning but can start at position 1000 and so on. Of course you also have to handle cases when the user deletes a character or pastes another text and so on, so there is quite some programming logic involved here.

Android: Filter Listview adapter in % searchstring % format

Im using one edittext and listview in my app.
i need to display the list according to the text in edittext.
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
adapter.getFilter().filter(s.toString());
}
eg
if i type the value as "an" means it display like
Andorra
Angola
Antigua
But i need like this
Andorra
Angola
Antigua
Bangladesh
Bhutan
Canada
Thanks in advance
Here is the example exactly which you are looking for. check this
Here is the simple logic with `Arrays and addTextChangedListener for EditText
In Change the block of code as like this.
if (textlength {
if ((String)listview_array[i].contains(et.getText().toString()))
{
array_sort.add(listview_array[i]);
}
I have replace equalsIgnorecase with contains. Please check the difference here

Categories

Resources