Search code examples
javaandroidandroid-activityandroid-recyclerview

RecyclerView list not showing anything after run the app


My app is supposed to take a string array and view it in RecyclerView. Everything seems ok but When I run the app it's showing nothing!
Firstly I declare a String array in String.xml file with name "array_countries"
This string file

<resources>
<string name="app_name">StringArrayWithRecyclerView</string>

<string-array name="array_countries">
    <item>Australia</item>
    <item>Canada</item>
    <item>Cambodia</item>
    <item>China</item>
    <item>England</item>
    <item>France</item>
    <item>Japan</item>
    <item>Rusia</item>
    <item>USA</item>
</string-array>

The countries_row.xml

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Dummy"
        android:textSize="30sp"
        android:textColor="@color/black"
        />

</LinearLayout>

then I create the CountriesAdapter

    package pro.cs_is.com.stringarraywithrecyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Collections;
import java.util.List;


public class CountriesAdapter extends RecyclerView.Adapter<CountriesAdapter.CountriesHolder> {

    private List<String> list = Collections.emptyList();
    private Context context;

    public CountriesAdapter(Context context, List list) {
        this.context = context;
        this.list = list;

    }

    @Override
    public CountriesHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.countries_row, parent, false);
        CountriesHolder holder = new CountriesHolder(view);
        return holder;

    }

    @Override
    public void onBindViewHolder(final CountriesHolder holder, int position) {
        String country = list.get(position);
        holder.textView.setText(country);
        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast toast = Toast.makeText(context, "You clicked " +
                        holder.textView.getText(), Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        });

    }

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


    class CountriesHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public CountriesHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.textView);
        }
    }
}

and finally the MainActivity

    package pro.cs_is.com.stringarraywithrecyclerview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private CountriesAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        List<String> countries = Arrays.asList(getResources().getStringArray(R.array.array_countries));
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        adapter = new CountriesAdapter(this, countries);
        adapter.notifyDataSetChanged();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        DividerItemDecoration dividerItemDecoration =
                new DividerItemDecoration(recyclerView.getContext(), linearLayoutManager.getOrientation());
        recyclerView.addItemDecoration(dividerItemDecoration);
        recyclerView.setAdapter(adapter);


    }
}

Solution

  • You forgot to add

    recyclerView.setLayoutManager(linearLayoutManager);