Display two blended cursors in android - android

I have what I assumed was a straight forward issue, but after a thorough search I can find no solution:
I have two data sources with two distinct data types: apples and oranges. The only column in common is "datePicked". I want to query the separate databases and then display both apples and oranges in the same list ordered and grouped by "datePicked".
I see some suggestions to use MergeAdapter. However, unless I am missing something, MergeAdapter simply concatenates two Adapters, it doesn't really merge them. Likewise, I have seen suggestions to use MatrixCursor or MergeCursor to create a single unified cursor and then creating an adapter for that. Neither seem a good solution. MergeCursor appears only to concatenate the cursors, while MatrixCursor appears to require that I iterate through both datasets start to finish building a cursor row by row. Even if these were acceptable solutions, I still have the problem that different schema require different layout on the screen. How would I inflate different layout based on underlying data type?
So I am stumped, and would appreciate any help. Is there a true merge adapter that will interleave the data rather than just concatenate it? If not, is there a way to create an Adapter that conditionally maps and inflates a layout based on some business logic done to the specific row being pointed at by the cursor?
Sorry if I am missing something obvious.
Thanks,

To answer my own question, it appears that building a Matrix cursor with a merge-sorted-list type algorithm stepping through both cursors, and then conditionally inflating a row layout by overriding the newView() method with an if statement is the only workable solution. I will post the code for anyone who is interested once I've worked out the details.

Related

What is the purpose of Cursor Adapter?

Now, I used ListView and ArrayAdapter to show data from sqlite. My old implementation is retrieve from db and set to arrayAdapter. Then set adapter in listview.
But now considering to move to efficient adapter. What if sqlite have thousands of records ? I knew I have to retrieve first 20 records then retrieve next items based on scroll. I wonder Can I get that implementation by using cursor adapter ? Or May I know better solution for that situation.
When having thousands of records in a Database/Server response or whatever you are fetching the information from, the best practice is to do use "Lazy Loading" technique, basically what it does is, showing chunks of data sets to the user for example, 50 elements at the time, so, there's no need to load 950 elements that might not even be used, this technique improves considerably the application response and your memory too, now how you implement it is really up to you, there's a BaseAdapter class to do it on your own, or a good implementation of Cursor/Array adapters might do the trick as well.
Is important to mention that cursors have one advantage:
Cursor lets Android manage resources more efficiently by retrieving
and releasing row and column values on demand.
Hope it helps!
Regards!
First of all, the main reason I use CursorAdapter is that the content will automatically change if the data in sqlite changes. I don't need to consider about refresh UI if something changes below.
Secondly, both the query of sqlite and listview already make some optimization you want. The sqlite will not give all the data on the moment of executing query by default. It uses a slide window to gradually give the query result. And listview also does not draw all the item at once, it draws the items as the user scrolls down.
Hope this helps, though doesn't exactly answer your question
you can involve a thread which retrieve data from database and put into a Map or List. then when you need data like per 20 records when you scroll read from Map. it may help you.
just make another layer between database and view.

The right coding approach for ListView with Images and Text?

The question arises from the way I have created a file explorer functionality for an application (my first).
Can some of you please help me know which of the following below mentioned approaches is better vis-à-vis performance.
Case I - There is an approach which advises to override the getView() method. Like the one given here.
Case II - I used a different one where I have a custom method (not getView) in my adapter class with
HashMap for each row of the listview.
ArrayList> which holds each of the HashMaps from (a)
SimpleAdapter(Context , List> , int , String[] , int[])
(I must say I have to do extra iterations to sort the list folder/file wise and then alphabetically within folders and files. I went the long way sorting, did not use comparator)
My code is close to as given here
Case III* OR, is there any thing better than the above two approaches?
Thanks.
Personally I would create my own adapter and override the getView method. The reason is that you will get much better flexibility and control over what you can do with the class. For instance, if you change your data structures that you back the data with it will be easy to handle. You could eventually want to do some sort of algorithm where you rely on caching the contents of a folder for a period of time and doing so will be more difficult with a SimpleAdapter.
A SimpleAdapter saves you a few lines of code that you'd need to write in getView but overall I don't think it's going to really help you that much. There are certain things that you should make sure to do though to make your listview fast, such as using viewholders.
Just extend ArrayAdapter and override getView(). Use the ViewHolder pattern to make it more efficient. It is a lot simpler than what is being proposed in case 2 and I don't think case 2 allows for images.

Index scrolling in android?

I want to display the ListView items in alphabetical order.
I also want to display the items with a separator.
If any one knows can you please help me on this? Thanks in advance.
There are two basic ways for ordering your ListView:
Use Collections.sort() and have your objects implement the Comparator interface.
If you are using a Cursor to load your data (e.g. from a ContentProvider or database), you can use the SQL to order the data returned.
As for adding sections to your ListView, there are also 2 basic ways to do this. Both methods are very well documented in this blog post by Cyril Mottier.

android database value retrieving

I have a database with three rows and columns.I have managed to insert the data into the table.Now i am trying to retrieve the values in a tabular format in my xml view.
How to go about this.
Any help is appreciated.
Thanks.
What you're asking for is a fair amount of work, but saying that, it's relatively straight forward.
The problem is, your request is so vague that people are reluctant to help.
One possible solution (although it might not fit what you need) would be:
1) Create a ListActivity
2) Create a custom SimpleCursorAdapter.
3) Create a custom xml view for each row.
4) override SimpleCursorAdapter's getView() to inflate said xml view
5) Override SimpleCursorAdapter's bindView() method to assign values from the cursor to the row.
The questions in my mind which stopped me from trying to code this for you are:
1) Might you want to change the number of columns dynamically?
2) Might you want more flexibility as to how the column widths are decided upon? etc..
A slightly more complex setup would use TableLayout and TableRow.

Android adapter with section headings: performance problems

I have a custom adapter to display a list of items with section headings. I've looked at Jeff Sharkey's SeparatedListAdapter and CommonsWare's MergeAdapter as examples of how to achieve this, and I now have a solution which works by providing a separate adapter for the content of each section.
This creates a big performance problem, though. In my case, there are potentially thousands of items in the list, each with an associated date, and I want to use the date as section heading for all the items with that date.
So, without section headings, I'd have a single Cursor which returns the items sorted by date. Nice and easy.
With section headings, I'm currently doing this:
One Cursor to select all distinct dates in the dataset
For each distinct date, a separate Cursor to return the items matching that date
Pour the dates (section headings) and separate SimpleCursorAdapters for each date's items, into my custom adapter.
This requires many more database queries and Cursors to be generated than I want, and there's a delay of several seconds before the ListView appears.
I suspect there might be a simpler solution where getView does something clever and detects when the date has changed between successive items, and then sneaks in a new heading by itself, thus requiring just the one Cursor. Can anyone suggest a way of doing this?
I guess the easiest way would be to check within every getView call whether the previous item has a different date, and if so to simply embed the header within the current view.
You can try http://code.google.com/p/android-section-list/, which requires just single cursor behind (returning cursor + section in one object). However it will anyhow have to go through all the elements (once) to calculate the size of resulting list+headers (needed by list adapter) - so it might still be slow.

Categories

Resources