change date difference when application loads - android

So im using a SimpleCursor Adapter to load up my task list, and in each row there is a text view with the time left in it. What i want to do is update the list rows when the user goes to the listview activity so it will update the time left. the problem im having is simple cursor adapters fill the list with items from the database and i need to change the time left text via android by calculating it on the fly and only accessing the DB for the datedue.
I tried storing the timeleft into the DB then updating it when the list is shown but thats not working and seems pretty messy.
edit: I do know how to calculate the time difference. All i want to know is how can i fill a list with Cursor items and non-cursor items.

Related

Android ListView real time content

I have a simple listView with simple adapter and I am getting a new live content (it means data with changed items) every 5 sec. I have to update some rows with new values, so I clear adapter and addAll in list, but then all list is freezing and scrolling up.
If it's a short list, all is work good, the problem becomes when I have long list with scroll.
Is there better solution for my task? How could I monitor only changed rows and redraw there?
Instead of ListView use RecyclerView. RecyclerView allows to refresh and add individual elements to list.
Adapter method item added at position
So my solution is. As I have abnormal list (I'm getting response from back-end with different values for each row, I must to group it and generate unique layout for each row), so I've used a TableLayout, I generated each row and put it to Map, when I catch new response I check old value with new one and if there are different, I take View from Map and bind new data.

Android Dynamic GridView updates within seconds

I want to create a dashboard wherein I want to populate some data in a GridView in android. For example,
Activity - GridView in which there are two columns.
Each grid is Header Text and Result Text.
Now I want to update only Result Text with time as I am fetching it from database and updating the values in real time.
Keeping my Header and whole view intact.
Can anyone suggest any quick way to do so...??
You only need to update the underlying data of the adapter and call notifyDataSetChanged() so the adapter knows the data was changed.

Android GridView : Adding items to a rendered gridview

I have a GridView with multiple rows and each row having 2 columns.
Let's say 10 rows are being displayed and on click of a button I want to make a call to the database fetch some more items and display these items below the previously rendered items in the same GridView.
I am using CursorLoader for my DB calls.
I tried the following approach:
Swapping the grid adapter with the new fetched Cursor.
But this makes the whole GridView redraw itself.
Whereas I want it to just add items below it.
Any suggestions or pointers on this would be really helpful
P.S. Fetching the items in any manner is not a problem.

What's the best way to handle a list view whose cursor gets frequent reads and writes?

I am building an Inventory application. The items are shown in a grid view and each cell of the grid view also has a TextView in the upper right corner that displays the available quantity of the item. The user can single click on the gridview cell to increase the quantity or long click to decrease it.
I am currently using a class derived from SimpleCursorAdapter to display the data, but I am not sure about how to update the quantity in the DB. I am afraid that if I write directly to the DB and then create a new cursor and change cursor that the application might become bogged down if the user clicks repeatedly (say to add 10 items)
I have considered copying the data from the query cursor to an array in the Activity and then using an ArrayAdapter but this seems kludgy.
I have also thought about creating an array in my SimpleCursorAdapter which would cache items that have been modified and then save those items when pausing...
Is there a better way? A more Android way?
I guess this comes down to: what is the best way to make rapid changes in the DB and UI?
I don't know much about Cursors in this regard, but I would create a custom ArrayAdapter based on a model object rather than a Cursor. That's just a personal preference, but by doing this I don't have to make any assumptions about the Cursor implementation or know it inside and out.
Let's say you're displaying a collection of type Item in your grid. I would create a class called ItemsList that derives from ArrayList<Item> and my adapter would derive from ArrayAdapter<Item>. The reason you create a wrapper around the ArrayList is that you can allow the user to interact with it freely and mark it as "dirty" and allow it to update the database asynchronously. This way, you just call notifyDataSetChanged() on your adapter and you don't have to think about the overhead of updating the db...you've separated interacting with the collection being displayed and the process of updating the persistent storage.
Btw...this video is a must when working with ListViews and Adapters if you haven't already seen it:
http://www.youtube.com/watch?v=wDBM6wVEO70
You could use a regular listView and a regular Adapter with a list (arraylist) of items in it. Every time user clicks or long clicks you can edit the list in your adapter and notifyDataChanged() which will reflect the list changes on your listView. If you want to keep your list after user quits your application you can use either database(for bigger amount of data) or preferences (for smaller data amount).

ListView's contents scrambled on scroll

So I am having a problem with the different pieces of that make up my ListView. I put them into an ArrayList and use a custom ArrayAdapter to hook up to the ListView, which I have done before so I don't believe there is a problem there. Initially the list seems to have the pieces in the correct order, but then I will scroll down the list and the contents will then load in incorrect order. I then scroll back up and everything is jumbled. Has anyone run into this before?
Thanks
-Jake
Yes your problem is related to the fact that List reuses the views for each row. So say your list can see 5 items, but your ListAdapter has 15 things in it. Android will create 5 + 1 instances of your row view instead of 15. One for each row in the list + 1 for when half of the top and bottom can be seen. When a row is moved out of the visible area the List will recycle that view instance for another row instead of creating a new one. If you don't properly reset all of the user interface components every time you'll get artifacts from other rows showing up. You must make sure that every time you bind your data from the objects in your array list to the view you set every field every time.
For a better description of this see
http://www.youtube.com/watch?v=N6YdwzAvwOA&feature=related

Categories

Resources