gpt4 book ai didi

java - 在 Activity 中发送和添加回收器 View 数据时遇到问题

转载 作者:行者123 更新时间:2023-12-01 19:52:38 25 4
gpt4 key购买 nike

我试图开发一个音乐应用程序,所以我有两个 Activity ,例如 ArtistActivityFavoriteActivity 。我的ArtistActivity包含 RecyclerView加载 SongName , SongUrl , SongArtistListView它从 Firebase 加载数据,并且对于每个项目,项目末尾都有一个心形爆炸图标。我的主要问题是,当我单击项目时,我想在其他 Activity 中发送单击的项目,这是我的 FavoriteActivity我想在单击项目时播放歌曲 FavoriteActivity 。因此,为此我需要管理 SongUrlFavoriteActivity我对这个话题感到困惑。

例如:

当我们点击Add to favorite in some apps时你可以在我的应用程序中以 Spotify 为例。我想构建相同的功能,我在下面提供了我的代码。因此,如果您对我的问题有任何答案,请回答我。

这是我的代码:

我的适配器类:

package com.geetmp3.GeetMp3.ArtistPanel;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.geetmp3.GeetMp3.R;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import xyz.hanks.library.bang.SmallBangView;

public class PanelAdapter extends RecyclerView.Adapter<PanelAdapter.PanelViewHolder> {


private Context mContext;
private List<PanelList> mUploads;


public PanelAdapter(Context context, List<PanelList> panelList) {

mContext = context;
mUploads = panelList;

}

@NonNull
@Override
public PanelViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.artist_recycler_layout, parent, false);
return new PanelViewHolder(v);

// Toast.makeText(mContext, "" + ge, Toast.LENGTH_SHORT).show();

}

@Override
public void onBindViewHolder(@NonNull PanelViewHolder holder, int position) {

PanelList panelList = mUploads.get(position);


holder.textViewName.setText(panelList.getSongName());

Glide.with(mContext)
.load(panelList.getSongImageUri())
.centerCrop()
.into(holder.circleImageView);
//

holder.bangView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (holder.bangView.isSelected()) {
holder.bangView.setSelected(false);

// What can i do here


} else {
holder.bangView.setSelected(true);
holder.bangView.likeAnimation(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
Toast.makeText(mContext, mUploads.get(position).getSongName() + "Added to playlist", Toast.LENGTH_SHORT).show();
}
});
}
}
});


}

@Override
public int getItemCount() {
return mUploads.size();
}

public class PanelViewHolder extends RecyclerView.ViewHolder {

private TextView textViewName;
private CircleImageView circleImageView;
private LinearLayout thisLayout;

private SmallBangView bangView;


public PanelViewHolder(@NonNull View itemView) {
super(itemView);

thisLayout = itemView.findViewById(R.id.artist_linear);
textViewName = itemView.findViewById(R.id.artist_song_name);
circleImageView = itemView.findViewById(R.id.artist_songs_image);
bangView = itemView.findViewById(R.id.bang_like_heart);


}

}
}

我的 getter 和 setter 类:

public class PanelList {


private String SongName;
private String SongUrl;
private String SongImageUri;
private String SongLyrics;
private String SongMusicDirector;
private String SongProducer;
private String SongArtist;

public PanelList() {

}

public PanelList(String SongName, String SongUrl, String SongImageUri, String SongArtist, String SongLyrics, String SongMusicDirector, String SongProducer) {
this.SongName = SongName;
this.SongUrl = SongUrl;
this.SongImageUri = SongImageUri;
this.SongArtist = SongArtist;
this.SongLyrics = SongLyrics;
this.SongMusicDirector = SongMusicDirector;
this.SongProducer = SongProducer;
}


public String getSongArtist() {
return SongArtist;
}


public String getSongLyrics() {
return SongLyrics;
}


public String getSongMusicDirector() {
return SongMusicDirector;
}


public String getSongProducer() {
return SongProducer;
}


public String getSongName() {
return SongName;
}


public String getSongUrl() {
return SongUrl;
}


public String getSongImageUri() {
return SongImageUri;
}


}

我的艺术家 Activity :

public class ArtistActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private ValueEventListener eventListener;
private DatabaseReference mDataRef, artist_ref;

private List<PanelList> mList;
private PanelAdapter panelAdapter;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.artist1layout);

mRecyclerView = findViewById(R.id.artist_song_recyclerview);
mDataRef = FirebaseDatabase.getInstance().getReference("ArtistView").child(getIntent().getStringExtra("Artist"))
.child("Songs");


mRecyclerView.setHasFixedSize(true);

mDataRef.keepSynced(true);

mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

mList = new ArrayList<>();

panelAdapter = new PanelAdapter(ArtistActivity.this, mList);

mRecyclerView.setAdapter(panelAdapter);

eventListener = mDataRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mList.clear();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
PanelList list = postSnapshot.getValue(PanelList.class);
mList.add(list);


}

panelAdapter.notifyDataSetChanged();
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}
}

我的FavoriteActivity里什么都没有除了onCreate .

最佳答案

如果列表是动态的,请勿在适配器中传递列表。

在您的适配器中添加此功能

public void setPanelList(List<PanelList> panelList){

panelList = mList;
notifyDataSetChanged();
}

修改您的mDataRef.addValueEventListener

   @Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

mList.clear();

for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {

PanelList list = postSnapshot.getValue(PanelList.class);
mList.add(list);
}

panelAdapter.setPanelList(mList);
}

希望这有帮助。

关于java - 在 Activity 中发送和添加回收器 View 数据时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59073474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com