0% found this document useful (0 votes)
9 views2 pages

Android Online Lab 4

This document outlines the steps to set up a RecyclerView in an Android project. It includes instructions for creating the layout, initializing the RecyclerView and its adapter, and implementing the necessary Java classes and methods. The final goal is to display a list of strings in the RecyclerView, which should be demonstrated to a professor after running the code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Android Online Lab 4

This document outlines the steps to set up a RecyclerView in an Android project. It includes instructions for creating the layout, initializing the RecyclerView and its adapter, and implementing the necessary Java classes and methods. The final goal is to display a list of strings in the RecyclerView, which should be demonstrated to a professor after running the code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Online Lab 4: RecyclerView

Project Set-up:
1. Create a new Android project with an empty activity
2. Go to activity_main.xml
3. Find the RecyclerView widget and drag it onto the layout, don’t forget to add constraints and
give it the id rView
4. Create a new XML file (File -> New -> XML -> XML Layout) and call it list_layout
5. Open list_layout.xml
6. Set the layout_width and layout_height of the layout to “wrap_content”
7. Add a TextView with id txtList Java:
8. Open MainActivity.java 9. Add the following:
RecyclerViewrecyclerView;
RecyclerView.AdaptermAdapter;
RecyclerView.LayoutManager layoutManager;
List<String> myDataset = new ArrayList<>();
10. Inside onCreate: initialize the ArrayList myDataset and fill it with some sample strings (for
example, “Bill Gates” and “Steve Jobs”) 11. Add the following code snippet:
reqclerView = findViewByid (R. id. rViewj ;
layoutMa.nager = new LinearLayoutManager( context this);
recyclerView.setLayoutManager(layoutl~anager);
This will get a reference to the RecyclerView we placed earlier. The RecyclerView uses something
called a “LayoutManager”. This determines how the RecyclerView should look. The
LineaerLayoutManager will make each entry appear in a single column. GridLayoutManager would
create a grid (good for 2-dimensional arrays)

12. Add the following snippet:


ml>.dapter = new MyAdapter(nr1DatasetJ;
recycle?View.setAdapter(mA.dapter);
The RecyclerView needs an Adapter which works with the data. When you construct the adapter it
takes some type of list based data, such as an ArrayList or an Array. We are passing the adapter our
ArrayList of strings.

13. Create a new Java class called MyAdapter which inherits from
RecyclerView.Adapter<MyAdapter.MyViewHolder>

14. Add the rest of the snippets inside the MyAdapter class:
List<String>mDataset;

11yAclapter(List<String> mylJataset) {
mDataset = myDataset;
}
The Adapter should have some type of dataset as a member variable. The constructor will take a
dataset as a parameter and set the member variable. Recall that we passed in the ArrayList of names
to the MyAdapter constructor earlier.

15. Add the following snippet:


static class MyViewHolder extends RecyclerView.ViewHolder {

TextView textView;
MyViewHolder{Layoutinflater inflater, ViewGroup parent) {
super{inflater.inflate(R,layout.l.ist_layout, parent, attachToRoobfalse));
textView = itemView,findViewByld{R.id.t:xtList);

The Adapter should have a ViewHolder as an inner class. The ViewHolder does what it says (it holds
the View that we want each list item to display). In this case we are displaying a single TextView. We
start by inflating the list_layout.xml file. Then we get a reference to the txtList TextView we created.
If we wanted to display images, checkboxes, or other widgets we would get references here.

16. Add this snippet:


@override
public MyAdapter.MyViewHolder onCreateViewHolder{ViewGroup parent, int viewType)
Layoutlnflater layoutlnflater = Layoutinflater.from{parent.getContext{));
return new MyViewHolder{layoutinflater, parent);

Android will create new views depending on memory and how much the user scrolls. In order to
create a view, it needs a LayoutInflater, which requires a context.

17. Add this snippet:


@override
public void onBindViewHolder{MyViewHolder holder, int position) {
holder.textView.setText(mDataset.get{position));

@override
public int getitemCount()
return mDataset.size{);

The onBindViewHolder is very important. Here you will set values of the widgets based on the array.
Basically as the user scrolls through the list, the TextView will get different text based on the position
in the ArrayList. So at the top it is getting from position 0, which will be the first string we entered in
our ArrayList.

18. Try running the code. It should display a list with all the strings you added earlier. Show the
result to your professor.

You might also like