D
D
DeNissss44442020-11-05 16:11:33
Android
DeNissss4444, 2020-11-05 16:11:33

Why doesn't adapter pass content to RecyclerView?

I wrote a regular program in which there is a RecyclerView. I don’t see any errors in Android Studio, but when I run the program in RecyclerView there is only a layer layout and there is no information itself. Tell me what I did wrong?

Main class

package com.example.recycleview;

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

import android.os.Bundle;
import android.widget.LinearLayout;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager layoutManager;

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

        ArrayList<ExampleItem> arrayList = new ArrayList<>();
        arrayList.add(new ExampleItem(R.drawable.ic_android,
                "Line example 1","Line example 2"));
        arrayList.add(new ExampleItem(R.drawable.ic_baseline_add_a_photo,
                "Line example 1","Line example 2"));
        arrayList.add(new ExampleItem(R.drawable.ic_baseline_backup,
                "Line example 1","Line example 2"));

        recyclerView = (RecyclerView) findViewById(R.id.recycleId);
        recyclerView.setHasFixedSize(true); // проверить что делает

        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        mAdapter = new AdapterExample(arrayList);
        recyclerView.setAdapter(mAdapter);
    }
}

Its XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycleId"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="149dp"
        tools:layout_editor_absoluteY="176dp"
        android:scrollbars="vertical"
        android:background="@android:color/darker_gray"
        />
</RelativeLayout>

Item class
package com.example.recycleview;

public class ExampleItem {
    private int mImageResourse;
    private String title;
    private String description;

    public ExampleItem (int mImageResourse,String title,String description){
        mImageResourse = mImageResourse;
        title = title;
        description = description;
    }
    public int getmImageResourse(){
        return mImageResourse;
    }

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }
}

CardView XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.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="4dp">

    <RelativeLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent">

        <ImageView
            android:id="@+id/iconId"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:padding="2dp"
            />

        <TextView
            android:id="@+id/titleId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="80dp"
            android:layout_marginTop="3dp"
            android:text="Line example 1"
            android:textColor="@color/black"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/descriptionId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/titleId"
            android:layout_alignParentStart="true"
            android:layout_marginStart="81dp"
            android:layout_marginTop="0dp"
            android:text="Line example 2" />

   </RelativeLayout>
</androidx.cardview.widget.CardView>

And here is the Adapter class
package com.example.recycleview;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

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

import java.util.ArrayList;

public class AdapterExample  extends RecyclerView.Adapter<AdapterExample.ExampleViewHolder> {
    private ArrayList<ExampleItem> mExampleList;

    public static class ExampleViewHolder extends RecyclerView.ViewHolder {
        public ImageView mImageResourse;
        public TextView title;
        public TextView description;

        public ExampleViewHolder(@NonNull View itemView) {
            super(itemView);
            mImageResourse = itemView.findViewById(R.id.iconId);
            title = itemView.findViewById(R.id.titleId);
            description = itemView.findViewById(R.id.descriptionId);
        }
    }
    public AdapterExample(ArrayList<ExampleItem> exampleList){
        mExampleList = exampleList;
    }
    @NonNull
    @Override
    // Получаем нужный нам лейаут
    public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item_view,
                parent, false);
        ExampleViewHolder evh = new ExampleViewHolder(v);
        return evh;
    }

    @Override
    public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
        ExampleItem currentItem = mExampleList.get(position);
        // Что бы получить доступ к картинке
        // 1.Обращаемся к Холдеру (помошник) потом к переменой вверху,и просим получить картинку
        // 2. А дальше отдаем эти данные
        holder.mImageResourse.setImageResource(currentItem.getmImageResourse());
        holder.title.setText(currentItem.getTitle());
        holder.description.setText(currentItem.getDescription());
    }

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DeNissss4444, 2020-11-05
@DeNissss4444

All figured out)) It was necessary to add this to ExampleItem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question