Dynamically updating AutoCompleteTextView sometimes doesn't show suggestions - android

I am dynamically updating my AutoCompleteTextView on every 3rd / 4th letter.
For some reason, about 5-10% of the time when the user types the third letter, though I know for sure I am making a valid arraylist and populating the AutoCompleteTextView, it does not show for unique cases. Something I noticed that was consistent was that the list of words that did not show up were from length 5-15 (others range 50+)
Any idea what is going ? Am I missing something about AutoCompleteTextView, where it doesn't think showing 10 or so suggestions is worth it if the datalist is not as big? Should I add irrelevant filer data or would that impact performance? Thanks
Here is some relevant source code..
Textwatcher for text view checks that;
if (((start + count) == 3) || ((start + count) == 4)
|| ((start == 3) && (before >= 1))) {
if (!last.equals(s)) {
thread = new Thread(new AutoCompleteThread(s));
thread.start();
}
}
in my custom thread I group up words .. then i call this on my text view and adapter;
if(words.size() > 0)
{
last = s;
// adapter.clear();
// for(String e : words)
// adapter.add(e);
// adapter.notifyDataSetChanged();
adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1, words);
textView.setAdapter(adapter);
textView.showDropDown();
}

Turns out there is a weird glitch that happens very often if my dynamically loading list is very small and my original list (which comes from my RES folder) is substantially large (500 or so). It doesn't glitch out when the dynamically loaded list is large, perhaps it gives enough time to the AutoCompleteTextView to show suggestions while loading? Not sure about that.
Anyways, the solution was to lower my original size of the arraylist in my res folder

Related

How to display three list view in loop one by one automatic?

I want to display three ListView in loop one by one, with different data after 30 sec interval change to next list view and auto scroll in each and every list view.
Any help regarding this would be really appriciated!
You firstly need to loop statement in which you'll loop through the handler. Then you'll set a delay using the handler (note I've set the delay for 30000ms which is equivalent to 30 seconds; therefore, adjust the figure to your requirement).
for (int i = 0; i < 3; i++) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(i == 0){
//This will be the first loop
}else if(i == 1){
//This will be your second loop
} else{
//This will be the third and final loop
}
}, 30000);
}
You should write a method to set your listview and trigger that method within the run() function. But you can customize that functionality based on your own requirements/how you best understand it. Additionally, because you are going to use different parameters for the listview, you'll have to go through if statements to determine at which part of the loop you are as this will determine which listview is going to be set now.
For multiple view types you can use RecyclerView with multiple view types.
Recycler view has more uses over ListView.
Please check example https://www.journaldev.com/12372/android-recyclerview-example

Accessing String Array in Button Click

