Android Multiple SimpleCursorAdapter - android

I'm working on a little App which works with a SQLite Database... i'm showing a picture (green, yellow or red) which depends on a priority.
i have 3 priorities (high, medium and low...) and i'm getting those cursors as following:
cursorh = dbAdapter.fetchAllHighPrio();
cursorm = dbAdapter.fetchAllMedPrio();
cursorl = dbAdapter.fetchAllLowPrio();
Then i'm calling some other stuff like this:
startManagingCursor(cursorh);
startManagingCursor(cursorm);
startManagingCursor(cursorl);
String[] from = new String[] { TodoDbAdapter.KEY_SUMMARY };
int[] to = new int[] { R.id.label };
SimpleCursorAdapter noteh = new SimpleCursorAdapter(this,
R.layout.todo_row_high, cursorh, from, to);
SimpleCursorAdapter notem = new SimpleCursorAdapter(this,
R.layout.todo_row_med, cursorm, from, to);
SimpleCursorAdapter notel = new SimpleCursorAdapter(this,
R.layout.todo_row_low, cursorl, from, to);
so then i have all of my stuff prepared to show on my list... but if i use setListAdapter i can only use 1 of them. since i have 3 different layouts with the different cursors, this is really pretty hard to do.
How can i get all those 3 SimpleCursorAdapters to show in my list now?
EDIT: Maybe i wasnt clear enough... All of this data is in only one table... but since i have 3 different layouts (because of the different priority colors) i need to add them seperately... or is there any other way of just saying like if the priority equals 'high' put this image in layout so i only need one SimpleCursorAdapter?

You can create a VIEW that would combine data from all 3 tables + an extra column to tell which table the data came from. Then you query from it and return appropriate row Views in a custom CursorAdapter.

To my knowledge, you would be unable to attach more than one adapter to a ListView. Unfortunately, I think you may need to approach this in a little different way.
You could stick with one adapter and make sure your sorting is correct in your database query.
You could create one adapter and loop through each cursor, adding items to that single adapter (probably not a Cursor Adapter but more like an ArrayAdapter).
You could, I suppose, Use a LinearLayout with 3 ListViews weighted to 1 each and have 3 separately scrolling lists on the screen at one time...
Depending on how you actually want the application to work, there could be several different approaches.

Related

How to display the data in listview from database and CursorAdapter by using search function

i hava listview in my app and i add EditView i need to search in database and CursorAdapter
how can get The result from search?
db = new DatabaseHandler(this);
lv = (ListView) findViewById(R.id.listView1);
Cursor cursor = db.readData();
String[] from = new String[] { DatabaseHandler.KEY_COED ,DatabaseHandler.KEY_Quantity};
int[] to = new int[] {R.id.Code ,R.id.Quantity};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(Show.this, R.layout.view_code, cursor, from, to);
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
This code is just for displaying the data in a listview.
To implement search, I use a custom Adapter. The implementation of the custom adapter really depends on your situation. To start with, is it an adaptive search? Like do you want a TextWatcher on the EditText so that results are filtered as the user types?
Also, do you need to perform DB updates or do you need to have an open Cursor during your interaction with the ListView. If not, you can create a custom ArrayList class and use the search data to filter the result list. If so, then you need a custom Cursor that will re-query the database using your search criteria.
Regardless, here is a reference for Custom ArrayAdapter:
http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter
And custom CursorAdapter:
http://tausiq.wordpress.com/2012/08/22/android-list-view-from-database-with-cursor-adapter/
Also, keep this in mind. Your custom adapter will need methods for updating/clearing your search. And in the "getView" or "newView" methods, you can dynamically skip or ignore elements of your cursor or array to only display the ones you want.
You may also want to build a custom ArrayList object that holds each row of data and then build your search methods and filtering on it.
It might also be useful to add a custom comparator to the ArrayList (which is what I ultimately ended up doing to get the results to display correctly depending on search options, etc.). For example, doing a search for a name, you may want to search the list several times for last, then first, then middle - and then you may want to display the results in a different order (like by last name, where a middle name result should be placed first).

