Reputation: 420
After coding a recyclerView MediaPlayer, the item lists refused to display after testing with emulator. I don't know what to do. Please your help is very important to me. All the codes below is what i tried that didn't work.
This is the MainActivity.java
public class MainActivity extends AppCompatActivity {
private ArrayList<Music> arrayList;
private CustomMusicAdapter adapter;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerViewList);
arrayList = new ArrayList<>();
arrayList.add(new Music("001 Music", "Salutatio Actus paenitentialis", R.raw.adoremus_snctus));
arrayList.add(new Music("002 Music", "Collecta, Liturgia Verbi (Prima & Secunda)", R.raw.adoremusl_collecta_liturgia_verbi));
arrayList.add(new Music("003 Music", "Evangelium (Alleluia)", R.raw.evangelium_alleluia));
arrayList.add(new Music("004 Music", "Evangelium (Responses)", R.raw.evangelium_responses));
arrayList.add(new Music("005 Music", "Oratio universalis, Oratio super oblata", R.raw.oratio_universalis));
arrayList.add(new Music("006 Music", "Prex Eucharistica (two versions)", R.raw.prex_eucharistica));
arrayList.add(new Music("007 Music", "Praefatio (Sanctus)", R.raw.praefatio_sanctus));
arrayList.add(new Music("008 Music", "Acclamans (Mortem tuam)", R.raw.acclamans_mortem_tuam));
arrayList.add(new Music("009 Music", "Elevans, Ritus Communionis (Pater Noster)", R.raw.pater_noster));
arrayList.add(new Music("010 Music", "Ritus Pacis", R.raw.ritus_pacis));
arrayList.add(new Music("011 Music", "Oratio post communionem, Benedicto, Dimissio", R.raw.dimissio));
arrayList.add(new Music("012 Music", "Penitential Act, Kyrie-Christe-Kyrie", R.raw.aud_3));
arrayList.add(new Music("013 Music", "", R.raw.aud_3));
arrayList.add(new Music("014 Music", "", R.raw.aud_3));
arrayList.add(new Music("015 Music", "", R.raw.aud_3));
arrayList.add(new Music("016 Music", "", R.raw.aud_3));
adapter = new CustomMusicAdapter(this, R.layout.custom_music_row, arrayList);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(false);
}
}
the cutom_music_row
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="7dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/ivMusic"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:src="@drawable/ic_lmusic" />
<TextView
android:id="@+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="48dp"
android:text="Song List"
android:textColor="#16c15b"
android:textSize="22sp"
android:layout_marginLeft="30dp" />
<TextView
android:id="@+id/txtSinger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/textName"
android:layout_below="@+id/textName"
android:text="Singer"
android:textColor="#645506"
android:textSize="18sp"
android:layout_alignLeft="@+id/textName" />
<ImageView
android:id="@+id/ivPlay"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_toLeftOf="@id/ivStop"
android:layout_marginRight="20dp"
android:src="@drawable/ic_play"/>
<ImageView
android:id="@+id/ivStop"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/stop_outline"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
this is MyViewHolder
class MyViewHolder extends RecyclerView.ViewHolder {
ImageView mImageaplay, mImageStop;
TextView mTitle, mDes;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
this.mImageaplay = itemView.findViewById(R.id.ivPlay);
this.mImageStop = itemView.findViewById(R.id.ivStop);
this.mTitle = itemView.findViewById(R.id.textName);
this.mDes = itemView.findViewById(R.id.txtSinger);
}
}
This the CustomMusicAdapter
public class CustomMusicAdapter extends RecyclerView.Adapter<MyViewHolder>{
private Context context;
private int layout;
private ArrayList<Music> myArraylist;
private MediaPlayer mediaPlayer;
private boolean flag=true;
public CustomMusicAdapter(Context context, int custom_music_row, ArrayList<Music> myArraylist) {
this.context = context;
this.layout = layout;
this.myArraylist = myArraylist;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
final View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.custom_music_row, parent, false);
return new MyViewHolder(itemView);
}
@SuppressLint("RecyclerView")
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
holder.setIsRecyclable(false);
final Music music = myArraylist.get(position);
holder.mTitle.setText(music.getName());
holder.mDes.setText(music.getSinger());
//Play Music Setup
holder.mImageaplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (flag){
mediaPlayer=MediaPlayer.create(context,music.getSong());
flag=false;
}
if (mediaPlayer.isPlaying()){
mediaPlayer.pause();
mediaPlayer.start();
holder.mImageaplay.setImageResource(R.drawable.ic_play);
}else {
mediaPlayer.start();
holder. mImageaplay.setImageResource(R.drawable.ic_pause);
}
mediaPlayer.start();
}
});
// stop player
holder.mImageStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!flag){
mediaPlayer.stop();
mediaPlayer.release();
flag=true;
}
holder.mImageaplay.setImageResource(R.drawable.ic_play);
}
});
}
@Override
public int getItemCount() {
return myArraylist.size();
}
public class ViewHolder {
TextView textView_SongName, textView_artist;
ImageView imageView_play, imageView_stop;
private View convertview;
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
public ViewHolder() {
convertview = layoutInflater.inflate(layout, null);
textView_SongName = convertview.findViewById(R.id.textName);
textView_artist = convertview.findViewById(R.id.txtSinger);
imageView_play = convertview.findViewById(R.id.ivPlay);
imageView_stop = convertview.findViewById(R.id.ivStop);
}
}
}
This is the model class Music.java
public class Music {
private String name;
private String singer;
private int song;
public Music(String name, String singer, int song) {
this.name = name;
this.singer = singer;
this.song = song;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSinger() {
return singer;
}
public void setSinger(String singer) {
this.singer = singer;
}
public int getSong() {
return song;
}
public void setSong(int song) {
this.song = song;
}
}
Upvotes: 0
Views: 52
Reputation: 1421
You pass myArrayList
as an argument to CustomMusicAdapter
, but do nothing with it in the constructor. Therefore myArrayList
remains null
in the adapter class.
public CustomMusicAdapter(Context context, int custom_music_row, ArrayList<Music> myArraylist) {
this.context = context;
this.layout = layout;
this.myArraylist = myArraylist; // Add this line
}
As pointed out here you also need to set the LayoutManager in your MainActivity's onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
adapter = new CustomMusicAdapter(this, R.layout.custom_music_row, arrayList);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this)); // Add this line
recyclerView.setHasFixedSize(false);
}
If it still does not work, you might need to append your MainActivity's xml file as well as the gradle file.
Do keep in mind though that this is a forum to ask specific questions and not a code review platform.
Some general tips:
layout
variable adds nothing to your code and can be removed. Same for the parameter custom_music_row
public CustomMusicAdapter(Context context, int custom_music_row, ArrayList<Music> myArraylist) {
this.context = context;
this.layout = layout; // remove this
this.myArraylist = myArraylist;
}
public class ViewHolder {
TextView textView_SongName, textView_artist;
ImageView imageView_play, imageView_stop;
private View convertview;
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
public ViewHolder() {
convertview = layoutInflater.inflate(layout, null);
textView_SongName = convertview.findViewById(R.id.textName);
textView_artist = convertview.findViewById(R.id.txtSinger);
imageView_play = convertview.findViewById(R.id.ivPlay);
imageView_stop = convertview.findViewById(R.id.ivStop);
}
}
Upvotes: 1
Reputation: 391
check this
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Upvotes: 1