SQLite Complex query and handling results - android

Good evening ( for me ), I'm Emanuele a young android developer.
I want to share with you my problem because I'm not finding a good solution.
I've 3 tables (in reality 5)
posts
categories
posts_categories
to create the many-to-many relation between posts and categories
I've created a ListFragment to show all my posts with their categories.
So I've created this query ( I don't write all the code :) )
SELECT posts._id, posts.post_title, categories.category_name
FROM posts
LEFT OUTER JOIN posts_categories
ON posts_categories.post_id=posts.post_id
LEFT OUTER JOIN categories
ON posts_categories.category_id=categories.category_id;
So if I have 2 post with 2 category each that query will return 4 records. Am I wrong?
So in my Cursor I have 4 record but what I really need is to collect datas from the cursor and display only what I really need.
In this case I cannot use a CursorAdapter to display my data because it will insert 4 item in the ListFragment.
And I don't want to load only post and than for each post load categories because if I have 100 posts I will do 1 query to select all posts and 100 query to select categories.
What can I do? I need an expert advice! Which is the best way to handle this situation?
Have you ever faced this problem?
Thank you very much. Emanuele Ricci.

I suggest that you extend ArrayAdapter and provide your own ArrayList with your own Object for each result in your list. The ArrayAdapter provides a function called getView() which should be overridden and customized to inflate and display what you need. That should be all you need.

Did you tried CursorAdapter with Filterable. Here is [link] (http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/AutoComplete4.html). Hope this help.

Related

listview with linked tables (many to many relationship)

I have three tables:
Employees, Courses and Attendance (which links employees and courses).
I'm looking to get a listview (or other similar view), with a line for each employee and a 'column' for each course, with the date (from the link table) where attended. I'm struggling to work out how this can be done:
I can have run a query that gets a row for each employee-course combination, but that doesn't tie in to a row in the view for each employee (i.e. the query will return multiple rows for each listview row).
I can get the listview for each employee, but am not seeing how to then pull in the data for courses in via the linked table.
with no real direction I don't have code to post yet.
This can't be that uncommon a problem, but I'm not finding any examples: maybe I'm not thinking of the right phrases
What's best practice to achieve this sort of display?
For me best display should be ExpandedListview. Let say you have Employee that attended 3 courses. In ExpandableListview you for your GroupView set Employee data and for child data just set the course that it attended with information that you have in your table. Like in this image :

android expandable list from online database

I'm designing an android app that retrieve universities from online database I have to use ExpandableListView to represent them. I have 700 universities so I need to represent them by category . My categories are by letters A,B,C,D etc.
>L
London Metropolitan University
Lea Valley College
>M
Manchester College
Manchester University
I have problem populating the child elements in each category. Because I have a lot of data I think the best way is when each category is pressed to create sql query to get all universities starting with 'A' for example BUT all ExpandableListView example I found use arraylists or hashmaps with a few elements.
Thanks in advance for your time.
You could probably use CursorTreeAdapter.
See getChildrendCursor method:
If you want to asynchronously query a provider to prevent blocking the UI, it is possible to return null and at a later time call setChildrenCursor(int, Cursor).

android ListView subitem database

I'm making an application with a database of courses (of school) that the user is participating in. I want to display those courses in a list (im using listview) and use also the subitem (extra information of that course) of the listitems. I found a tutorial (http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/) that displays a list like I want but it uses a class to store the data in instead of a database. So the tutorial uses an arraylist of objects of that class. It would be a detour though if i had to put my information from my database into a class to put it in the list.
Does anyone have a clue how to solve this?
Thanks!
I found a beter way. By using a simple adapter and putting the values of the cursor in a hashmap in a arraylist. Found the idea from this site: http://eureka.ykyuen.info/2010/01/03/android-simple-listview-using-simpleadapter/
Here is a perfect tutorial for what you need. It shows you how to create a simple SQLite database and then populate a listview using the database.
SQLite Database creation and Listview Population Tutorial

Android Notepad Tutorial extension: getting more out of the (SQL) NotesDatabase

Confused beginner here.
I'm extending the functionality of the android notepad tutorial program. I can successfully get the data from the sql to display in a list view and I can use the findNote function to get an individual note.
What I want to be able to do is extract an individual note AND all following notes in the table. I'd be happy with some way to iterate through the remaining notes (a puzzle for me because the rowIds are not sequential) but would also settle for designing a query that... I don't know, returns a String[] with the item with the matching id at position 0 and all subsequent items later in the array.
I'm sure there are a thousand ways to do this, I'm willing to take almost any of them. Please let me know if I need to clarify further.
Just looking at the NotesDbAdapter class in that tutorial, there doesn't appear to be any timestamp captured with the notes. You would need to extend the table structure to include this and update the createNote and updateNote methods to set that accordingly. Then you can write your SQL based upon that timestamp.
John

Retrieving Database Information and Showing in ListView with Android

I am working on an Android app that will allow the user to see restaurants in the city I live in. I am storing each restaurants information (name, address, telephone, hours, category, website) in an SQLite database.
What I am trying to do is to create a SortByAlpha activity that will list the restaurants by name in alphetically-descending order.
I understand that I should be using a Cursor to do this but I can't find a half decent tutorial, all of the "tutorials" I find are a bunch of code with minimal explanation. How can I do this / Where can I find a good tutorial?
Use SimpleCursorAdapter, which bridges between Cursor and Adapter. Here is an example how to use it: http://developer.android.com/guide/topics/ui/binding.html
I would advise creating a "Restaurant" class that contains all the fields you will be listing. Then make your SQL call and specify "ORDER BY Name". Then create an ArrayList that can be fed to your custom list adapter! Also, if they were to somehow get out of order, just implement the "Comparable" interface for the Restaurant class and compare based on "Name" and then you can call Collections.sort(restaurantlist); to sort them in alphabetical order. I personally think ORDER BY is the easier way to go!

Categories

Resources