Using MergeCursor and a SimpleCursorAdapter, What's Missing?

No matter what I do, the following throws an error that one of the columns contained in the cursor_counterparty does not exist. When I checked the merge_cursor, I can find the column in there, here's my code, what am I doing wrong?
cursor_invoices = Invoices.getInvoicesCursor(counterparty.getId());
Cursor cursor_counterparty = Counterparties
.getCounterpartyCursor(counterparty.getId());
startManagingCursor(cursor_invoices);
startManagingCursor(cursor_counterparty);
/* Joins cursors akin to doing an SQL join */
MergeCursor merge_cursor = new MergeCursor(new Cursor[] {
cursor_invoices, cursor_counterparty });
merge_cursor.moveToFirst();
int[] listview_columns = new int[] { R.id.textview_invoice_number,
R.id.textview_counterparty_name, R.id.textview_amount,
R.id.textview_account_name, R.id.textview_invoice_date,
R.id.textview_date_paid };
String[] listview_fields = new String[] { App.INVOICENUMBER,
App.COUNTERPARTYNAME, counterparty_amount_field,
App.ACCOUNTNAME, App.INVOICEDATE, App.DATEPAID };
SimpleCursorAdapter cursor_adapter_invoices = new SimpleCursorAdapter(
this, R.layout.listview_invoice_item, merge_cursor,
listview_fields, listview_columns);
The error I get is:
java.lang.IllegalArgumentException: column 'counterparty_name' does not exist
When I debug the App, I can see 'counterparty_name' as a column in one of the cursors in the merge_cursor.
Any help would be great, thanks!
Are you wanting to join the cursors vertically (adding rows) or horizontally (adding columns)?
This is theory, as I haven't peeked at the code, but it makes sense to me...
MergeCursor concatenates cursors vertically (fact), one after another. So for part of the cursor you have one set of columns and for the other you have a different set of columns (supposition).
Your adapter is trying to find a column that doesn't exist in one portion or the other for the row being displayed.
If you changed to a CursorJoiner, which concatenates the columns, I think it would work more like you expect, although how you would line up the rows appropriately I don't know.
A good explanation here
EDIT
I see you use the same ID to fetch each cursor, so my concern about lining them up is irrelevant. I think you do want CursorJoiner rather than MergeCursor.

Android question: When using SimpleCursorAdapter, how can I format one of the columns

I'm trying to create a list that is mapped to a database query, however, one of the fields in the database is a timestamp which when displayed should be displayed as a date like "Wednesday, March 2" instead of the actual value in the database which is something like 1299517239487...
I can solve this by rephrasing the query or by decorating the cursor after I do the query, but I would much rather have the simple cursor adapter display this column in that specific way.
Does anyone have any idea on how to do it?
some code:
// the desired columns to be bound
String[] columns = new String[] { DBHelper.COL_START_TIME, DBHelper.COL_AMOUNT};
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.dayMonthDate, R.id.amount};
// create the adapter using the cursor pointing to the desired data as well as the layout information
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.single_activity,cursor, columns, to);
I would like to be able to add some kind of post processor to the adapter in order to fix that.
Thanks!
You can use the setViewBinder() method of SimpleCursorAdapter to have a ViewBinder manually set the values for the views but that will get called for every column.

Cursor Adapter with multi select

