Populate ExpandableListView with data from SQLite in Android Studio - android

I'm developing android app and want to fetch some result from sqlite db inside the app and display it in "ExpandableListView". here is what i want. The app will provide some list of disease(problem) for the user to select and when a specific disease is selected, a query will get executed based on the selected disease and fetch 'TradeName' from db that have the same 'ActiveIngredients'
The ActiveIngredient will be the PARENT list and all tradeNames that contain the active ingredient will be the CHILD list.
Previously i have used to normal ListView to populate the results but when the data is populated redundant ActiveIngredients will be listed.
The expected output should look like this.

Related

How would I update a single item in an ArrayList of items with data from Room? (android Kotlin)

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

How to sort groups based upon recent message in firebase?

Goal: show the list of groups based upon the recent message and when a new message comes, update the chat group list.
Firebase Structure:
Left one structure represents user model: Basically under student key, there is a list of the student then its details along with group ids in which he/she participated.
Right one structure represents the group detail model: Under chat_group key, there is a list of groups with details and also recent message timing.
Solution Tried:
Fetch the group id from the student using the onChildAdded method of firebase DB one by one then fetch its group details using the addListenerForSingleValueEvent method, then store in the list and every time sort it using sort method of array list and then call notifyDataSetChanged method of recyclerview adapter.
Problem with this approach: Too time-consuming and as the number of groups increases processing time also increases.
I think the best solution is to add a new key to the group details model for the students signed up , like that
SignedStudents : "student1 , student2 , student3"
Then you will read data from chat_group directly orderedByChild("recent_message_timeStamp") , then you have to filter it locally to get the groups only related to the specific student to add it to your list like that
String students = model.getStudents();
if (students.contains("student1")) addGroupToYourList();

How to set spinner adapter from firebase query

How can I get a firebase query result into a spinner adapter in android. The image above is a sample structure of my database. I want only those that available to be shown in the spinner, and only the the number is to be shown, so in this example 0954555 will be shown, but in the query i have to get only those whose available is yes. Thank you :)

Display SQLite query result in Listview in Android

In my Android app I have an activity with a listview that displays about 4000 items that are stored
locally in a SQLite Database. If I make an edit to an item, how can I get only this change in the listview,
without having to refresh all the item list (which means a new query for 4000 results)? This query slows down the performance.
I would like something like NSFetchedResultsController of ios.
Strategy should be -
As you are editing the contact, if the update is successful you just fetch the latest info from db for this specific contact only.
Update the edited contact object in your adapter's source List/Array's specific position.
Invoke your adapter.notifyDataSetChanged().
you must have an id for each contact in your SQL lite database.
when you edit a contact update that specific record in your database on basis of the id.
if update is successfull means the information you sent to database is stored successfully .
now you can use the same informtion to update your ArrayList/HashMap whatever you are using to populate your listview.
Ex:- suppose you edited 3rd index contact in your listview on successfull update you add like yourarraylist.add(3,contact);
and the fire notifydatasetChanged.
Try these steps if possible:
Try to fetch the data from db, but do it in different thread which won't effect the main UIThread.
Then you can call the adapter.notifyDataSetChanged() on adapter object. It will do the job i hope :).

Expandable ListView and cursor adapter

I have a ListView which uses cursor adapter to show the records from database. When a new records is inserted in database ,it works great and shows that entry on top of ListView on requery.
Now I am facing problem when User scroll down the List, background thread call web service and brings old data. In this case when it does requery, old data is also getting appended on top of list which should append old data at the end of list.
What should i need to do differently, to add old data at the end of List rather than top ? do i need to change method of insertion when I am getting this old data ?
I think you need to store a date field in your bd table and make an ORDER BY in the query....

Categories

Resources