Jonathan Hoffmann
Jonathan Hoffmann

Reputation: 3

RecyclerView setLayoutManager() cannot be resolved

So I am following this tutorial: https://www.youtube.com/watch?v=18VcnYN5_LM

Everything working great until the very end, where he sets the layout manager. Android Studio tells me, that this method cannot be resolved. Since this seems to be used as well in other tutorials and even on the official page: https://developer.android.com/guide/topics/ui/layout/recyclerview I wonder what I did wrong. I guess that I forgot something somewhere, but I cannot figure out what. Already watched the video 3 times.

Would be great, if someone could help me.

My code:

Main Activity

package com.hkr.Views;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.hkr.R;
import com.hkr.Views.RecyclerViewDevices.DevRecViewAdapter;

public class MainActivity extends AppCompatActivity {
    String stringDevices[];
    RecyclerView devices;

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


        stringDevices = getResources().getStringArray(R.array.devices); //Test devices in String ressource file. Replace with json!
        devices = findViewById(R.id.RecyclerViewDevices);

        DevRecViewAdapter adapter = new DevRecViewAdapter(this, stringDevices);
        devices.setAdapter(adapter);
        adapter.setLayoutManager(new LinearLayoutManager(this));
    }
}

Main Activity XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".Views.MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/RecyclerViewDevices"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="157dp"
        tools:layout_editor_absoluteY="240dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

device_row.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" 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_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/DevName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Device Name"
                android:textSize="24sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

DevRecViewAdapter

package com.hkr.Views.RecyclerViewDevices;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hkr.R;

public class DevRecViewAdapter extends RecyclerView.Adapter<DevRecViewAdapter.DevViewHolder> {

    String names[];
    Context c;

    public DevRecViewAdapter(Context context, String devicenames[])
    {
        c=context;
        names=devicenames;
    }

    @NonNull
    @Override
    public DevViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(c);
        View view = inflater.inflate(R.layout.device_row, parent, false);
        return new DevViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull DevViewHolder holder, int position) {
        holder.DevName.setText(names[position]);

    }

    @Override
    public int getItemCount() {
        return names.length;
    }


    public class DevViewHolder extends RecyclerView.ViewHolder{

        TextView DevName;

        public DevViewHolder(@NonNull View itemView) {
            super(itemView);
            DevName = itemView.findViewById(R.id.DevName);
        }
    }
}

Upvotes: 0

Views: 1996

Answers (1)

insa_c
insa_c

Reputation: 2931

The adapter doesn't have the layout manager, the RecyclerView does so you need to set the layout manager to the RV

LinearLayoutManager layoutManager = LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL) // or vertical whatever you want
device.setLayoutManager(layoutManager);

should work

you can also set the layout manager on the xml file using

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:orientation="horizontal"

on the recycler view tag

Upvotes: 3

Related Questions