I have a string array :
private String [] arrFilePaths=new String[4];
and I have a funtion which returns strings and im adding that to array:
#Override
public void accept(List<File> files) throws Exception {
int size = files.size();
while (size-- > 0) {
arrFilePaths[size]=files.get(size).toString();
}
}
Now i have a button and I am accessing array inside of it:
#Override
public void onClick(View v) {
if(arrFilePaths[0]==null){
//some code
}else {
//some code
}
Now the problem is arrFilePaths only return strings in button click sometime. most of time its returning null.The data always getting inserted to array before button click. so i dont know whats going on.
Yes i have tested in debug mode and arrFilePathsis getting data inserted, but somehow after the button click it saying null(even though I am using same variable).
Can anyone explain about this weird output? Thanks!
As far as I can see, you will be getting null every time when your List<File> files has fewer elements than arrFilePaths array.
Imagine this is files list [L1 , L2 , L3]
And your array is [A1 , A2 , A3 , A4 , A5 , A6]
As you're writing from end, the last element from the end of files will appear at last position of arrFilePaths so on so force.
Finally, array will look like [null, null, null, L1 , L2 , L3].
Obviously, the 1st element of this array will be null.
I am not sure how you are seeing the values sometimes because to me it seems like a logical problem in below code
while (size-- > 0) {
arrFilePaths[size]=files.get(size).toString();
}
Lets assume your size is 3 so you travel here in the reverse order. So you fill the position 3 and 2 and 1 but as soon as size becomes 0, your condition fails and it never sets the value for 0th element.
then your if condition checks -
if(arrFilePaths[0]==null)
So its checking if 0th element is null which is obviously the case because your while loop didn't let it enter there. hence no data.
so if your size is more than 4 in that case your 0th element will get filled as your arrFilePaths can only take 4 elements
In my opinion you should Modify this line
while (size-- > 0)
like this
while (size-- >= 0) {
Just replace that while with a fori, I don't see any reason at all why you would want to populate that array from the end to the front.

RecyclerView keeps repeating cached (?) subview and not looking to onBindViewHolder

I have RecyclerView where every list item has an ImageButton, thee image of which I define in the adapter's onBindViewHolder():
int myVote = getMyVote();
if (myVote != 0) {
Log.d("dbg", myVote + "");
holder.ratingButton.setImageResource(R.drawable.ic_star_grey600_36dp);
}
So ratingButton is a star in the right bottom corner of the list item layout. Its shape is filled with gray color (and accordingly, a log record is pushed) if the condition (myVote != 0) is satisfied.
The problem is that when I scroll the list down I can watch other stars became filled even though I can see the only one record in the log window (for the correct list item). Moreover, this list items with incorrectly changed buttons repeat every 5 rows, and that's what's confusing me. If I changemListView.setItemViewCacheSize(0); the repeat period changes to 3, so we can assume it somehow connected with the RecyclerView's caching and recycling mechanism.
Please, help me to work the problem out. Thanks!
Don't forget to implement
public long getItemId(int position) {}
otherwise, you'll see repeated items in RecyclerView.
Try to change your code to:
if (myVote != 0) {
Log.d("dbg", myVote + "");
holder.ratingButton.setImageResource(R.drawable.ic_star_grey600_36dp);
} else {
holder.ratingButton.setImageResource(int another resource);
}
}
You may also have to write else part of main condition with some another resource like:
if (myVote != 0) {
Log.d("dbg", myVote + "");
holder.ratingButton.setImageResource(R.drawable.ic_star_grey600_36dp);
} else {
holder.ratingButton.setImageResource(int another_resource);
}
It is worked for me.

Linking two NumberPicker views with onValueChanged causes unpredictable crashes

I've created a dialog containing two NumberPicker views. The first contains a list of groups, the second contains the items from the selected group:
Group Group Items
1 2: Group 2 Item 2
[2] [3: Group 2 Item 3]
3 4: Group 2 Item 4
I'm hooking in to the setOnValueChangedListener in the first NumberPicker to populate the second NumberPicker.
mNumberPickerGroup.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker numberPicker, int from, int to) {
int size = mData.getItemsForGroup(to).size();
String[] strings = mData.getItemTitlesForGroup(to);
mNumberPickerItems.setMinValue(1);
mNumberPickerItems.setValue(1);
mNumberPickerItems.setMaxValue(size);
mNumberPickerItems.setDisplayedValues(strings);
}
});
This basically works - until, in certain circumstances, changing the group a few times can cause a crash in the NumberPicker class, when setting the setDisplayedValues strings.
The error is an array index out of bounds exception in the numberpicker for items, a line to do with the string array I passed in. I've set a break point in the update code above, and verified that the String array is always the correct size for the number of items set between min and max value on the number picker, so this has stumped me.
java.lang.ArrayIndexOutOfBoundsException: length=22; index=22
at android.widget.NumberPicker.ensureCachedScrollSelectorValue(NumberPicker.java:1768)
at android.widget.NumberPicker.initializeSelectorWheelIndices(NumberPicker.java:1583)
at android.widget.NumberPicker.setMaxValue(NumberPicker.java:1390)
at uk.co.my.app.fragments.GroupMarkUptoDialog.updateItemPicker(MarkUptoDialog.java:99)
I'm about to start reading through what happens in the NumberPicker to figure out if I'm using it wrong, but any suggestions would be welcome. "ensureCachedScrollSelectorValue" makes me think I need to reset the numberpicker somehow before updating it with new data but I'm not sure.
Can anyone see what I'm doing wrong here?
I realise the NumberPicker is not really a String picker, so if anyone has a better suggestion for how to achieve this sort of UI I'm all ears. Otherwise, I'm heading down the route of trying to implement some kind of debouncer, to update the items picker once all activity on the group picker is complete.
It happens when you setDisplayedValue(String[]) many times.
If the string[]'s length is larger than the current getMaxValue(), Exception happens!
My solution
use
picker.setMaxValue(0);
before
picker.setDisplayedValues(stringArr);
My code
cityPicker.setMaxValue(0);
try {
cityPicker.setDisplayedValues(citySet.toArray(new String[citySet.size()]));
} catch (Exception e) {
Log.e("Ninja", "cityPicker.setDisplayedValues(citys) occurs Error. province is " + province);
}
cityPicker.setMaxValue(citySet.size() - 1);
If you use indexes started from 1 then use:
int size = mData.getItemsForGroup(to-1).size();
Use this to change second picker (if current value is above maximum it will set it to new maximum):
int i = mNumberPickerItems.getValue();
int max = strings.length;
if (i > max)
i = max;
mNumberPickerItems.setMinValue(1);
mNumberPickerItems.setMaxValue(1);
mNumberPickerItems.setDisplayedValues(strings);
mNumberPickerItems.setMaxValue(max);
mNumberPickerItems.setValue(i);
And try to use values as indexes, i.e.
minValue=0
maxValue=strings.length-1

scroll to list item in AlertDialog without selection

I have a huge list of items that I present in an AlertDialog. I would like to present the user the list scrolled to the most likely area they will select one item from. I'm using
AlertDialog.Builder.setSingleChoiceItems(myAdapter, ...).
ArrayAdapter<MyType> myAdapter;
The problem I'm having a hard time with is how to scroll to an item when it's not logically correct to present the item as selected.
I tried getting the ListView from the resulting AlertDialog. But it's empty (even after the Builder creates and shows it).
I tried forcing a populated ListView by inflating a plane ListView in res/layout. listView.scrollTo(x, y) didn't seem to have an effect.
I tried up setting the OnShowListener for the AlertDialog. onShow() is never invoked.
Does anyone know of a work around?
You could use the functions which are part of the ListView class:
smoothScrollByOffset(int offset);
or
smoothScrollToPosition(int position);
Or
if you want to scroll one by one you could use functions like:
private void scrollToNext() {
int currentPosition = getListView().getFirstVisiblePosition();
if (currentPosition == getListView().getCount() - 1)
return;
getListView().setSelection(currentPosition + 1);
getListView().clearFocus();
}
private void scrollToPrevious() {
int currentPosition = getListView().getFirstVisiblePosition();
if (currentPosition == 0)
return;
getListView().setSelection(currentPosition - 1);
getListView().clearFocus();
}

Categories

Resources