0% found this document useful (0 votes)
5 views3 pages

MAD Model Answer Q5

The document provides a step-by-step guide to implement a RecyclerView in Android using Java. It includes XML layout for the RecyclerView, Java code for the MainActivity to set up the RecyclerView with a LinearLayoutManager, and an Adapter class to bind data to the RecyclerView items. The example uses a simple string array as the data source for the RecyclerView items.

Uploaded by

kakise5896
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)
5 views3 pages

MAD Model Answer Q5

The document provides a step-by-step guide to implement a RecyclerView in Android using Java. It includes XML layout for the RecyclerView, Java code for the MainActivity to set up the RecyclerView with a LinearLayoutManager, and an Adapter class to bind data to the RecyclerView items. The example uses a simple string array as the data source for the RecyclerView items.

Uploaded by

kakise5896
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
You are on page 1/ 3

Q5. Write Java code to implement a RecyclerView in Android.

Answer:

1. XML for RecyclerView:

<androidx.recyclerview.widget.RecyclerView

android:id="@+id/recyclerView"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

2. Java (MainActivity.java):

RecyclerView recyclerView = findViewById(R.id.recyclerView);

recyclerView.setLayoutManager(new LinearLayoutManager(this));

String[] data = {"Item 1", "Item 2", "Item 3"};

recyclerView.setAdapter(new MyAdapter(data));

3. Adapter Class:

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

private String[] localDataSet;

public MyAdapter(String[] dataSet) {

localDataSet = dataSet;

public static class ViewHolder extends RecyclerView.ViewHolder {

private final TextView textView;


public ViewHolder(View view) {

super(view);

textView = view.findViewById(android.R.id.text1);

public TextView getTextView() {

return textView;

@Override

public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

View view = LayoutInflater.from(viewGroup.getContext())

.inflate(android.R.layout.simple_list_item_1, viewGroup, false);

return new ViewHolder(view);

@Override

public void onBindViewHolder(ViewHolder viewHolder, final int position) {

viewHolder.getTextView().setText(localDataSet[position]);

@Override

public int getItemCount() {

return localDataSet.length;

}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy