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.
Related
I am fetching a list of dogs from an endpoint. Some of the attributes of the dog, color for example, are identified by id. The id's and colors corresponding to the id were all saved in a database when the app is opened. I am using MVVM architecture and assume that the model is consuming the response data correctly.
In the main screen of the app, I have a recyclerView that will populate with a list of dogs. when I fetch the list of dogs, I am returned the id of the dog's color as part of the dog object. The user isn't going want to see the id of the color, but the actual string value of the color corresponding to that id. I need to query the database with that id to get back the string value of the color to display to the UI.
My question is how would I go about doing this? The recyclerView adapter would be where the UI is updated, and I would need the position of the dog to know what color Id I am fetching, but I can't run that query from the adapter as it would lock up the main thread. I thought to create a function in the view model that would update that particular list item, but I think I would still need the position from the adapter. I'm not really sure how to proceed...
I hope my question isn't too confusing, I had a hard time figuring out how to ask! Thanks for your time!
You want to build proper query in your DAO and "join" values from another table, depending on color_id.
You are not allowed to do any queries at adapter level. Data passed to adapter must contain everything you need.
Joins and mappings are described here: https://developer.android.com/training/data-storage/room/accessing-data
I didn't use parse query adapter, instead I made a custom adapter and passed all the text and images in that but Parse only loads 100 items. Can anyone tell me how to get to the next page of the database without using the parse query adapter?
This is a well known problem, the default limit is 100, you should use (saying that you have a query which is a ParseQuery object), you need to call the query.setLimit(int N) where N represents the number of results that you want returned .
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.
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.
I have 2 tables.
In the first table I have news (for example: 2 fields 'id' and 'text').
In the second table I have images, that attached to news (for example: 'id', 'new_id', 'src').
There are more than one image for one news. How can fill ListView with such data?
this is how I think of it:
first of all, create a DTO (Data transfer object) for each table;
then, create the layout that represents the list's cell.
you'll need to create a custom adapter, and here is how you can do it http://www.vogella.de/articles/AndroidListView/article.html#ownadapter.
and the last thing is to extract your data from the database and pass it to your adapter.
this link http://www.vogella.de/articles/AndroidListView/article.html explains what I said in a clear way and in more details.
hoe this helps.