I have created a SQLite DB, in this DB is a whole wack of invoices, each invoice has a customer ID or supplier ID depending on if the invoice is a sale or an expense respectively.
Basically, what I am trying to do is query the DB, grab a list of all invoices by whatever customer ID or supplier ID is selected, and display all the invoices for this customer or supplier in a multi-column listview. I have already created the multi-column listview, it is driven by an xml template for each row which is basically a linearlayout with 4 textview's, one for each column.
This being said, there are 4 values from each invoice I would like to populate the listview with. I assume that if I create an ArrayList, add each invoice as it's own row (also an array of the 4 values I need per invoice), I will get a nice and easy multi-dimensional ArrayList that I can simply loop through to populate the listview.
Before I dive into the code, I want to ensure this is the best approach, perhaps there is a method I am missing? I was surprised I couldn't find multi-column listviews, so you never know!
Thanks!
If your ListView is meant to show items from a SQLite DB you want to write a CursorAdapter instead of reading the whole result set into an ArrayList. CursorAdapters can display each row from a query efficiently and help you easily reflect any changes to the backing data that happen while the user is viewing it.
You may also be interested in this Google I/O talk that goes over the basics of ListView: http://www.google.com/events/io/2010/sessions/world-of-listview-android.html
Related
I am trying to create a VanSale model app in android. I have a form with customer name and customers item field. In the item field I have created a custom listview with 7 columns.Column contains Sl No, ItemCode, ItemName, Quantity,Discount,Rate and Amount. 5 items will shown in the list. When I pressed a add button will add 5 more items to listview. List items can be upto 100. I want to insert the each items in the listview into sqlite database. How can I done this? Is there any other easy methods to done this?
First Think, you have to learn about looping, iteration conceptual in your mind and take a practice using simply array or list data. if You already know about the sqlite database and listview, i thought you only need to made some function to get from listview using position, and another function to write into database.
Second, Listview has position parameter if you passing it into onSelectedItem method, or you can get directly from listview self and get using position, and sqlite db which you use list or array has position also.
I think its a logical question. so my answer is logical too, or you can attach your code's also here to detailing your problem, good luck.
say i have a database with a table in it, and the table contained data in the following layout:
Name type difficulty
-------------------------------------
Test1 polo 7
Test2 golf 2
Test3 vw 8
Test4 vectra 100
Now imagine that the list goes down to Test100. How would I utilize a listview to show the data in four neat columns with each listview element showing 1 row of the table?
It doesn't have to use sql databases, if it can be done in xml, i can work with that
I have looked around at lots of database tutorials, but i want to use a table that does not have to be added to the device first. So maybe a different format would be better?
Thanks
While retrieving data, write query accordingly to your required sorting order.
Use list view with list item of 3 textviews having weights.
I'm filling a spinner with user-provided data from a SQLite database.
The problem is that on Android 3.0 and newer the spinner list occurrence is the same order as the data in the database (not sorted). However, in Android 2.3.3, the spinner contents are automatically sorted alphabetically. This means that the first item in my spinner is not the same item as the first item in my database/cursor.
A possible solution is to use getItemAtPosition(pos).toString() but since my spinner rows contains a combination of two columns from my database, this means I have to split the outcome of getItemAtPosition(pos).toString(), search the database for the first word, then search the results for the second word. Then I have to retrieve the corresponding ID and display the data belonging to this ID on screen. All of this code has to run every time the user selects another item on the spinner, which seems rather inefficient.
Most examples/tutorials I have found assume that the list order of the spinner and data are the same.
What am I doing wrong here?
I'm not sure what's going on. I've used spinners myself and never encountered that problem. Then again, I usually impose some sort of ordering myself, as I can't rely on the semi-random ordering of the database entries.
I suggest you add a "order by _id asc" (or desc) to your request. This will force Sqlite to sort the data according to the sequence in which they were added to the database.
So at the moment I have a simple reminder app that you enter some details, select a data and time and then the app reminds you on the selected date/time.
I now want to be able to add functionality to select categories which I want to select from a spinner, which is populated from separate database table - I will be adding a class to add new categories.
Is an sqlite database the best way of doing this or is there an easier alternative? I assume I will need to make foreign keys/triggers?
Any advice is much appreciated.
Yes, I think the best way is to use sqlite3, especially if you're mutating data.
Spinner is an AdapterView like ListView or GridView, so you can just use a CursorAdapter (or possibly SimpleCursorAdapter) to populate it with data from your database.
This looks like a decent tutorial for creating a SimpleCursorAdapter. In your own code you would call spinner.setAdapter(mySimpleCursorAdapter);
With the application i am working on i came with the following problem. I have a listview that should display data from a data base table. There are two scenarios that could happen:
Scenario one - the database table is filling dynamically and the listview also should dynamically display table's information e.g to grow depending on table size.
Scenario two - the table has been already filled and the listview has to just display the content
So the problem is how to accomplish this behavior using ListView and ListAdapter.
So far i have solution for each scenario but no for the two together.
Scenario one - Use AsyncTask and ArrayAdapter. In doInBackGround query the db periodically and pass the result to onProgressUpdate, than just fill the ArrayAdapter with the newly added values. But when came scenario two i query all the table than copy all values to the ArrayAdapter in one step which is pretty slow.
Scenario two - Query the db again the just use CursorAdapter. But in this case i can't update the ListView dynamically using CursorAdapter.
So any ideas how to implement this using one adapter, or just should i use ArrayAdapter and CursorAdapter depending on the case ?
Scenario one - the database table is
filling dynamically and the listview
also should dynamically display
table's information e.g to grow
depending on table size.
Try:
Scenario 1a: Whoever is getting the data not only puts it in the database, but tells the activity "here's some new data to display".
Databases are great data stores but are lousy pipes. Your "Scenario one" tries to use the database as a pipe -- instead, pipe around it.
Now, if the case is that you have both in-the-database data and new data coming in, you'll need to stitch those datasets together. You can use my MergeAdapter for it: give it two ListAdapters, one representing your existing data (perhaps a CursorAdapter) and one representing the new data (perhaps an ArrayAdapter). It will render them as a combined entity.