Android: Show real time stamp on list view items - android

In my application, i have a ListView with a Custom adapter containing data like an image, 3-5 TextViews.
This data is fetched from a web server, until now i am showing something
This item is posted on Oct 10th 2012
but i want to change it to something like
This item is posted 6 days ago
So, its something like refreshing the ListView every 1 min, I dont want to use adapter.NotifyDataSetChanged() or something like that, which reCreates the whole ListView with the new data.
I just want that TextView alone to change in background and get displayed on the UI.
Thank You

One option would be to create a custom text view by extending the android TextView class and put a time task in it which update the text view message every one minute.
you can also use the
http://developer.android.com/reference/android/widget/TextView.html#onVisibilityChanged(android.view.View, int) to start and stop the timer task.

I don't think that is supported out of the box.
Since it seems you already have a Date/Calendar at hand just convert it to relative time string with a function like this: http://www.zachstronaut.com/posts/2009/01/20/php-relative-date-time-string.html
If you want real-time updates, I think your best bet is to start a background thread giving it the ListView and update the visible items periodically, however you may want to update all of them to prevent inconsistencies.

For Reference i suggest you to use below thing for change in ListView.
1.You need to get Row View for Particular item in which you want to change Textview.
2.Now from step 1 i believe you have your row view so apply change in it's child .

Related

Edit button in listview item depending on other listview items

I would like to start by saying if you can think of a better title for this problem, feel free to change it since I have no clue how to explain this in a short way.
So here is my problem:
For the application I am trying to make I have these schedules, one schedule for today, and one for upcoming days. Both of these are a listview inside a fragment.
(I used those fragments to create tabs to seperate the two.)
Each game (let's call them games because calling them activities would be confusing) on the schedule has a status, and here is where the annoying part comes. I have been asked to check if any game has been started, and if so I need to disable the buttons to start any other game than the one that is already ongoing.
EDIT: The main problem is that I cannot use findViewById on the listview item because apparently it is a null object reference
I made a little "paint"ing to give you more of a graphical representation.
So long story short, I need a way to check the status inside of every listview item inside of the listview inside of the fragment inside the activity to use said status to disable every button except for the one of the ongoing game.
Little side note: If no games have been started yet, all buttons are enabled.
I hope this made sense.
If you want some code to go with this, feel free to ask but be warned, I am making this inside a testing app so a lot of useless testing data and sloppy code.
EDIT:
This is where I am stuck in a more clear example:
The start buttons are enabled but should be disabled.
Scrolling further down the list, there is a started 'game' and right below it, a game with the same status as in the previous picture where the button is disabled as it should be.
This is because the "isStartable" boolean in my code goes to false after the game with status "start" has passed and the following items are disabled.
When I scroll back up, it is how it should be, the items are all disabled but I need them to be like this when the listview gets filled. Any way to refresh the data in this listview and taking the "isStartable" boolean with it?
This is what it looks like after I scroll back up.
create a model class for your listview data items. Set a boolean variable in model class like isGameStarted. Set that variable as per your result.Then in your listview adapter, put a condition as below
if(isGameStarted){
holder.startButton.setEnable(true);
else
holder.startButton.setEnable(false);

Hourly list view in android

I'm trying to implement a simple calendar as my first Android app, but I don't know how I would go about implementing the daily view which consists of a ListView of all available hours, including those that have an event assigned to them.
Should I generate the empty hours and insert them into an ListView? Is there any better way?
Thanks!
You insert into your ListView what you want to show to your users. If you want to show a row of empty case for those hours without an event then yes you put those hours into the ListView. However, if you want to represent a collapsed view where only the hours with an event are shown then you don't include the other hours.
A good idea would be to represent only the hours with events but expand the list to dynamically include the other hours for the day when the user touch on it and vice-versa.
You could also use an ExpandableListView if you want to have something more like an hierarchy but using an ExpandableListView is not essential if you want to dynamically add or remove items to your ListView.

List View Pagination

How do I do pagination using ListView. For an example, I have 12 records in my ListView and I want to show first 10 data in 1st page and the remain data in next page.
You can add a footer on your listview that will hold the buttons for next and back. In an stackoverflow question you have the complete solution for that.
Next step can be done in several ways. The simplest one, that I am seeing right now, is: Save the page you are at a variable. Then, each time you do next you add one to that variable. In your adapter, in the getView you multiply that variable by the position so you can get the correspondent position depending on the page the user is.
Better way to build view like pages, use Fragment,You can show desire content in pages as you mention in your question, for more details look out section 21 example,it cover fragment.

Android: ListView Listener?

I am wanting to display a text field stating that my listview is empty if there is nothing in my list. I know this can be accomplished quite simply using an android:id/empty textview in the xml file, but this also requires that I extend my activity with something other than Activity and I don't want to do that.
I guess my only solution is to create a textview and then set its visibility to gone when my code detects that something has been added to the listview. I can simply check the array that populates the list, but is there some sort of listener so I don't have to run a checkListIsEmpty() constantly throughout the code. I was hoping I could use something that would simply sit quiet and wait for the listview to become populated and when that happened change the visibility to visible and then begin waiting for it to become empty again.
Or you can use setEmptyView(View v) on your ListView in your Activity.

Access a custom ListView item

l'm new here so hope to follow guidelines as I'm a newbie for both the side and android.
For my first app I'm trying to make a world clock.
In order to check it the time is shown and update in seconds. (00:00:00)
As the only thing I want to update every second is the time I don't want to use:
ListView's notfiyDataHasChanged()
I have a custom ListView & extended BaseAdapter and row.xml with each ListView item layout.
Trying:
lv.getChildCount() returns 0;
So I understand it has no child.
But I don't know how I can retrieve the View of the specific item in the listview.
Then call that view's TextView by findViewById(R.id.time) and set it each second.
I've googled and read alot of threads but still didn't get that :(
Thank You.
I would go a different way about that problem: Subclass the TextView widget into your own TimeView widget and include that one in your row layout.
The TimeView widget will own a handle that calls itself to refresh the view.
Hence, automatically, all views visible in the list will update themselves, the other adapter items will not be having views and won't as a consequence require any update.

Categories

Resources