Alxa
Alxa

Reputation: 79

How can I show an array using RecyclerView?

class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private final int listSize;
    private String[] list;

    public MyAdapter(int listSize, String[] list) {
        this.listSize = listSize;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.rv_item,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.bind(position);
    }

    @Override
    public int getItemCount() {
        return listSize;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView item;
        Button button;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            item = itemView.findViewById(R.id.item_view);
            button = itemView.findViewById(R.id.button_item_view);
        }
        void bind(int index) {
            button.setText(String.valueOf(index));
            item.setText(list[index]);
        }
    }
}

In this adapter class I want to show a String array. The only problem is that the first element (with zero index) is missed on the screen, despite the fact that element exist beyond the screen (Sorry for such a big picture).

enter image description here

The buttons must be numbered with zero, so should the array be printed starting with zero element. How can I resolve this problem?

UPD: Here's the layout:

rv_item

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/item_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|start"
        android:text="TextView"
        android:textSize="40sp" />

    <Button
        android:id="@+id/button_item_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|end"
        android:layout_marginEnd="48dp"
        android:padding="10dp"
        android:text="INFO" />
</FrameLayout>

activity_top, in which recyclerView widget is used

<?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=".TopLevelActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/listOfGoods"
        android:layout_width="409dp"
        android:layout_height="729dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:listitem="@layout/rv_item" />
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

Views: 78

Answers (2)

chand mohd
chand mohd

Reputation: 2550

Do not give fixed height of recyclerView

Correct Way:

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/listOfGoods"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:listitem="@layout/rv_items" />

P.S: I have taken your example and run the app on api 29 work fine cheers! Also Do not make it listSize final, compiler will give error as you are not initializing

Upvotes: 1

isthemartin
isthemartin

Reputation: 126

I know that it could be the same, but I would like to corroborate, try to change this code:

@Override
public int getItemCount() {
    return listSize;
}

For this:

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

One more thing: try to debug this object this.list = list; and check if the array contains zero index element. If the array contains that element, probably it should something about layout as mentioned @Rui Alves.

Upvotes: 1

Related Questions