1

I have to do a list of 24 hours that contains rows. Each row represents an hour.

For example: I have an activity at 06 am to 10 am. I need an object to cover that period time (6am - 10am). As the image shows:

http://img824.imageshack.us/img824/6190/20130221192829.png

I wanna know how can I do that and what layout should I use?

3 Answers 3

1

I would use a ListView with 24 rows, one for each hour, and write a custom adapter to paint the hours backgorund according to the time period you want to highlight.
In the adapter you just make the getView function change the row layout background color if the hour fits the time period.

Here you have nice tutorial for ListViews and Adapters

If you need to be able to mark partial hours, lets say 15 min interval you can use a custom row layout like this one:

enter image description here

You have TextView placeholder for the hour on the left, 4 textViews for 15 min interval, (color their background) and an overlapping one with transparent background to place text.

0

Another solution would be to simply have a linearlayout in a scrollview, it's not quite as slick memory consumption wise as a ListView as everything is loaded but since you're restricted in the number of entries you'll be showing you can determine how much memory it'll take to load it all.

The big advantage with a LinearLayout / ScrollView combo is that you control the layout without being restricted to fitting it into a series of lines. It's also much easier to get your head around than the ListView / Adaptor combo so should be easier to implement.

Edit:

Thinking about it, the LinearLayout probably wouldn't be the best as it wouldn't allow you have a single view overlap multiple hours, you might be better off with a RelativeLayout or TableLayout.

4
  • Actually, if the OP needs to define 6 TextViews per row, each with its own id, plus the LinearLayout,it will have to manually write 145 XML elements. ::: Wiht ListView it only has to write 2 elements in the main XML and 6 in the custom row layout. ::: Not to mantion the coding to manage 144 TextViews vs the simplicity of managing a generic row.
    – ilomambo
    Feb 23, 2013 at 6:29
  • This isn't actually true, firstly you can write a single xml element and inflate it multiple times, secondly since I'm assuming that this is wanted to be dynamic rather than static then the app will want to adjust what there is according to some data. This means that manually writing 145 xml elements wouldn't allow the app to function. There doesn't need to be 145 textviews (that sounds insanely large), from the original description there's 24 textviews, one per hour, plus something else to the right. Feb 23, 2013 at 15:25
  • If you inflate the same XML multiple times, wouldn't the elements in that XML have the same android:id ? ::: "plus something else on the side" means another 24 textviews ::: Plus, it only allows to mark the entire hour, if the OP wants to mark form 6:30AM to 10:30AM how would he do that ? ::: The ListView solution is not only simpler, it is much more flexible because it allows to change the rows layout easily. ::: If the list was significantly smaller I would agree to your solution, but it is not the case.
    – ilomambo
    Feb 23, 2013 at 16:04
  • Yes, if you inflate from the same XML you'll get the same ID, but the views are in a hierarchy so you can select the one you wish to use (or keep a reference as you inflate it). The OP hasn't specified what content is supposed to be to the right, I'm guessing it's a calendar like goal so I'm not certain that 4x15 minute TextViews per hour would be the best bet. Personally I'd customise a relativeLayout or something similar to reduce the number of views onscreen but having a single view cover the entire 'block' of time is slightly easier to handle than same view covering 6 listView lines. Feb 25, 2013 at 10:32
0

I solved using a ListView. First, I have to get the database information and save it in a cursor. Then, I create a personalized adapter with an arrayList that contains an activity/event where the id and the activity/event description were saved. Each arrayList position represents an hour. The cursor contains the "inicial hour" and the "final hour" If the cursor contains number 03 as inicial hour, and 08 as final hour, then: -I have to complete the arrayList with null values since the beginning to 03. In that way, the first 3 hours will be the first three positions of the arrayList - Then I have to do the subtractor between the inicial hour and the final hour, 'cause I need to know how many arrayList positions will be in that activity/event. - In the fourth position of the arrayList I put the activity/event with an id and the description. - Then, in the next position, I have to put an activity/event with an id and without description of many rows and the subtraction results. Finally, I put away the arraylist in the personalized adapter. I appreciated your help.! Thanks a lot! Regards, Jose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.