Over accessing a content provider in android - android

I want to write an app that loads in a load of words from a content provider. Then i want to update certain fields associated with a word, ie view count etc, one at a time
What is the best approach to accessing the data.
So first i load all the words into a Cursor object, when i want to update a word view-count do i just call an update event . Will this update just the Content provider or will it also update the fetched results that are stored in the Cursor
After a word is viewed i want to remove it from the Cursor object so it wont be shown again, is that possible or do i need to do a full query to get all the words again
sorry if vague or hard to understand

Related

How to retrieve data from SQLite table with inserted values

I have dozens of data value that already inserted inside Database class that extends SQLiteOpenHelper, however I want to get the data values from it and not just to view but use the values, to calculate them. I've been reading lots of examples and following tutorials, but none of them ever explain how to get a data or some data and make a use of it (in my case, use them as java variables(get from inserted data then send to another class)). To take few (not all at once), select 1 or couple to be a multiplier for user input. My app will calculate pmt function, which the user will input the price and etc, then when the user press calculate the app (will do in background) access data from database class (the value that have been inserted) get them, send them via intent to another class, then calculate the database values along with user input.
In short, the app have stored values inside its database that will then be called whenever a button is triggered. Passing the existing values to another class.
I've tried this far My Previous post, click here
There I was trying to send a data (testing) from database to a class(that should have retrieved but it got null values)
The question:
How to get a data(datas) from a database which have inserted values to another class and make a use of it as some calculate-able values?
UPDATE:
I found out that the cursor getting null values, any ideas why?
thanks in advance.
Solution has been added on the above link, check on the answer.

Select multiple object type with auto complete

I am looking for some help to find a powerfull way to allow selection of different List items.
My case is that i have for exemple a List profiles, List teams ... and i'd like to have an autocomplete input that will show, for exemple if i type Al, all Teams and Profiles objects having there member variable name begining by Al.
The result would be that i could get from the activity, on submit click performed, a List & a List containing all the objects who have been selected through the autocomplete form.
Also i'd like that the list offered to the user that match the chars he typed show the name and a picture (facebook like tag selection).
Obviously i am not asking for some code but at least some guidelines from experienced Android devs who know what to do and not to do to create this kind of thing.
Thanks
Loader are one of the best way to filter list. You init a loader which take the String constraint used for filter, the each time the user type, you update the constraint and restart your Loader.
If I suppose that all your object are cached in a SQLite database you can use a CursorAdapter, Cursor and CursorLoader.
You create the needed CursorLoader by filter the query with the content of the EditText.
If you're not familiar with CursorLoader there is the AsyncTaskLoader, with this you won't have the need of DB and to code a provider which can accept raw query. Your object in ListA, ListB, etc must inherits of common class (hum... DataThing maybe :-)), you concatenate the objects in a list then you can filter the list : What is the best way to filter a Java Collection?
That's for the data filtering. Now in order to display the data the way you want, you can display of list below the EditText field or create a custom component if you want a more advanced look.

Using Cursor respond and getExtras not working?

I'm designing a ContentProvider which needs to return a cursor with data and I need to know the status of that data.
I need a status for the cursor data since this data was downloaded and I need to know if it only downloaded partial data or if during the download some of the elements weren't downloaded. It's a bit more complex but you might get the idea why I need a status for my cursor data.
So I have reviewed the source code for all Cursor implementations in Android and it seems getExtras and respond functions are plain garbage since they don't do anything internally... Also, there seems to be missing a setExtras function. Any ideas?
I think that the method is not setExtras(), but respond().
I'm not sure what you mean by the "status of the data", but it sounds like you want to know if the columns in the cursor are clean or not. Most content provider implementations I've seen put this type of status into the rows themselves, or store it in a separate table. I don't think you should try attaching the status to the Cursor itself.
Just so you know, if you want to get notification when the data underlying the cursor changes, you can use setNotificationUri(), registerContentObserver, or registerDataSetObserver().
Notice that nothing requires you to make the columns you provide from a content provider be the same as the columns in any underlying database you use. You could add a column that the provider itself generates, and store status in that.

Which adapter choose?

I have two methods which read the same data from database, the first returns Cursor and the second returns List of objects.Now I show my items in activity using SimpleCursorAdapter and the first method, byt I can also use the second method and appropriate adapter.
Which of these two ways is beter to use and in the second way which adapter I should use?
P.S sorry for poor english
Definitely go with SimpleCursorAdapter. If possible, always use Cursor if your data comes from database, you save memory by not creating List of objects. Creating objects in Java is expensive with regards to time and memory consumption and you have to bear in mind you are on mobile platform with limited resources. If you are using List of objects for your ListView than use custom adapter extending from ArrayAdapter.
It's not always straightforward to use Cursor although your data comes from database. Let's say you store places in the database defined by its name and location and you want to display them in a ListView sorted by distance from current location. It makes it difficult to execute a query which returns sorted results unless you don't store relative distance in additional column. But you can get Cursor convert it to List of objects and sort this collection before sending it to your ListView.

Android SQLite - Update table only if different

I currently successfully use a SQLite database which is populated with data from the web. I create an array of values and add these as a row to the database.
Currently to update the database, on starting the activity I clear the database and repopulate it using the data from the web.
Is there an easy method to do one of the following?
A: Only update a row in the table if data has changed (I'm not sure how I could do this unless there was a consistent primary key - what would happen is a new row would be added with the changed data, however there would be no way to know which of the old rows to remove)
B: get all of the rows of data from the web, then empty and fill the database in one go rather than after getting each row
I hope this makes sense. I can provide my code but I don't think it's especially useful for this example.
Context:
On starting the activity, the database is scanned to retrieve values for a different task. However, this takes longer than it needs to because the database is emptied and refilled slowly. Therefore the task can only complete when the database is fully repopulated.
In an ideal world, the database would be scanned and values used for the task, and that database would only be replaced when the complete set of updated data is available.
Your main concern with approach (b) - clearing out all data and slowly repopulating - seems to be that any query between the empty and completion of the refill would need to be refused.
You could simply put the empty/repopulate process in a transaction. Thereby the database will always have data to offer for reading.
Alternatively, if that's not a viable solution, how about appending newer results to the existing ones, but inserted as with an 'active' key set to 0. Then, once the process of adding entries is complete, use a transaction to find and remove currently active entries, and (in the same transaction) update the inactive entries to active.

Categories

Resources