在我的数据库中,有多个歌曲的条目。这是数据库的样子,也是显示的内容:
"DjSunGazer" : {
"song" : {
"-LmHrkYkU1qD2GND9wY2" : "Blaya - Cash",
"-LmHrlalIVUStU6nqBJI" : "Blaya - Cash",
"-LmHrmRRXy4UYoF7DNZz" : "Taylor Swift - You Need to Calm Down"
}
},
在我的应用程序中,我能够使用哈希映射将每个唯一的歌曲与计数器相对应。现在,在Firebase RecyclerView Adapter中,它只显示数据库中的最后一个条目。以下是代码:
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();
我希望在RecyclerView中不仅显示值为"Rick Ross - Gold Roses (feat.Drake)",还要显示下面的第一条目录下有3个出现次数的"Ginuwine - So Anxious"。这已经在我的songCounts HashMap中计算和处理过了。谢谢!
编辑添加了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>
编辑:添加了数据插入方式
FirebaseDatabase.getInstance().getReference().child(djName).child("song").push().setValue(songName);
R.layout.djname_item
怎么样(我会怀疑这个)? - Martin Zeitler