0

I have created an app in Android Studio and linked with Real-time Database of Firebase. I have a table named Attendance Records in my database, in which the first key is employee ID, the second key is month, then date and then the details of that employee's attendance of that date.

I basically have to let the user view his attendance and his attendance record should have the rows of that date colored on which it was a holiday.

I have an activity in which user enters his empid along with year and month and then a table should be displayed with same column names as the details under the date key. I can do this by directly displaying the data snapshot but it can't be modified. I need to modify it in the way that I access another table Holidays and compare the dates mentioned in 'Holidays' table and the data snapshot of the specific user and color those rows (for which date exits in both tables) in the dynamically created table.

I am referring to https://technotzz.wordpress.com/2011/11/04/android-dynamically-add-rows-to-table-layout/ for dynamically creating the table.

How can I get values from the data snapshot one by one and put them in the while loop to enter them in the table rows which are being dynamically created?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Dpka
  • 73
  • 6
  • 1
    To iterate over the `DataSnapShot` [this](https://stackoverflow.com/q/37508782/5482999) link should help you. While iterating over the items add them to an ArrayList so you can populate your table. Personally I would go for a RecyclerView Firebase has its own RecyclerAdapter. – Guanaco Devs Mar 26 '18 at 08:47
  • What difference will recyclerview make? – Dpka Mar 26 '18 at 09:02
  • 1
    Recyclerview is optimized for that kind of operations. However if your data is not meant to grow maybe you just use a tablelayout, I think that it will depend how you want to manipulate the data you are populating and how you are tackling your problem. – Guanaco Devs Mar 26 '18 at 09:15
  • Alright and can you tell me how can I color specific rows(for dates which exist in both the tables) ? – Dpka Mar 26 '18 at 09:23
  • 1
    If you will go for the Recyclerview approach, you'll do it in your adapter in onbindviewholder. – Guanaco Devs Mar 26 '18 at 09:37
  • And what if I want to use Table Layout? Is there any method to color rows in this? – Dpka Mar 26 '18 at 09:52
  • Using an adapter to populate the table. – Guanaco Devs Mar 26 '18 at 10:13
  • RecyclerView seems more promising, so, if I use RecyclerView I will still have to fetch data from Firebase by Datasnapshot and iterate through it and serially store in the RecyclerView? I am new to RecyclerView please guide. – Dpka Mar 26 '18 at 10:19
  • [This](https://youtu.be/Vyqz_-sJGFk) video should get you started, he is very clear and understandable. Good luck . – Guanaco Devs Mar 26 '18 at 10:26
  • Okay! Thankyou so much! You helped a lot! :) – Dpka Mar 26 '18 at 10:30
  • @Racu Hey! Unfortunately I am unable to post questions, but I wanted to ask one, I want to check if the date(I have in a variable) is a Sunday or not, how can I do this? Please guide.. – Dpka Apr 06 '18 at 09:00
  • 1
    I think it will depend on the format of your date, e.g.: `Calendar calendar = Calendar.getInstance(); calendar.set(2018, 3, 18); System.out.println(calendar.get(Calendar.DAY_OF_WEEK));` Note that here the date is April, 18, 2018, which returns a "4" corresponding to WEDNESDAY – Guanaco Devs Apr 06 '18 at 12:32
  • Oh okay great, and can you also tell me in which format should I get the date from the user(and save in the database)? – Dpka Apr 10 '18 at 10:02
  • @Racu If I have entered 10 dates in my Firebase database using date picker in one activity and then in another activity I want to use your code of Calendar class then for that, how can I break the date string into year, month and day? – Dpka Apr 11 '18 at 05:36
  • How are you storing your date? Milliseconds long? String? Maybe [this](https://stackoverflow.com/a/12473614/5482999) helps you – Guanaco Devs Apr 11 '18 at 05:41
  • or [this](https://stackoverflow.com/a/8655033/5482999) – Guanaco Devs Apr 11 '18 at 05:44
  • @Racu great! Thanks! Just one more thing, for reading from Firebase I am using addListenerForSingleValueEvent in onBindViewHolder and then doing my if-else statements for coloring the cardview, the issue is, I have 2 dates for 2 different cases and both of those cases(if-elseif) are being reached in code, but only the first cardView's color is changing, is it because of the addListenerForSingleValueEvent? Should I use any other listener? If I use addChildEventListener then should I enter my if-else statements in onChildAdded or what? Please help :/ – Dpka Apr 11 '18 at 10:48
  • Man it depends on what you want to achieve. The way I see it having your listener in onbindviewholder, not a good idea. It's hard to “advise" without seeing code. Maybe it would be OK to have the listener there. The color change has nothing to do with your listener. If your data is not going to change use the singleValueEvent... – Guanaco Devs Apr 11 '18 at 10:58
  • Okay, I'll look to it, Thanks! – Dpka Apr 11 '18 at 11:00
  • What you might wanna do if the color change is supposed to happen when the Recyclerview is attached is to notifyDataSetChange() or notifyItemChanged(int position). – Guanaco Devs Apr 11 '18 at 11:12
  • @Racu I have posted the code : https://stackoverflow.com/questions/49774931/only-first-cardview-of-recyclerview-is-being-colored Please have a look! :) – Dpka Apr 11 '18 at 12:19

0 Answers0