I've written an APP the uses has a small SQL lite DB and using a cursor adapter I can retrieve the records and populate a list view with them. from there I can get the Id of a selected item and delete it from the DB which works great. the issue I have is that as the DB grows deleting one row at a time would be slow and frustrating so I wanted to know if there was any way to allow multiple selections possibly with check boxes or by even changing the text color of the items selected so that I can retreiving their relative ID's.
I have read some posts that talk about custom cursor adapters but I am not sure how to adapt them to my code. I have posted my code below.
private void fillData() {
Cursor c = mDbHelper.fetchAllNotes();
startManagingCursor(c);
String[] from = new String[] {DBHelper.KEY_FIELD0,
DBHelper.KEY_FIELD1,
DBHelper.KEY_FIELD2,
DBHelper.KEY_FIELD3,
DBHelper.KEY_FIELD4};
int[] to = new int[] {R.id.text,R.id.text2,R.id.text3,R.id.text4,R.id.text5};
SimpleCursorAdapter dblist = new SimpleCursorAdapter(this, R.layout.row, c, from, to);
setListAdapter(dblist);
}
Thanks.
I have not done that myself but I did find this tutorial that explains how to add check boxes to a list:
http://www.androidpeople.com/android-listview-multiple-choice-example/
You can use that example to allow multipe selection of items in a list control. Instead of using the array array adaptor as in this example instead use your SimplecursorAdapter. The only gotcha you will have to work out is that the example uses android.R.layout.simple_list_item_multiple_choice for the list entries and you need to replace that with your layout since you obviously want a few more than one text field.
I'm not at home so I cannot try it out but I did find this:
http://www.mail-archive.com/android-developers#googlegroups.com/msg21920.html
which seems to refer to making your own multiple_choice layout for a list item.
After the user has made their choices you can remove multiple records at one time by running SQL in your database adaptor:
DELETE FROM test WHERE _id IN (1, 3, 6, 7)
where the list of numbers are the selected id values.

Computation on db data then list them using either SimpleCursorAdapter or ArrayAdapter

I juststarted programming in android a few weeks ago, so I am not entirely sure how to deal with listing values. Please help me out!
I have some questions regarding displaying data sets from db in a list. Currently I have a cursor returned by my db points to a list of rows and I want display 2 columns values in a single row of the list. The row xml looks like this:
<TextView android:id="#+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/text2"
android:textSize="14sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
so I was thinking using simplecursoradapter which supposedly makes my life easier by displaying the data in a list. However that is only true if I want to display the raw data.
For the purpose of my program I need to do some computations on the raw data sets, then display them. I am not sure how to do that using SimpleCursorAdapter. Here's how I display the raw data:
String[] from = new String[]{BtDbAdapter.KEY_EX_TYPE,BtDbAdapter.KEY_EX_TIMESTAMP};
int[] to = new int[]{R.id.text1, R.id.text2};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter records =
new SimpleCursorAdapter(this, R.layout.exset_row, mExsetCursor, from, to);
setListAdapter(records);
Is there a way to do computation on the data in those rows before I bind it with the SimpleCursorAdapter? I was trying to use an alternative way of doing this by using arraylist and arrayadapter, but that way I dont know to how achieve displaying 2 items in a single row.
This is my code for using arrayadapter which only display 1 text in a row instead of 2 textviews in a row:
//fill in the array
timestamp_arr = new ArrayList<String>();
type_arr = new ArrayList<String>();
fillRecord();
Log.d(TAG,"setting now in recordlist");
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,timestamp_arr));
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item2,type_arr));
It's very obvious that it only displays one textview in a row because I set the second arrayadapter overwrites the first one! I was trying to use R.id.text1 and R.id.text2 for them, but it gave me some errors saying
04-23 01:40:58.658: ERROR/AndroidRuntime(3309): android.content.res.Resources$NotFoundException: Resource ID #0x7f070008 type #0x12 is not valid
I believe the second method can achieve this, but I'm not sure how do deal with the layout problems, so if you any suggestions, please post them out. Thank you!!
SimpleCursorAdapter is just what it is, errr simple. More elaborate requirements require different adapters. In your case, switch to ResourceCursorAdapter and overwrite bindView(). Call view.findViewById(R.id.text1), fetch data from the cursor, do your modifications and set the text to the view.
I would avoid switching to ArrayAdapter in your case. E.g. you can easily refresh a cursor adapter, it will consume less resources and scale better.

Categories

Resources