I just created a simple RecyclerView
project which will show the name of fruits. But when i run the file it is not showing anything. In fact my whole source code is just fine.
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"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java
package com.fruits.myrecyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
String[] fruits = {"Apple", "Mango", "Pineapple", "Orange"};
FruitsAdapter fruitsAdapter;
RecyclerView recyclerView;
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
fruitsAdapter = new FruitsAdapter( fruits );
recyclerView = findViewById( R.id.recycler_view );
recyclerView.setAdapter( fruitsAdapter );
}
}
FruitsAdapter.java
package com.fruits.myrecyclerview;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class FruitsAdapter extends RecyclerView.Adapter<FruitsAdapter.FruitsView>{
String[] fruits;
public FruitsAdapter( String[] fruits){
this.fruits = fruits;
}
public class FruitsView extends RecyclerView.ViewHolder{
TextView textView;
public FruitsView( View view ){
super(view);
textView = (TextView) view.findViewById( R.id.fruit_list );
}
}
@Override
public FruitsView onCreateViewHolder( @NonNull ViewGroup parent, int viewType ) {
LinearLayout linearLayout = (LinearLayout) LayoutInflater
.from( parent.getContext() )
.inflate( R.layout.fruites_list, parent, false );
return new FruitsView( linearLayout );
}
@Override
public void onBindViewHolder( @NonNull FruitsView holder, int position ) {
holder.textView.setText( fruits[position] );
}
@Override
public int getItemCount( ) {
return fruits.length;
}
}
fruites_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/linear_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/fruit_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="30dp"
android:gravity="center"
android:text="Something"
android:textColor="@android:color/holo_blue_dark"/>
</LinearLayout>
You're missing the LayoutManager, add
recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));