RecyclerView does not display items from a list

I tried to create a RecyclerView that displays my songs on my phone from a pre-populated ArrayList. Here is my code for my activity:

public class HomeActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

private RecyclerView songRecyclerView;
private RecyclerView.Adapter songRecyclerAdapter;
private RecyclerView.LayoutManager recyclerLayoutManager;


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

    //get the songs list for the adapter
    ArrayList<Audio> songList;
    StorageUtils storageUtils = new StorageUtils(getApplicationContext());
    songList = storageUtils.loadAudio();


    //Recycler view setup for songs display
    songRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    songRecyclerView.setHasFixedSize(true);

    recyclerLayoutManager = new LinearLayoutManager(this);
    songRecyclerView.setLayoutManager(recyclerLayoutManager);


    songRecyclerAdapter = new SongAdapter(songList);
    songRecyclerView.setAdapter(songRecyclerAdapter);
}

The Audio class has getTitle () and getArtist () methods that work. Loud sound () also works, so there are elements in the song list. Here is the xml of the recyclerview element:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/recycler_item"
    android:layout_width="match_parent"
    android:layout_height="72dp"
    android:visibility="visible">
<TextView
    android:id="@+id/recycler_item_songName"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:text="song name"
    android:textColor="@color/textPrimary"
    android:textSize="16sp"
    android:textStyle="normal"
    android:visibility="visible"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/recycler_item_artistName"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:text="song artist"
    android:textColor="@color/textSecondary"
    android:textSize="16sp"
    android:textStyle="normal"
    android:visibility="visible"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/recycler_item_songName" />

</android.support.constraint.ConstraintLayout>

Here is my adapter implementation:

package com.ecebuc.gesmediaplayer;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

public class SongAdapter extends RecyclerView.Adapter<SongAdapter.ViewHolder> {

    // The dataset
    private ArrayList<Audio> songList;

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView recyclerTitleView, recyclerArtistView;

        public ViewHolder(View itemView) {
            super(itemView);
            this.recyclerTitleView = (TextView) itemView.findViewById(R.id.recycler_item_songName);
            this.recyclerArtistView = (TextView) itemView.findViewById(R.id.recycler_item_artistName);
        }
    }

    // Constructor
    public SongAdapter(ArrayList<Audio> songList){
        this.songList = songList;
    }

    @Override
    public SongAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View listItemView = layoutInflater.inflate(R.layout.song_list_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(listItemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Audio currentSong = songList.get(position);
        holder.recyclerTitleView.setText(currentSong.getTitle());
        holder.recyclerArtistView.setText(currentSong.getArtist());
        Log.d("onBind: ", (String)holder.recyclerTitleView.getText() + (String)holder.recyclerArtistView.getText());
    }

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

}

The failure is that on the very first attempt to create an entire recycleView, it worked and displayed the text. I tried adding imageView as a song cover to the layout of each item in the list, as well as code to display it, and it no longer worked. When you tried to return and had only code for the text, it completely stopped working.

, , - - , , . , . , .

, onBindViewHolder Log.d . getText(). . , @color/text primary textSecondary XML - # ​​212121 # 424242 ( , , ).

StackOverflow , . , . , XML , recyclerView. , :

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".HomeActivity"
    tools:showIn="@layout/app_bar_home">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

, , , , ... , , ? , , ... Layout Outlines

+4
2

, - , , XML, ( , , ) .

, . , XML , , - . , .

+1

, , .

    public Context context;
// Constructor
public SongAdapter(Context context,ArrayList<Audio> songList){
    this.context=context;
    this.songList = songList;
}

, .

SongAdapter adapter;

.

private RecyclerView.Adapter songRecyclerAdapter;

, , .

RecyclerView recyclerView;
SongAdapter songRecyclerAdapter;
private void setAdapter(){
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    if (songRecyclerAdapter==null) {
        songRecyclerAdapter = new SongAdapter(this,songList);
        recyclerView.setAdapter(songRecyclerAdapter);
        songRecyclerAdapter.notifyDataSetChanged();
    } 
    else{
        songRecyclerAdapter.notifyDataSetChanged();
    }
}
0

Source: https://habr.com/ru/post/1695996/


All Articles