Sunday, February 25, 2018

How to populate different object with same fields or properties in same recyclerview in android

Leave a Comment

I am going to develop soccer app, and having following json response.(link)

I have two different class with different names, but with same properties or fields and want to display one same single RecyclerView.

Here I have worldmatch and world having same fields like matchTime, startTime and endTime. I have created pojo class using jsontopojo plugin.

Here is main things, I want to display worldmatch in position 0 and rest of other position world based on club. You can see more details in picture.

enter image description here

enter image description here

This has to be first tab(world) and similarly to other tab like Europe , Asia with respective similar pattern.

Tab1(World)

---------------position 0 (All match)------------------ | | 930   1100   1130  and so on ( horizontal recyclerview) | ------------------------------------------- ---------------position 1(Barcelona)------------------ | | 1130   1230   1330   1430  and so on (  horizontal recyclerview) | ------------------------------------------- ---------------position 2(Chelsea)------------------ | | 1300   1400   1500  and so on (  horizontal recyclerview) | -------------------------------------------                      .                      .                      .                    so on         (vertical recyclerview) 

Details explanation picture view:

enter image description here

I have two Recyclerview Adapter, first one is display clubname and pass the respective data to other recycler view which gonna display horizontal view with respetive matchTime.

Populated the Outer Recyclerview position 0, worldmatch data and it reflect all the others, how do i pass the populated both worldmatch and world data in same recyclerview and how to filter out all the tab.

Display matchTime field only from WorldMatch list in position 0 and World list in below 0.(horizontal recyclerview)

Any one have any idea, which is really helpful for me and highly appreciate any idea behind this. Thanks in advance.

4 Answers

Answers 1

It is possible. Now you can set the value to another class in response parsing time or setting Adapter time. I also did this way. For example:

   setmatches(List<club.wordlWild> ww){    List<WorldWild> list = new ArrayList<>();    for(club.worlWide cw : ww){      WorldWild w = new WorldWild ();      w.setMatchTime(cw.getMatchtimie);      ..      ...// set values     list.add(w);     }    } 

Now you can get add club.worlWide values to WorldWild. If you want change do vise versa.

Answers 2

You can have the worldMatch as the header of your vertical RecyclerView. You do not really need two different adapters for your RecyclerView in your case. Just add the header in your RecyclerView with the worldMatch class will suffice.

If you want to take a look at how you will be going for adding a header or footer in your RecyclerView, you might take a look at the answer here.

Once you are done with adding the worldMatch as the header of your vertical RecyclerView you will be passing the world data in the adapter of your RecyclerView and show them accordingly.

Let me know if you have any confusions.

Answers 3

I encourage you to create a TabData class that you will use for each tab.

public class TabData {     private List<TimeData> regionTimes;     private List<Pair<String, List<TimeData>>> clubTimes = new ArrayList<>();      public void setRegionTimes(List<TimeData> list) {         this.regionTimes = list;     }      public void putClubTimes(String clubName, List<TimeData> times) {         clubTimes.add(new Pair<>(clubName, times);     }      // Getters } 

This would be the TimeData class.

public class TimeData {     int matchTime;     String startTime;     String endTime; } 

Then, for example, to show the first tab (World tab) you should do the following:

TabData worldTabData = new TabData(); worldTabData.setRegionTimes(jsonData.getSoccer().getWorldMatch());  for (ClubData clubData : jsonData.getSoccer().getClubs()) {     String clubName = clubData.getClubProfile().getClubName();     List<TimeData> times = clubData.getWorld();     worldTabData.putClubTimes(clubName, times); }  regionAdapter = new RegionAdapter(worldTabData); 

And this would be the adapter for the outer RecyclerView (vertical):

public class RegionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {     private TabData data;      public RegionAdapter(TabData data) {         this.data = data;     }      public int getItemCount() {         return 1 + data.getClubTimes().size(); // One row per club plus another one for position 0     }      public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {         if (position == 0) {             // Set adapter for horizontal recyclerview with data.getRegionTimes()         } else {             Pair<String, List<TimeData>> clubTime = data.getClubTimes(position - 1);             // Set textview with club.first             // Set adapter for horizontal recyclerview with clubTime.second         }     } } 

Hope you get the idea!

Let me know.

Thanks.

Answers 4

I did something similar via a parent class to use in the adapter but in your case it might be possible to just have the same class for each match.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment