Home > Software engineering >  Recycler View is not displaying items
Recycler View is not displaying items

Time:08-15

I have been working on a Java Project where I need to display multiple rows with different view items (images and textviews). I have created Adapter Class as well where the images and texts are setting to the textviews and images. But nothing happens and there is not any error displayed either. Following are the codes of my project:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".MainActivity"
    android:background="@drawable/ic_bg"
    android:orientation="vertical">

    <ImageView
        android:id="@ id/imageView"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:src="@drawable/ic_action_bar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:contentDescription="@string/action_bar" />

    <androidx.recyclerview.widget.RecyclerView
        android:background="@color/white"
        android:id="@ id/recylerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="24dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="24dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/imageView" />


</LinearLayout>

item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:layout_marginStart="12dp"
    android:layout_marginTop="12dp"
    android:layout_marginEnd="12dp"
    android:orientation="vertical">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@ id/playerRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="12dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@ id/rank"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginStart="4dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:srcCompat="@tools:sample/avatars" />

        <ImageView
            android:id="@ id/player_profile"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_marginStart="12dp"
            android:layout_marginEnd="24dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@ id/name"
            app:layout_constraintStart_toEndOf="@ id/rank"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0"
            tools:srcCompat="@drawable/ic_profile" />

        <TextView
            android:id="@ id/player_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="36dp"
            android:layout_marginTop="16dp"
            android:text="@string/player_s_name"
            android:textColor="#F8BF2A"
            android:textSize="10sp"
            app:layout_constraintEnd_toStartOf="@ id/arrow"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toEndOf="@ id/player_profile"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="SmallSp" />

        <TextView
            android:id="@ id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:layout_marginBottom="23dp"
            android:text="@string/player_s_name"
            android:textColor="@color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@ id/arrow"
            app:layout_constraintStart_toEndOf="@ id/player_profile"
            app:layout_constraintTop_toBottomOf="@ id/player_name"
            app:layout_constraintVertical_bias="1.0" />

        <ImageView
            android:id="@ id/arrow"
            android:layout_width="20dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="27dp"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="25dp"
            android:contentDescription="arrow"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toEndOf="@ id/name"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/ic_arrow" />

        <TextView
            android:id="@ id/score_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="48dp"
            android:layout_marginTop="16dp"
            android:text="@string/score"
            android:textColor="#F8BF2A"
            android:textSize="10sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toEndOf="@ id/arrow"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="SmallSp" />

        <TextView
            android:id="@ id/total_score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="36dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="32dp"
            android:layout_marginBottom="23dp"
            android:text="@string/gained"
            android:textColor="@color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@ id/arrow"
            app:layout_constraintTop_toBottomOf="@ id/score_id" />


    </androidx.constraintlayout.widget.ConstraintLayout>

    <View
        android:id="@ id/divider"
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="#FFFFFF" />

    <View
        android:id="@ id/divider2"
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:layout_marginBottom="640dp"
        android:background="#7F4242"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/divider" />
</LinearLayout>

Main Activity.java


package com.example.leaderboard;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.Adapter;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        String[] playerNames = {"Alex Wingates", "Wolfe", "Queens", "Adrian Oz", "Timber", "Timber", "Timber", "Timber", "Timber"};
        String[] scores = {"12540","10,706","9,283","9,336","5,124","5,124","5,124","5,124","5,124"};
        int[] ranks={R.drawable.ic_one, R.drawable.ic_two, R.drawable.ic_three, R.drawable.ic_four, R.drawable.ic_five,
                R.drawable.ic_five, R.drawable.ic_five, R.drawable.ic_five, R.drawable.ic_five};
        RecyclerView recyclerView;
        RecyclerView.Adapter mAdapter;
        RecyclerView.LayoutManager layoutManager;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
        recyclerView=findViewById(R.id.recylerView);
        //setting layout

        layoutManager= new LinearLayoutManager(this);
        mAdapter = new ProgramAdapter(this, playerNames, scores, ranks);
        // Finally, attach the adapter with the RecyclerView
        recyclerView.setAdapter(mAdapter);



    }
}

ProgramAdapter.java

package com.example.leaderboard;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

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

public class ProgramAdapter extends RecyclerView.Adapter<ProgramAdapter.ViewHolder> {
    Context context;
    String[] programNameList;
    String[] programDescriptionList;
    int[] images;

    // Create a static inner class and provide references to all the Views for each data item.
    // This is particularly useful for caching the Views within the item layout for fast access.
    public static class ViewHolder extends RecyclerView.ViewHolder{
        // Declare member variables for all the Views in a row
        TextView rowName;
        TextView rowDescription;
        ImageView rowImage;
        // Create a constructor that accepts the entire row and search the View hierarchy to find each subview
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            // Store the item subviews in member variables
            rowName = itemView.findViewById(R.id.name);
            rowDescription = itemView.findViewById(R.id.total_score);
            rowImage = itemView.findViewById(R.id.rank);
        }
    }
    // Provide a suitable constructor
    public ProgramAdapter(Context context, String[] programNameList, String[] programDescriptionList, int[] images){
        // Initialize the class scope variables with values received from constructor
        this.context = context;
        this.programNameList = programNameList;
        this.programDescriptionList = programDescriptionList;
        this.images = images;
    }

    // Create new views to be invoked by the layout manager
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // Create a LayoutInflater object
        LayoutInflater inflater = LayoutInflater.from(context);
        // Inflate the custom layout
        View view = inflater.inflate(R.layout.item_layout, parent, false);
        // To attach OnClickListener
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView rowName = v.findViewById(R.id.player_name);
                Toast.makeText(context, "Clicked Item: "   rowName.getText().toString(), Toast.LENGTH_SHORT).show();
            }
        });
        // Return a new holder instance
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    // Replace the contents of a view to be invoked by the layout manager
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // Get element from your dataset at this position and replace the contents of the View with that element
        holder.rowName.setText(programNameList[position]);
        holder.rowDescription.setText(programDescriptionList[position]);
        holder.rowImage.setImageResource(images[position]);
    }

    // Return the size of your dataset
    @Override
    public int getItemCount() {
        return programNameList.length;
    }
}

LeaderBoard

CodePudding user response:

I ran your code and saw that you are not displaying anything as you have not set the Layout Manager to the recyclerView.

recyclerView.setLayoutManager(layoutManager);

CodePudding user response:

In the Main Activity.java :-

recyclerView.setLayoutManager(layoutManager);

In the item_layout.xml :-

In the last view, remove the layout_marginBottom

If still the problem is not solved then push your code to the github and send me the link to the email "[email protected]".

I'll try to help you out.

  • Related