Android 2.3.3 spinner auto sorting - android

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.

Related

Dilema: The best way to make table column not sorted

I am creating android app using Kotlin. I have fully functional multi column sorting. When I click on table header column it adds it index to sorting column list. It sorts both ways (ascending and descending). On click it changes direction. Everything works fine. But I would like to know what would be the best solution to remove sorting from one column.
I have few ideas but they don't sound good:
On third column click remove sorting on that column.
Add button which is visible when column is sorted. Button on click removes column from sorted list.
I would like some help because I don't know how to implement this feature in a good way.
Edit1:
Forgot to mention that sorting happens in back-end. In android I just set parameters which columns I need to sort and then send GET request to the server. As response I get sorted data.
I would mimic the way Google Spreadsheets does:
It shows a dialog and you can add sort columns and delete them.
You can see it by selecting some columns and going to Data / Sort the range.
Another possibility would be to have a 3 states switch in each column (sort ascending, sort descending, not sort). Here you can see it working in a website (look at the Multiple column sorting example): http://demos.shieldui.com/web/grid-general/sorting but, in my opinion, it is more confusing.
Hope this helps.

How to insert edittext value from custom listview into sqlite database in android

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.

How can I delete an item in a hashtable and have the keys adjust accordingly?

Currently I have a homescreen where the user stores titles of their classes they're taking this semester, and tied to each class is their assignments and flashCards for those classes. When the user long-clicks on one of the items on the homepage, I want to delete the class and all its contents (this includes all flashCards and assignments).
I've stupidly stored my data in hashtables with the keys as integers unfortunately. This has caused some problems when I delete items from the table. The keys are directly correlated to the position on the homescreen, and I can't seem to figure out how to readjust the hashtable when the user deletes something from it.
I've thought about using other data structures such as a arrayList but the problem is that the user could possibly and very likely have only one set of flash cards or only one assignment and it happens to be in the 4th position on the homescreen. This means that I would have to fill the first 3 positions with null first before I could insert the value at the 4th position. I definitely don't think I want that...
Any ideas?
I am afraid that with your current implementation, you will not be able to readjust the numbers in the HashTable. I would suggest you use LinkedList for the items on your screen and store iterators to the elements in that list in the HashTable. This way you will be able to efficiently delete any item.
Just assign unique IDs to all entities such as classes, users and assignments. Use these unique IDs, which may be integers, as keys and in all other places where you need to refer to entities. Do not use sequential order of entity as identifier, because it may change when item is deleted or even when items are reordered.

select any item from a list of 10,000 items in android

I try to search through the questions to see if there is any similar thread to my problem but so far haven't found any.
Here is my problem is: I have a list of products which contains ~10,000 items stored in a SQLite db. In my app, I need to search for any item from this list. I have a few options:
Use the autoCompleteTextView, with all products preloaded, and as I type the product's name, the list will suggest the product, hence I just need to select from the suggestion. This is the simplest way but I feel 10,000 items (or even more in the future) would be very heavy to load
As I type any character and click search, the app will do a select all products from the db with the character as a filter. The result set is then fed to some list view so that i can pick any item. This approach would save the memory as the app won't load all items to the memory but only items that contain the filtering characters.
Is there any better way to do this?
Thanks
Your second option is the best way. Its similar to how you get a list of recommended search results while you type in your query. For you, assuming your using asynctask to query your db you can have a proper loading message signaled by onProgressUpdate
Can you categorize the items? So that you can use use spinner to select the category, and use AutoCompleteTextView to type and select items.

Best way to approach Invoice Listing into Multi-Column ArrayList?

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

Categories

Resources