Android AutoCompleteTextView suggestion if nothing is found - android

I am making a point of sale app for Android, and I have a AutoCompleteTextView loaded in with the database of items that they can buy, but I also want the AutoCompleteTextView to double as a way to add custom items/notes into the order.
So for example, the customer is ordering Steak, but doesnt want any seasoning. My database includes options for 'No Onions' 'No Mushrooms', etc, but not 'No Seasoning', and if the waiter types in 'No Seasoning' into the AutoCompleteTextView, I want a suggestion 'Add as Note' to pop up.
Is there a way to add this suggestion when no other suggestions are found, either by making it always on the dropdown list (making it not able to be filtered out), or by detecting when no items are on the dropdown list and adding it in then?

I think this can be done using MatrixCursor.
When you retrieve the cursor from your database, check if its empty with Cursor.moveToFirst(). Then either fill the MatrixCursor with your database cursor or a single "Add as Note" item if empty.
I use this to combine two database Cursor in one for suggestions here (method getUrlSuggestions(), the last one). Note that this example is not very clean, as all values are casted to String before insertion in the MatrixCursor. This is not necessary as the addRow() method take an array of Object.

Related

Search in sqlite database by criterias selected with checkbox

I have in my app a database with two tables : country and rights. Long story short, the db tells me whether a right (there is 10 rights in total) is legal or not in a specific country.
Now, I want the user to be able to search in my db by criterias. I have a layout with checkbox. If the user check a box, it mean he want to see every country in where the right is legal. For exemple, if he check the box "criteria1" and "criteria6", the user want the list of every country where criteria1 and criteria6 are legal, but we don't care wether the other rights are legal or not.
I asigned values to the checkboxs (1 if legal, 0 if illegal, just like in my db) and passes all of them to the activity who display the result of the search.
My problem is, I can't figure out how to search in my database. I need to only get the country where where the selected criters are equal to 1, but I don't know how to formulate my sql request (since I never know which criterias are going to be checked or not). My request need to only be about the criterias who has the value 1.
I had the idea of sending all my values to a function (witch returns a cursor) where I excecute a select statement if the value is equal to one, but I don't know how I could join all the result of my selects in a cursor. I also thought about using "CASE WHEN..." but it doesn't seem to work.
Does anyone have a clue on how I could deal with my search ?
If you need precisions on my problem, please ask.
This guy here (https://www.youtube.com/watch?v=NGRV2qY9ZiU&list=PL200JxfhYIgCrrpH4rCz-uNfBTb5sng1e) has the right idea.
The clip may be a bit slow but it does exactly what you want.
He creates a custom string based on if checkbox is checked and removes it from the string if unchecked.
To get what you want, you need to do a couple of things.
First, create a table with countries as rows, and rights as columns. Add 1 for right is present in country and 0 if not. Get this into an sqlite database (eg import via csv in DB browser for SQLite, free software; don't forget to create the android_metadata table in the sqlite database - search online for this). Import the database in the app (there is plenty of documentation for this online).
Second, change the text inputed in the if/else checkbox part of the script (he writes fruit names, you write for ex. "right1 = 1", or the exact query the checkbox should do on the column right1).
You also need to pay attention to the selection.add and selection.remove (know that selection is an array list which will store all your criteria for search by column).
Third, you need to change the content of his finalSelection (View view).
Delete all he has written and just create two strings:
String final1 = android.text.TextUtils.join(" or ", selection);
String final2 = "select country from table where " + final1;
The string final2 is your key for a cursor with a rawQuery. Just create a cursor in the database and pass the key to it. This can be done easily.
PS the method android.text.TextUtils.join() is amazing :)
You can place the operator of choice there.
If you need more than one operator (and, or etc), you can create different ArrayLists which you fill in the if/else checkbox is filled and join later in the finalSelection.
Oh, btw, if you have too many checkboxes, you will get a warning in the XML file (layout has more than 80 views is bad for performance).
In order to get around that, you need to get to know grid views a bit better. After reading a few tutorials on the basic use of GridViews, a good start for checkboxes inside them is here.
It may seem like a lot, but you need to learn to use holders to get information out of the getView of the modified BaseAdapter.
If you want to understand it better, follow the arrPath.
It is a String[] filled with all the paths of images found inside the cursor (string values from the dataColumnIndex, which contains paths of images).
Within the onClick() listener of the Button, from the arrPath he extracts only the rows of the cursor that were selected by checkbox click (thumbnailsselection[i] is a boolean - with a value TRUE/FALSE for each row in the cursor).
The selected paths are placed in the selectImages String, separated by OR.

Usin SimpleCursorAdapter confusion

I have a ContentProvider implementation in my app which works fine. I have a table called elements where the user can store a bunch of information.
What I am doing is when the user opens the app, I pull this data out of the database, process it using the display options set by the user (Like change the string formats, time formats, date formats, number decimals etc), and then put it in ListFragment which using my own implementation of ArrayAdapter. User can of course change the preferences in the middle of the session, where I reload the data and reformat it and present it to the user. The user can click on an item in the list, and see more details of that item. I accomplish this by overriding ListFragment.onListItemClick().
I have been reading about SimpleCursorAdapter. I am confused if the use of this would be more correct than using an ArrayAdapter for what I am doing. I am confused because I am not directly mapping the database data to the view. So should I be using a SimpleCursorAdapter? Also, the _ID column seems to be a requirement. I don't want to rename my table at this point. After a few articles and tutorials, I am not sure what to do. So any suggestions are appreciated.

Select multiple object type with auto complete

I am looking for some help to find a powerfull way to allow selection of different List items.
My case is that i have for exemple a List profiles, List teams ... and i'd like to have an autocomplete input that will show, for exemple if i type Al, all Teams and Profiles objects having there member variable name begining by Al.
The result would be that i could get from the activity, on submit click performed, a List & a List containing all the objects who have been selected through the autocomplete form.
Also i'd like that the list offered to the user that match the chars he typed show the name and a picture (facebook like tag selection).
Obviously i am not asking for some code but at least some guidelines from experienced Android devs who know what to do and not to do to create this kind of thing.
Thanks
Loader are one of the best way to filter list. You init a loader which take the String constraint used for filter, the each time the user type, you update the constraint and restart your Loader.
If I suppose that all your object are cached in a SQLite database you can use a CursorAdapter, Cursor and CursorLoader.
You create the needed CursorLoader by filter the query with the content of the EditText.
If you're not familiar with CursorLoader there is the AsyncTaskLoader, with this you won't have the need of DB and to code a provider which can accept raw query. Your object in ListA, ListB, etc must inherits of common class (hum... DataThing maybe :-)), you concatenate the objects in a list then you can filter the list : What is the best way to filter a Java Collection?
That's for the data filtering. Now in order to display the data the way you want, you can display of list below the EditText field or create a custom component if you want a more advanced look.

