Recycler View Part - II

Passing your list data to recycler view!

In Part I we just created our very first recycler view which had 30 elements. We made our own RecyclerItem that had a TextView where we populated order of the item from 1 to 30.

So now let's see how can we pass our own data into the RecyclerView.

You can continue to edit your previous SimpleRecyclerViewAdapter

Steps

1. Creating ArrayList of String

In your MainActivity, create a global ArrayList of String as

ArrayList<String> daysData = new ArrayList<>();

Now inside onCreate() method add some random data in the daysData as

daysData.add("Sunday");
daysData.add("Monday");
daysData.add("Tuesday");
daysData.add("Wednesday");
daysData.add("Thursday");
daysData.add("Friday");
daysData.add("Saturday");

I've created a ArrayList of days of week

Now what we aim to is, show this data in our RecyclerView. But to show these data, we need to first pass this data. But again to pass this data we need to actually make arrangement in our adapter to make it able to accept our data.

In simple words, to pass data from activity to adapter, you need a constructor in the adapter to accept the data

2. Editing the Adapter to accept data

Now, in our SimpleRecyclerViewAdapter let's create another global variable of ArrayList that will hold the incoming daysData so that we can use it everywhere in our adapter.

Also let's add create a constructor in our SimpleRecyclerAdapter

 ArrayList<String> recyclerData = new ArrayList<>();

 SimpleRecyclerAdapter(ArrayList<String> receivedData){
      recyclerData = receivedData
 }

Now finally in your MainActivity, change adapter initialization with the following code

adapter = new SimpleRecyclerViewAdapter(daysData);

So let's stop and look back what is happening

  1. We made a list of days in our MainActivity as daysData
  2. We needed to pass that list of string to our Adapter but we could not.
  3. For the SimpleRecyclerViewAdapter to accept list from MainActivity we needed a constructor that accepted list of string
  4. But if the constructor receives the data, the data is bound within the constructor only, we cannot use that data anywhere else. So we made a variable as recyclerData
  5. Now for our recyclerData we assigned with the incoming receivedData so now the list we passed from MainActivity is easily useable in our whole SimpleRecyclerViewAdapter
  6. We passed the daysData to the adapter.

3. ItemCount

Now we actually have some finite data in the adapter, so let's return recyclerData's size rather than returning a random value of 30.

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

4. onBind

Now lets populate each days to our RecyclerViewItem in our onBindViewHolder as

 @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
                  holder.textView.setText(recyclerData.get(position).toString());
        }

This method will iterate through EACH data item and create a new DAY entry in the item

That's It. Once you build the project you should see List of Days in your output.

Total Adapter Code

public class SimpleRecyclerViewAdapter
        extends
        RecyclerView.Adapter<SimpleRecyclerViewAdapter.ViewHolder> {

    ArrayList<String> recyclerData = new ArrayList<>();

    SimpleRecyclerViewAdapter(
            ArrayList<String> receivedData) {
        recyclerData = receivedData;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(
                LayoutInflater.from(
                        parent.getContext())
                        .inflate(
                                R.layout.layout_recycler_item,
                                parent,
                                false
                        )
        );
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.textView.setText(recyclerData.get(position));

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;

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

Total Main Activity Code

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    LinearLayoutManager linearLayoutManager;
    SimpleRecyclerViewAdapter adapter;


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

        ArrayList<String> daysData = new ArrayList<>();

        daysData.add("Sunday");
        daysData.add("Monday");
        daysData.add("Tuesday");
        daysData.add("Wednesday");
        daysData.add("Thursday");
        daysData.add("Friday");
        daysData.add("Saturday");

        recyclerView = findViewById(R.id.recycler_view);
        linearLayoutManager = new LinearLayoutManager(this,RecyclerView.VERTICAL, false);
        adapter = new SimpleRecyclerViewAdapter(daysData);

        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(linearLayoutManager);

    }


}

Assignment

Please upload this working project as assignment of recycler-view-part-II