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.
Related
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.
What I want to do:
Display a list of items
The actual data is coming from an external source and can change
Each item can have several "columns". E.g. "type", "content", "date"
The list should e.g. be sortable by "type" and "date".
I thought I might be able to get the desired functionality using a ListView (but maybe also this is a very bad choice?). I have read some stuff about them, but still have some questions. I know that the ListView Displays data which is managed by an Adapter. If I have an ArrayAdapter, does the Listview always display the items in the order of the underlying Array in the Adapter? So to implement sorting, I would have to somehow change the Array in the Adapter? I have read, that you can use SimpleAdapter to have several properties in a row, but the data SimpleAdapter uses seems to be static. How can I achieve that?
Thanks in advance!
Yes, ListView will represent depending on how your ArrayList or Vector([]) is sorted. Now, if you want to have several properties for each row, I would recommend that you create an Object of your choice, for instance: class Person which takes name, age, address etc etc.
Then create a list which takes Person objects. Then in your getView of your ArrayAdapter that you will override, you can call Person person = getItem(position); and now you have a hold of that object in that specific row (position), and can do whatever you want with that Person object.
Create your own adapter, and in getView() return view that matches your position. For instance if you got data like this:
id name
0 ccc
1 bbb
2 aaa
then when you sort by id and listview wants row at position 1, you return 1 bbb. But if you sort by name ascending, and list wants row #2, then you return 0 ccc. Of course you need to maintain the "mapping", but that's rather trivial.
I have two methods which read the same data from database, the first returns Cursor and the second returns List of objects.Now I show my items in activity using SimpleCursorAdapter and the first method, byt I can also use the second method and appropriate adapter.
Which of these two ways is beter to use and in the second way which adapter I should use?
P.S sorry for poor english
Definitely go with SimpleCursorAdapter. If possible, always use Cursor if your data comes from database, you save memory by not creating List of objects. Creating objects in Java is expensive with regards to time and memory consumption and you have to bear in mind you are on mobile platform with limited resources. If you are using List of objects for your ListView than use custom adapter extending from ArrayAdapter.
It's not always straightforward to use Cursor although your data comes from database. Let's say you store places in the database defined by its name and location and you want to display them in a ListView sorted by distance from current location. It makes it difficult to execute a query which returns sorted results unless you don't store relative distance in additional column. But you can get Cursor convert it to List of objects and sort this collection before sending it to your ListView.
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.
Can someone point me to right direction, how to create an adapter for AutoCompleteTextView, which would be getting data from SQLite DB, using data, which user entered into the text field? I want to use the user-entered data to filter suggestions for autocompletion. I imagine that adapter should always take user-entered data as soon as changes appears and use it for fetching suggestions on-the-fly. Is that possible? So far I've seen many tutorials for autocompletion where static String arrays were used, but never seen them build dynamically. Is it possible to do it automatically or I need always fetch String array myself and pass as ArrayList to adapter on every AutoCompleteTextView change?
You might be looking for CursorAdapter. Use it just like an ArrayAdapter, but instead of feeding it with an ArrayList, provide a database Cursor. Google for CursorAdapter and you should get a lot more example codes.