A
A
Anton Loginov2020-10-27 21:07:01
Java
Anton Loginov, 2020-10-27 21:07:01

Implemented RecyclerView, but it doesn't even get to the onCreateViewHolder method! What could be the reason for this?

Hello dear programmers! The problem is this: I implemented recyclerView, but not a single element appeared on the screen, although it should, there was data for this (They are loaded from FireBase)! With the help of logs, I decided to check which methods work and do not work in the adapter. getItemCount and constructor worked, onCreateViewHolder and onBindViewHolder didn't work. What could be the problem? I spent many hours on this, I did not find the result. I found out that RecyclerView removes Views that are not visible for optimization, but no matter how much I would not steam over xml, nothing happened. I want to accept your helping hand!

Link to GitHub: https://github.com/Olaf-06/SimbirsoftProject.git

DataAdapterSimulators.java

public class DataAdapterSimulators extends RecyclerView.Adapter<ViewHolderSimulators> {

    List<Simulators> simulatorsList;
    LayoutInflater inflater;

    public DataAdapterSimulators(Context context, ArrayList<Simulators> simulatorsList){
        this.simulatorsList = simulatorsList;
        this.inflater = LayoutInflater.from(context);
        Log.d("logmy", "конструктор адаптера");
    }

    @NonNull
    @Override
    public ViewHolderSimulators onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Log.d("logmy", "OnCreateViewHolder");
        View view = inflater.inflate(R.layout.item_simulators, parent, false);
        return new ViewHolderSimulators(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolderSimulators holder, int position) {
        Log.d("logmy", "onBindViewHolder");
        holder.nameOfSimulator.setText(simulatorsList.get(position).name);
        holder.descriptionOfSimulator.setText(simulatorsList.get(position).description);
        FirebaseStorage storage = FirebaseStorage.getInstance();
        StorageReference storageRef = storage.getReferenceFromUrl("gs://simbirsoftproject.appspot.com/" +
                "photoOfUsers").child("simulator" + simulatorsList.get(position).photoID);
        final File localFile;
        try {
            localFile = File.createTempFile("images", "jpg");
            storageRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                    Bitmap bitmap = BitmapFactory.decodeFile(localFile.getAbsolutePath());
                    holder.imgSimulator.setImageBitmap(bitmap);
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    Log.d("logmy", "onFailure: фотка не загрузилась ");
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.d("logmy", "onBindViewHolder: ставлю на вьюшки значения");
     
    }

    @Override
    public int getItemCount() {
        if(simulatorsList == null) {
            Log.d("logmy", "getItemCount: насчитал 0");
            return 0;
        }
        Log.d("logmy", "getItemCount: насчитал несколько");
        return simulatorsList.size();
    }
}

ViewHolderSimulators.java
public class ViewHolderSimulators extends RecyclerView.ViewHolder {

    TextView nameOfSimulator, descriptionOfSimulator;
    ImageView imgSimulator;

    public ViewHolderSimulators(@NonNull View itemView) {
        super(itemView);
        nameOfSimulator = (TextView) itemView.findViewById(R.id.nameOfSimulator);
        descriptionOfSimulator = (TextView) itemView.findViewById(R.id.descriptionOfSimulator);
        imgSimulator = (ImageView) itemView.findViewById(R.id.imgSimulator);
    }
}


fragment_simulators.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FragmentSimulators">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/simulators_recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@drawable/plus" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

item_simulator.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="wrap_content">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp"
        android:orientation="horizontal">

           <ImageView
            android:id="@+id/imgSimulator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:srcCompat="@tools:sample/avatars" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:id="@+id/nameOfSimulator"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Тренажёр"
                android:textSize="20dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/imgSimulator"
                app:layout_constraintTop_toTopOf="parent" />
            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/descriptionOfSimulator"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Описание"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toEndOf="@+id/imgSimulator"
                    app:layout_constraintTop_toBottomOf="@+id/nameSimulator" />
            </ScrollView>
        </LinearLayout>
    </LinearLayout>


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

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2020-10-28
@Olaf-06

You change the data in the adapter, but don't notify it about it (notifyDatasetChanged, notify*, DiffUtil).

A
Anton Loginov, 2020-10-27
@Olaf-06

FragmentSimulators.java

public class FragmentSimulators extends Fragment implements View.OnClickListener {

    RecyclerView RVSimulators;
    ArrayList<Simulators> simulators = new ArrayList<>();
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    FloatingActionButton floatingActionButton;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_simulators, container, false);
        RVSimulators = (RecyclerView) view.findViewById(R.id.simulators_recycler);
        floatingActionButton = (FloatingActionButton) view.findViewById(R.id.fab);
        floatingActionButton.setOnClickListener(this);

        RVSimulators.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        final DataAdapterSimulators adapter = new DataAdapterSimulators(this.getActivity(), simulators);
        RVSimulators.setAdapter(adapter);

        db.collection("simulators")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                Simulators simulatorsClass = document.toObject(Simulators.class);
                                simulators.add(new Simulators(simulatorsClass.name, simulatorsClass.description, simulatorsClass.photoID));
                            }
                       
                            Log.d("logmy", "прогрузились документы");
                        } else {
                            Log.w("logmy", "Error getting documents.", task.getException());
                        }
                    }
                });
        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fab:
                Intent intent = new Intent(FragmentSimulators.this.getContext(), AddSimulator.class);
                startActivity(intent);
                break;
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question