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.
Related
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).
*Hi I am new to android development and I am no where a professional as you'll soon find out, anyways here's the problem I am having:
I have two databases, namely
1->DatabaseClass and
2->TransactionDatabase
The database DatabaseClass has an entry for bank_name field and account_number(and others, but it doesn't matter at this point). The TransactionDatabase also has a field for bank_name and account_number.
A spinner item in transaction side holds the list of all bank_names, these names are accessed from the DatabaseClass, by using a cursor.The items selected from the spinner are stored in the TransactionDatabase.
Now the user enters the account number for that selected bank in TransactionDatabase. If that bank's account number matches the account number added in the database 'DatabaseClass', all the fields contained in that "particular" row which has that "particular" account number should be picked up from both the tables and should be inserted in different strings.
How can I compare the account_numbers ?, I collected the text contained in the edit text field named 'account number' in the transaction side.Also a bank name from spinner is selected. Now how can I get the account number from the 'DatabaseClass' corresponding to that bank name, for comparison?
I used this to set up the bank name in spinner :*
SimpleCursorAdapter myAdapter;
Cursor cursor = myDatabase.getData();
cursor.moveToFirst();
myAdapter = new SimpleCursorAdapter(AddTransaction.this,
R.layout.spinnerlayout, cursor,
new String[] { DatabaseClass.KEY_BANK_NAME_ID, },
new int[] { R.id.bankName });
mySpinner.setAdapter(myAdapter);
So far I am able to get the position of the item selected from spinner through the database DatabaseClass, please help me I am very confused.
int item = mySpinner.getSelectedItemPosition();
Cursor cursor = myDatabase.getData();
cursor.moveToPosition(item);
Your options:
Combine the two databases, use separate tables inside one database
and join the tables on the foreign_key. (Preferred)
Subclass cursor adapter to take multiple cursors.
Create a custom class that implements cursor and stores references to both and create a custom helper class that access both tables to populate it.
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.
Basically I want a list view that has all the data from a database but the first item in the view always being "add new session" or add new something. That takes the user to the entry form. The thing is my lists use a SimpleCursorAdapter that is set from a cursor like
myCursor = getSessions();
which would fill out myCursor with all the sessions from the database, and I would set the list adapter to:
myCursor = getSessions();
theSessions= new SimpleCursorAdapter(this.getListView().getContext(),
R.layout.session_row, myCursor, columns, to);
setListAdapter(theSessions);
This is fine and dandy, but I don't know how to make that first element not part of the cursor and its own "add new..." button essentially. Is there any way to do this or am I going about this wrong?
Example list would be:
"Add new customer..."
"Bob"
"Matt"
"Mike"
Where the names are from the database table of customers, and the add new customer is just part of that list that when clicked launches a new activity to add a new customer. The list view specifically I want this for is in another layout that has other information above the actual list of items.
Tried the header view, but not sure if its selectable this way (cannot set the true using this constructor), and its hard to tell one can even touch this, it looks just like text anyway to add borders to my list view?
EDIT:
ListView listView = (ListView)v.findViewById(R.id.session_list);
TextView sessionLabel = new TextView(mContext);
sessionLabel.setText("Add new session...");
listView.addHeaderView(sessionLabel);
Have you considered myListView.addHeaderView()
Use the Header to add your column view.
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.