Dynamic search results in Android App using SearchView - android

I'm new to Android development and i would like to make a single activity to show featured results using Youtube API and when a search query is typed, i want to update the same list with search results.
I got stuck in my first step, which is configuring the SearchView and receiving data from it. After adding a SeachView to the layout, i made a reference for it in the Activity java code, then internet documentation went too fuzzy for and i couldn't understand what should i do in my case (dynamic results).

you need to make listView's adapter extend from Filterable, and use its getFilter() function according to the query that is changing in the SearchView, using the function setOnQueryTextListener() .
it's that easy.
i also suggest that you watch the "the world of listView" lecture in case you want to learn some tips about using a listView. they also explain how the filter works.

Related

SearchView suggestions: SearchView.OnQueryTextListener vs custom content provider (android:searchSuggestAuthority)

I am trying to implement my first SearchView with suggestions. As far as I understand, I have two possibilities to provide suggestions:
In the searchable.xml configure a custom content provider using android:searchSuggestAuthority which is then queried for suggestions (system calls query() method on the specified custom content provider)
Or implement SearchView.OnQueryTextListener and provide the suggestions via SearchView.setSuggestionsAdapter
Did I understand that correctly? Am I correct that the second possiblity gives me the possiblity to handle the search logic inside a Fragment/ViewModel without creating a content provider as required for the first possiblity?
Is there any advantage of one method over the other? Which one should I use? The Adding custom suggestions guide only mentions the first possiblity (using a custom ContentProvider), but that guide seems to be already quite old.

Segmented Control in Android

We all know segmented control that is IOS but not in android. So I have an application that need something like that. For example let suppose I have a list of students and teacher marks the student either absent or present. and on click of each option there is a service call . for this I am using a library which is as follows:
compile 'info.hoang8f:android-segmented:1.0.6'
As I told you I have a list of students so in list I have the following case;
Case 1: If the teacher review the attendance the of last day , I fetch a list of students that are either absent or present. So on this , the segmented control that I am using in list fires the onCheckChangedlistener which run the code which is also performing some task , where as I only want to perform the task only when user change the listener. In short listener should not fire when I change any check pragmatically. and Also it fires the onCheckChangedListener multiple times. which creates problem for me :
So I have couple of question:
1) what can be used in android in place of the Segmented control library as I stated above.
2) what is possible workaround for this problem. I tried using flag but I am working in getview of listview.
this is a good library in response to what you achieve. I have used this and found the similar problem. So hence by putting flag , and making a raw check over it does a work for me . But As you are using list, i think you can not achieve what you are looking for.
I will suggest to use the radio button to get the check or you can make your custom control like this and see this library

Android - Should I use a Handler in that situation?

I'm learning Android, and I'm building an activity that will receive users that are using different devices, and I would like to show all these users nicknames together, in a Listview.
So, I was thinking about how could I do that, and had the idea to use a Handler to update my listview every second, and to display the list of users in real time, for all users that are connected to the activity.
I created a Database using Phpmyadmin, and I'm using Json to print all the informations from the database, so I can use Java to get all the data.
My questions are:
Is there any better way of doing what I'm trying to do?
Will my App crash?
Does the handlers have a huge effect on the users battery?
Thank you.
Yes there is another way you can do it. You can look for RecyclerView . And notifyDataSetChanged() method. Also you can use notifyItemRemoved()bla bla ..
In RecyclerView you have an adapter and you give list of items to that adapter. When your item list changed , if you notify it to your adapter your recyclerview should change.
Android Recyclerview : RecyclerView
Also you can follow guides like in this blog : Creating lists with recyclerview

I'm building a dynamic form in Android using RecyclerView and I need to save the state of the form

First of all I'm not native english speaker so I'm sorry if there is any grammar or spilling mistakes but I'm really trying to do my best here.
OK, I'm developing an application in android system for e-commerce, the user can add any product to sell, but as you know there are many filters questions.
For example if the user want to sell a car he has to select the model and the company of the car, but if he want to sell a mobile then the questions changed to select the storage for example and the model and the company questions will be deleted, so the questions here are dynamic.
I already developed the form using RecyclerView and its look great.
figure1
figure2
figure3
The problem that when I used
notifydatasetchanged
The form data get mixed!
I solved this problem using
notifyItemRangeRemoved and notifyItemRangeInserted
So it's working great, but I still confused why this is happened?
The second problem that is the dynamic form is a fragment called "DynamicFormBuilderFragment" and sometimes I have to replace it with another fragment like "Google Maps fragment" for example the problem that when I call back the "DynamicFormBuilderFragment" all my data is Lost and the form get duplicated!
Like this
figure4
I want to put some codes but really I don't know what to put because they all are too long and complex, but for start I think this problem because I made the RecyclerView.Adapter not static, I will try for now to made it static and see what happens, but I still need your help please.
Thanks for reading and I wish to you a great day and happy coding.
Update
this is my java files and classes
AddAds.class extends ActionBarActivity
DynamicFormBuilderFragment.class extends Fragment // is contain RecyclerView + Adapter + LoadToAdapter + questions classes and it's where dynamic form get build.
GoogleMaps.class extends Fragment
SLOVED!
I Replaced the RecyclerView with the ListView and it's working great now so thanks #greenapps.
I think it's a bug so I will reported to Google.

Search implementation Questions on android

I am a newbie here and hope you can help me out to clear some of my understanding (or misunderstanding). I have been reading for days and trying out based on Android guide to implement search function but it really didn't work out well. Perhaps I've got the whole concept wrong so do help me out here if you can be kind.
I have an app which data is from remote xml and I want to implement search function. I have 1 main activity (under my SRC -> my package)that list all the data retrieved from the xml. My questions:
1) In the guide, it saws we must define a .Searchable Activity. I supposed this is to handle the search query and store the result? If this is a case, do I need to create a new activity or I can use my main activity as .Searchable Activity?
2) After the above item is done, I supposed I need to create an activity to receive the result and do something. I take it as I need to create "public class SearchableActivity extends ListActivity" in the activity I define in item 1? The problem is that I don't know what to do with this as the guide are incomplete.
3) Presumingly I did it successfully and upon search, there are some results return, how do I show (or filter to show the search result) in my main activity? I do not want to have another UI to show the result as I prefer it just get filter to show the matched records
4) Lastly, since my data is from remote xml, do I need to "search" based on remote xml or it will search based on cache info on the app?
I am sorry for being so ignorant but I am totally new here and hope you can help me out. So far I have no problem, getting the dialog shown on my action bar. The only problem is that the search didn't work at all :( upon search, there is just nothing happened.
Thanks very much in advance. Cheers!
For searching in a list you can use built-in functions of android, but if you want a good result you can use your own function. The simplest function is:
for(int i = 0 ; i < arraylisy.size() ; i++)
{
if(arraylist.get(i) == youobject)
return true;
}
return false;
but there are better functions like binary search that you can use.
Now, how to create search activity?
First: create XML file an add a listview in it.
Second: create java file and link xml file to it.
Now use above code to search an object on your list. If in every search you find several items then add them to new listview.
Now create an adapter and send your search list to that.
Finally set your adapter to listview.

Categories

Resources