Android RecyclerView - Fill With Array

November 29, 2017 Oclemy Android RecyclerView, Java Array 2 minutes, 13 seconds

This is a simple RecyclerView tutorial.Here is what we do:

  • Fill RecyclerView with data from a simple Array.
  • We derive from RecycelView.ViewHolder to get our custom viewholder class.
  • We aslo derive from RecyclerView.Adapter to get our adapter class that helps us bind our data to our RecyclerView.
  • We create a model layout using cardview that's inflated to a view item for our recyclerview.

1.Our View Holder Class

  • Holds our views fro recycling.

 

package com.tutorials.hp.recyclerviewarray;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

public class MyViewHolder extends RecyclerView.ViewHolder {

    TextView nameTxt;

    public MyViewHolder(View itemView) {
        super(itemView);

        nameTxt = (TextView) itemView.findViewById(R.id.nameTxt);

    }

}

2.Our Adapter Class

  • Responisble for layout inflation.
  • Initializes our View holder.
  • Binds data to our views.

 

package com.tutorials.hp.recyclerviewarray;

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

public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

    Context c;
    String[] spacecrafts;

    public MyAdapter(Context c, String[] spacecrafts) {
        this.c = c;
        this.spacecrafts = spacecrafts;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(c).inflate(R.layout.model, parent, false);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        //BIND DATA
        holder.nameTxt.setText(spacecrafts[position]);

    }

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

1.Our MainActivity

  • Launcher activity.
  • References RecyclerView and sets its layout manager.
  • Instantiates and sets our adapter to RecyclerView.

 

package com.tutorials.hp.recyclerviewarray;

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

public class MainActivity extends AppCompatActivity {

    RecyclerView rv;
    String[] spacecrafts={"Juno","Hubble","Casini","WMAP","Spitzer","Pioneer","Columbia","Challenger","Apollo","Curiosity"};

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

        rv = (RecyclerView) findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(this));

        //ADAPTER
        MyAdapter adapter = new MyAdapter(this, spacecrafts);
        rv.setAdapter(adapter);
    }
}

  2. Our Model XML Layout

  • A single cardview in our recyclerview.
  • Shall be inflated to a view item.

 

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_margin="10dp"
    card_view:cardCornerRadius="5dp"
    card_view:cardElevation="5dp"
    android:layout_height="200dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Name"
                android:id="@+id/nameTxt"
                android:padding="10dp"
                android:textColor="@color/colorAccent"
                android:textStyle="bold"
                android:layout_alignParentLeft="true"
                />

</android.support.v7.widget.CardView>

Comments