Spinner that returns multiple value on selected?

is it possible for a Spinner to return multiple values or class object on selected?
For example I have a Spinner of Laptop models. When selected I want it to return LaptopSpecs object that contains size, weight, processor, etc. Then use it to display the information in the view below it.
Thanks
Sorry, there is no multi-select Spinner. You are welcome to use a multi-select list AlertDialog to allow the user to make their selection(s), but you will need to decide for yourself how you want to render those selection(s) when the dialog is not on the screen.
It depends on how you are populating your spinner.
If you are pulling the data from a database in a cursor, what you are trying to do is easy.
As a matter of fact, using a database, there's a couple ways you can do it:
1) You simply pull all the necessary data you need to create the object into your cursor (kinda heavy load on the front end), and when a selection is made (fromthe single bit of data displayed in the spinner), you use the cursor position reference in the onItemSlected method to pull the related data from the cursor and pack it into your object.
2) You pull only the piece of data to display in the spinner and when a selection is made, use the database row id in the onItemSelected method to fetch the rest of the data for your object from the database.

If no search result, then add item

In my android app I have a search screen.
The content is stored in a database, and a listview is populated via a ListAdapter.
This all works correct.
But I want the following functionality.
When a user searches, but gets no result, I would like to give him/her the option to add the search item.
for example:
list in listadapater is: [monkey, donkey, mouse]
when the user searches for: lion, he will see no results.
But I want the option to add lion to the database list.
Hopefully someone can get me in the right direction.
Thanks in advance,
Goldhorn
You simply add the new item into the database. Then you requery the database for a new cursor which you then pass to the adapter.

Categories

Resources