In my database, I have entries with multiple songs. This is what it the DB looks like, and what is showing in the display:
"DjSunGazer" : {
"song" : {
"-LmHrkYkU1qD2GND9wY2" : "Blaya - Cash",
"-LmHrlalIVUStU6nqBJI" : "Blaya - Cash",
"-LmHrmRRXy4UYoF7DNZz" : "Taylor Swift - You Need to Calm Down"
}
},
In my app, I was able to use a hashmap to assign each unique song with a counter. Now, in the Firebase RecyclerView Adapter it only shows the last entry in the database. Here is the code:
query = mProfileDatabase.child(djName);
FirebaseRecyclerOptions<DataSnapshot> firebaseRecyclerOptions =
new FirebaseRecyclerOptions.Builder<DataSnapshot>()
.setQuery(query, new SnapshotParser<DataSnapshot>(){
@NonNull
@Override
public DataSnapshot parseSnapshot(@NonNull DataSnapshot snapshot)
{
return snapshot;
}
}).build();
final HashMap<String, Integer> songCounts = new HashMap<String, Integer>();
firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<DataSnapshot, ResultsViewHolder>(firebaseRecyclerOptions)
{
@Override
protected void onBindViewHolder(@NonNull ResultsViewHolder holder, int position, @NonNull DataSnapshot model)
{
List<String> sArr = new ArrayList<String>();
for(DataSnapshot snapshot : model.getChildren())
{
sArr.add(snapshot.getValue(String.class));
}
for(int i = 0; i < sArr.size(); i++)
{
String eachSong = sArr.get(i);
if(songCounts.containsKey(eachSong))
{
int count = songCounts.get(eachSong);
songCounts.put(eachSong, count + 1);
}
else
{
songCounts.put(eachSong, 1);
}
}
Collection<String> name = songCounts.keySet();
Collection<Integer> ctn = songCounts.values();
for(String s2 : name)
{
Log.d("INFO", s2);
DisplaySong ds = new DisplaySong(s2);
ds.setSong(s2);
holder.setDJNameView(ds);
}
for(Integer i : ctn)
{
holder.setBadgeCount(i);
}
}
@NonNull
@Override
public ResultsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
{
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.djname_item, viewGroup, false);
return new ResultsViewHolder(view);
}
};
firebaseRecyclerAdapter.startListening();
recyclerView.setAdapter(firebaseRecyclerAdapter);
songCounts.clear();
Instead of just the value showing in the recyclerview as "Rick Ross - Gold Roses (feat.Drake)" with a 1 badge saying theres only one occurrence in the DB, I want it to display "Ginuwine - So Anxious"(3) as well below that first entry. This has already been calculated and handled in my songCounts HashMap. Thanks!
EDIT Added dj_name_item.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="@+id/song_result_dj"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp" />
<TextView
android:id="@+id/song_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginTop="3dp"
android:layout_marginEnd="-5dp"
android:layout_marginRight="15dp"
android:background="@drawable/badge_background"
android:gravity="center"
android:padding="3dp"
android:paddingRight="15dp"
android:text="0"
android:textColor="@android:color/white"
android:textSize="10sp" />
</RelativeLayout>
EDIT: added how the data is being inserted
FirebaseDatabase.getInstance().getReference().child(djName).child("song").push().setValue(songName);
The problem is with your json structure. In this situation, you'd want to be using a json array for the songs, and likely have a Song
object. Right now, they're likely parsed out as just an individual "song" which is really a String
. Something like the following json structure would be a start to something that would work as intended:
"DjSunGazer" : {
"songs" : [
{ "id" : "-LmHrkYkU1qD2GND9wY2", "name" : "Ginuwine - So Anxious" },
{ "id" : "-LmHrlalIVUStU6nqBJI", "name" : "Ginuwine - So Anxious" },
{ "id" : "-LmHrmRRXy4UYoF7DNZz", "name" : "Ginuwine - So Anxious" },
{ "id" : "-LmHrpZzOKOtY_8AGQ_S", "name" : "Rick Ross - Gold Roses (feat. Drake) }"
]
}
I'd still recommend updating the json structure further, so it's easier to parse. Right now it seems like you're including the artist's name as the key value for the song set, which I could see also causing problems or making things more difficult than they need to be.