• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Setting up a RecyclerView

dwf2008

Newbie
May 18, 2023
11
0
Noobie here.
I am working on my first app using a Recyclerview. I have an Adapter (SongsAdapter) to make it work. This is my code for the Adapter>

package com.example.musiclyrics;

import android.content.Context;
import android.database.Cursor;
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.recyclerview.widget.RecyclerView;

import com.google.android.material.snackbar.Snackbar;

public class SongsAdapter extends RecyclerView.Adapter<SongsAdapter.ViewHolder> {
private Context context;
private Cursor cursor;

public SongsAdapter(Context context) {
this.context = context;
}
public void setCursor(Cursor cursor) {
this.cursor = cursor;

notifyDataSetChanged();
cursor.moveToFirst(); // Move cursor to the first row
ViewHolder holder = onCreateViewHolder(null, 0);

onBindViewHolder(holder, 0);
}

@override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.songs_layout, parent, false);
return new ViewHolder(view);
}

@override
public void onBindViewHolder(ViewHolder holder, int position) {

if (cursor != null && cursor.moveToPosition(position)) {

int songNameIndex = cursor.getColumnIndex("SONG_NAME");
int artistIndex = cursor.getColumnIndex("ARTIST");
int durationIndex = cursor.getColumnIndex("DURATION");
int songIdIndex = cursor.getColumnIndex("KEY_ID");

String songName = cursor.getString(songNameIndex);
String artist = cursor.getString(artistIndex);
String duration = cursor.getString(durationIndex);
final int songId = cursor.getInt(songIdIndex);

holder.songNameTextView.setText(songName);
holder.artistTextView.setText(artist);
holder.durationTextView.setText(duration);

holder.itemView.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
// MainActivity.varCurrentSong = songId;
// Handle the click event
}
});
}
cursor.moveToNext()

}

@override
public int getItemCount() {
return (cursor != null) ? cursor.getCount() : 0;
}

public static class ViewHolder extends RecyclerView.ViewHolder {
TextView songNameTextView;
TextView artistTextView;
TextView durationTextView;

public ViewHolder(View itemView) {
super(itemView);
songNameTextView = itemView.findViewById(R.id.txt_songName);
artistTextView = itemView.findViewById(R.id.txt_Artist);
durationTextView = itemView.findViewById(R.id.txt_Duration);
}
}
}

At runtime this appears to work OK but the screen only shows the first record. I have tried about 50 different ideas and suggestions on how to make this work to no avail. Can someone help me out please?
Thanks
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones