Android Search fast performance in listview - android

I am making a search application in android, my searching purpose is that when i type word in edittext then when the textchange, it will jump to select the position of the word that begin with that letter in listview.
This is my code. it work well but the speed of text when i type or delete in edittext seems slow, not smoothly.
What can i do to make it faster?
I have over 20,000 entries from database.
txtword.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count){
// TODO Auto-generated method stub
for(int i =0;i<list.size();i++)
{
if(list.get(i).toLowerCase().startsWith(s.toString()))
{
pos = i;
break;
}
}
lv.setSelection(pos);
}

You can use the patricia trie data structure to perform the search:
Here are the typical steps:
Populate your Strings into the patricia trie.
Perform look-up for strings starting with the entered characterd in onTextChanged().It will return you a sub-trie. For next character entered, search in that sub-trie.
When a text is removed from the editText, go back one level in the trie.
Here is the reference:
https://code.google.com/p/patricia-trie/
And, here is a sample example:
https://code.google.com/p/patricia-trie/wiki/Examples

you can user search filter in baseAdapter and arrayadapter for Listview.
use this
1)https://stackoverflow.com/a/2726348/942224
2)https://stackoverflow.com/a/14359161/942224
to get detail information.

You may consider that iterating an ArrayList with over 20000 may take a lot of resource. I think you have to reevaluate the problem and how you can solve it.
Why don't you try to research via SQL request ? You can use the "LIKE" operator.
SQLite Android Doc
Like operator example

Related

Random string puller for a quotation viewer

so I am trying to create an area which shows quotes in my app. The quotes are saved within a string file. I am currently using a code like
public void Quotes(){
int randNum= random.nextInt((100+1)-1)+1;
String randQuote="";
switch (randNum){
case 1 :
randQuote=getString(R.string.quoteA);
}
}
}
Here I have to numerate any and all strings and represent them as cases. I have a hundred quotes.
Is there any method to complete the same task in a less redundant manner?
If yes then what method would you advise me.
Thank you in advance.

AutoCompleteTextView is taking much time to search in huge Sqlite database

I have my own dictionary application WordYard and in that whenever we type in AutoCompleteTextView i am showing the words list in dropdown.
In addTextChangedListener of autoCompleteTextView i am querying for written text in sqlite database. Since database is very huge of 1.5 lac words it takes time to make the arraylist of particular text.
Suppose I wrote 'A' then this string will searched in database and written inside arraylist of limit 15 data starting from 'A'. On scrolling the dropdown list i am adding next 15 words in getView of adapter.
Scrolling is fine but whenever i type in autocompleteTextview to read 15 words from database also it take time if we write faster. Please tell me if there is any other method i can do it to make my app better.
Everytime you type anything within AutoCompleteTextView a query to your relative huge database is sent thus causing the (justified) delay.
The addTextChangedListener (TextWatcher watcher); method needs a TextWatcher object to operate. What your are going to to is create a TextWatcher and override it's afterTextChanged (Editable s) method in order to perform queries to your database less often.You will also need a custom Filter for that.
autoCompleteTextView.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
yourAdapter.getFilter().filter(s);
}
then you will create a class that extends [Filter][1] and override the methods suitable to your needs.
For example optimize your implementation to send queries only after 4 characters are typed or some time has passed.

Android - Override user edittext input and add characters towards a specific text?

So I'm currently trying to achieve the following: I have one big piece of text not stored in my app yet. I will have one big EditText and what I want to do is when a user touches any key on the keyboard, a part of my text (like 2 or 3 characters at the time) should appear in the EditText. Then when a user touches a key again, another 2 or 3 characters of the text should appear inside the EditText. I've thought a lot about it but I can't seem to find the right approach to do it. Where to store my Text and how to code so that the app overrides whatever the user inputs and adds 2 or 3 characters in the EditText according to my text that I need to store somewhere.
Any ideas?
Take a look at
http://developer.android.com/reference/android/text/TextWatcher.html
Implement a TextWatcher and check the user input in onTextChanged() to determine the user input. Mark the input the user has made with a Span (which can be any custom object, it is just used to tag the text area), so you can later look it up with Editable#getSpans() in afterTextChanged(), where you can replace the whole span with your override text.
Code idea (untested):
onTextChanged(CharSequence s, int start, int before, int count){
((Spannable)s).setSpan(new MyMarkObject(), start, start+count, Spannable.SPAN_MARK_MARK)
}
afterTextChanged(Editable s)
{
MyMarkObject[] markers = s.getSpans(0, s.length(), MyMarkObject.class);
for (MyMarkObject marker: markers){
int start = s.getSpanStart(marker);
int end = s.getSpanEnd(marker);
s.replace(start, end, getYourDesiredReplacementTextFor(s.subSequence(start,end));
}
}

EditText.setText() gives index out of bounds

I'm having an error I can't seem to wrap my head around. I'm writing an app that allows the user to send text from an EditText to a TextView. If the user makes a mistake, the user can hit the space key to bring the most recent text sent from the EditText to the TextView. This works sometimes, but other times, it gives me an IndexOutOfBounds exception.
textInput is an EditText, back1,2,3 are the three most recent strings (with back1 the most recent)
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(count==1&&before==0&&s.toString().equals(" ")){
textInput.setText(back1);
}else if(s.toString().equals(back1 + " ")){
textInput.setText(back2);
}else if(s.toString().equals(back2 + " ")){
textInput.setText(back3); //causes error if back2 > back3
}
textInput.setSelection(textInput.getText().toString().length());
The above code checks if the user hit the space key, and if so, what to do with it. If the user pressed space on an empty EditText, they get the last thing they sent. If they hit space again, they get the next to last thing they sent, and so on. This is still a bit rough, but I hope you get the idea.
The OutOfBounds exception comes from taking a large item in the EditText, hitting space, and setting the EditText to a smaller string. I assumed it was because the cursor is at the end of the EditText and could no longer be there when the text got smaller, so I tried adding textInput.setSelection(0) right before the setText(). That didn't help. I also tried setting the EditText to setText(""). That didn't work either. If I comment out the lines of setText(back#), everything works fine.
An example:
A user types in "hello", "hi" and "hey" in that order.
back3 = hello, back2 = hi, and back1 = hey.
Hitting space once will set the EditText to "hey"
A second tap will crash, since the setSpan(3...4) ends beyond length 2, presumably because back 1 is larger than back2. It is supposed to set the text in the EditText to "hi"
From the TextWatcher documentation:
public abstract void onTextChanged (CharSequence s, int start, int before, int count)
Since: API Level 1
This method is called to notify you that, within s, the count characters beginning at start have just replaced old text that had length before. It is an error to attempt to make changes to s from this callback.
(My emphasis.)
You should use afterTextChanged instead if you want to change the text more in response to an existing change. Even in this case, your handler will be called re-entrantly when you change the text from afterTextChanged, so use an extra precaution to make sure you won't get into an infinite loop, such as something like this:
public void afterTextChanged (Editable s) {
static boolean is_reentrant = false;
if (!is_reentrant) {
is_reentrant = true;
try {
// do stuff
} finally {
is_reentrant = false;
}
}
}
I haven't tested this exact snippet, but something like that will make your code only run if it's not already running. You don't need to worry about thread-safety in this case, because it's only being called from inside the same thread.

Android - TextWatcher is invoked once for each character in pasted text

I've added a TextWatcher to an EditText and am listening for changes in the text via the onTextChanged(CharSequence s, int start, int before, int count) method. When I paste text that has say 10 characters, into this EditText, onTextChanged() gets called 10 times, once for each character in the text I pasted, from left to right. I want onTextChanged() to be called only once after all 10 characters have been pasted into the EditText. I'm sure this should be possible, because otherwise what's the point in having the "count" param if it's always going to be 1?
count won't always be 1: for instance, if you select and delete a block of text or if you choose an autocomplete option.
In any case, the details of whether pasting happens in one chunk or one character at a time is an implementation detail, and if you rely on either behavior it's likely your app will break in the future.
Try using afterTextChanged it will only get one call

Categories

Resources