
Example 1
If the JSON data describes an array, and each element of that array is an object:
[
{
"id": 1,
"discipline": "BCS-2E",
"starttime": "08:30",
"endtime": "10:00",
"day": "Monday",
"courseCode": "ENG325",
"courseName": "CPS",
"venue": "Lt3",
"teacherName": "Naeem",
"sessionId": "5",
"sessionName": null
},
{
"id": 103,
"discipline": "BCS-2D",
"starttime": "10:00",
"endtime": "11:30",
"day": "Tuesday",
"courseCode": "CS400",
"courseName": "DBS",
"venue": "Lab6",
"teacherName": "Abdul Rehman",
"sessionId": "5",
"sessionName": null
},
{
"id": 214,
"discipline": "BAI-3A",
"starttime": "11:30",
"endtime": "01:00",
"day": "Wednesday",
"courseCode": "CS632",
"courseName": "AI",
"venue": "Lab6",
"teacherName": "Dr. Sadaf,Rifhat",
"sessionId": "5",
"sessionName": null
},
{
"id": 326,
"discipline": "BIT-8B",
"starttime": "01:30",
"endtime": "03:00",
"day": "Thursday",
"courseCode": "ENG315",
"courseName": "TBW",
"venue": "Lt12",
"teacherName": "Sumaira",
"sessionId": "5",
"sessionName": null
},
{
"id": 439,
"discipline": "BCS-2B",
"starttime": "03:00",
"endtime": "04:30",
"day": "Friday",
"courseCode": "MTH315",
"courseName": "MVC",
"venue": "Lt5",
"teacherName": "Shahid Rasheed",
"sessionId": "5",
"sessionName": null
}
]the JSON Data Set will create a row for each object in the array, and each property on the object will become a column.
code XML Show Recycler View:
| <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".fragments.Admin.Schedule.Reschedule.AdminScheduleRecheduleSelectTimeTableActivity"> <LinearLayout android:layout_width="match_parent" android:layout_marginRight="5dp" android:layout_marginTop="30dp" android:layout_height="wrap_content" android:orientation="vertical">
<androidx.cardview.widget.CardView android:layout_marginLeft="5dp" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/card_view" android:layout_width="match_parent" android:layout_height="wrap_content" card_view:cardCornerRadius="10dp" card_view:cardElevation="5dp" card_view:cardUseCompatPadding="true">
<GridLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:columnCount="2" android:rowCount="2">
<com.google.android.material.imageview.ShapeableImageView android:id="@+id/profile_image_teacher" android:layout_width="96dp" android:layout_height="96dp" android:layout_row="0" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_column="0" android:layout_margin="5dp" android:adjustViewBounds="true" android:elevation="5dp" android:maxWidth="50dp" android:maxHeight="50dp" android:scaleType="fitXY" android:background="@color/white" android:src="@drawable/image_icon" app:shapeAppearanceOverlay="@style/imageViewRounded" app:strokeColor="@android:color/black" />
<TextView android:id="@+id/textView_venue_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="42dp" android:layout_marginLeft="10dp" android:layout_marginRight="20dp" android:text="Teacher" android:elevation="15dp" android:textSize="20dp" android:textStyle="bold" /> </GridLayout>
</androidx.cardview.widget.CardView>
<androidx.recyclerview.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/Recycerview_admin_Reschedule_time_table"/>
</LinearLayout> </RelativeLayout> |
Background XML Design Code :
| <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Drop Shadow Stack --> <item> <shape> <padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
<solid android:color="#00CCCCCC" />
<corners android:radius="3dp" /> </shape> </item> <item> <shape> <padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
<solid android:color="#10CCCCCC" />
<corners android:radius="3dp" /> </shape> </item> <item> <shape> <padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
<solid android:color="#20CCCCCC" />
<corners android:radius="3dp" /> </shape> </item> <item> <shape> <padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
<solid android:color="#30CCCCCC" />
<corners android:radius="3dp" /> </shape> </item> <item> <shape> <padding android:bottom="1dp" android:left="1dp" android:right="1dp" android:top="1dp" />
<solid android:color="#50CCCCCC" />
<corners android:radius="3dp" /> </shape> </item>
<!-- Background --> <item> <shape> <solid android:color="@android:color/white" />
<corners android:radius="3dp" /> </shape> </item>
</layer-list> |
code Recycler View Cell Design:
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_marginLeft="6dp" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:orientation="horizontal" android:id="@+id/linearLayout_Day_of_Name"></LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_marginLeft="6dp" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:orientation="horizontal" android:id="@+id/linearLayout_TimeTable_Schedule"></LinearLayout> </LinearLayout> </LinearLayout>
|
Recycler View Java Class Code :
| package com.example.meyepro.adapters; import android.app.Fragment; import android.content.Context; import android.graphics.Typeface; import android.os.Build; import android.text.method.ScrollingMovementMethod; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TableRow; import android.widget.TextView; import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView;
import com.example.meyepro.R; import com.example.meyepro.databinding.RecyclerviewTimetableScheduleCellBinding; import com.example.meyepro.models.TimeTable;
import java.util.ArrayList; import java.util.stream.Collectors;
public class AdminTimeTableScheduleAdapter extends RecyclerView.Adapter<AdminTimeTableScheduleAdapter.AdminTimeTableScheduleViewHolder>{ private ArrayList<TimeTable> TimeTableList; ArrayList<TimeTable> TimeTableSchrduleList; private Context context; private Fragment fragment; ArrayList<String> uniqueStartTimes= new ArrayList<>(); ArrayList<String> uniqueEndTimes= new ArrayList<>(); ArrayList<String> uniqueDays= new ArrayList<>();
public AdminTimeTableScheduleAdapter(ArrayList<TimeTable> TimeTableList, Context context) { this.TimeTableList = TimeTableList; this.context = context; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
uniqueStartTimes.addAll(TimeTableList.stream().distinct().map(t -> t.getStarttime()).distinct().collect(Collectors.toList())); uniqueEndTimes.addAll(TimeTableList.stream().distinct().map(t -> t.getEndtime()).distinct().collect(Collectors.toList())); uniqueDays.addAll(TimeTableList.stream().distinct().map(t -> t.getDay()).distinct().collect(Collectors.toList())); } } public AdminTimeTableScheduleAdapter(ArrayList<TimeTable> TimeTableList ,ArrayList<TimeTable> TimeTableSchrduleList, Context context) { this.TimeTableList = TimeTableList; this.context = context; this.TimeTableSchrduleList=TimeTableSchrduleList;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { uniqueStartTimes.addAll(TimeTableList.stream().distinct().map(t -> t.getStarttime()).distinct().collect(Collectors.toList())); uniqueEndTimes.addAll(TimeTableList.stream().distinct().map(t -> t.getEndtime()).distinct().collect(Collectors.toList())); uniqueDays.addAll(TimeTableList.stream().distinct().map(t -> t.getDay()).distinct().collect(Collectors.toList())); } for (TimeTable t :TimeTableList) { System.out.println("TimeTable"+t.getVenue()); } } @NonNull @Override public AdminTimeTableScheduleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { RecyclerviewTimetableScheduleCellBinding b =RecyclerviewTimetableScheduleCellBinding. inflate(LayoutInflater .from(parent.getContext()), parent, false);
AdminTimeTableScheduleViewHolder vh = new AdminTimeTableScheduleViewHolder(b); return vh; }
@Override public void onBindViewHolder(@NonNull AdminTimeTableScheduleViewHolder holder, int position) { //row one only if(position==0){ holder.bindDays(position); } String obj = uniqueStartTimes.get(position); //multiple column create holder.bind(position);
}
@Override public int getItemCount() { return uniqueStartTimes.size(); }
class AdminTimeTableScheduleViewHolder extends RecyclerView.ViewHolder { RecyclerviewTimetableScheduleCellBinding binding;//<---- public AdminTimeTableScheduleViewHolder( @NonNull RecyclerviewTimetableScheduleCellBinding itemView) { super(itemView.getRoot()); binding = itemView;//<---- } // row one Day Name public void bindDays(int position) { binding.linearLayoutDayOfName.removeAllViews();
// Create the first TextView with a width of 0 and weight of 1 to distribute equal width TextView textViewFirst = new TextView(itemView.getContext()); TableRow.LayoutParams firstParams = new TableRow.LayoutParams( 98, TableRow.LayoutParams.MATCH_PARENT); textViewFirst.setLayoutParams(firstParams); textViewFirst.setGravity(Gravity.CENTER); binding.linearLayoutDayOfName.addView(textViewFirst);
// Create the remaining TextViews with equal width and a margin of 5dp between them for (int i = 0; i < uniqueDays.size(); i++) { TextView textView = new TextView(itemView.getContext()); TableRow.LayoutParams params = new TableRow.LayoutParams( 0, TableRow.LayoutParams.MATCH_PARENT, 1f); // params.setMarginStart((int) itemView.getContext().getResources().getDimension(R.dimen.spacing_xxlarge)); textView.setLayoutParams(params); textView.setBackgroundResource(R.drawable.teacher_recyclerview_background_cell); textView.setTypeface(null, Typeface.BOLD); textView.setGravity(Gravity.CENTER); textView.setText(uniqueDays.get(i).substring(0, 3)); textView.setTextSize(18); binding.linearLayoutDayOfName.addView(textView); } } //end row of Days // start Stat Time And End Time public void bind(int RowPosition) { binding.linearLayoutTimeTableSchedule.removeAllViews(); TextView textViewFirst = new TextView(context); // Set the text textViewFirst.setText(uniqueStartTimes.get(RowPosition)+"\n"+uniqueEndTimes.get(RowPosition)); // Set the width and height int width = ViewGroup.LayoutParams.WRAP_CONTENT;; // in pixels int height =200; ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(width, height); textViewFirst.setLayoutParams(layoutParams); /// Set the gravity and layout gravity textViewFirst.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { textViewFirst.setForegroundGravity(Gravity.CENTER); } // // Create a TableRow to contain the TextViews // TableRow tableRow = new TableRow(itemView.getContext()); // tableRow.setLayoutParams(new ViewGroup.LayoutParams( // ViewGroup.LayoutParams.MATCH_PARENT, // ViewGroup.LayoutParams.WRAP_CONTENT)); binding.linearLayoutTimeTableSchedule.addView(textViewFirst); for (int i = 0; i < uniqueDays.size(); i++) { TableRow.LayoutParams params = new TableRow.LayoutParams( TableRow.LayoutParams.MATCH_PARENT, 200, 1f); params.gravity = Gravity.RIGHT; TextView textView = new TextView(itemView.getContext()); textView.setLayoutParams(params); textView.setTypeface(null, Typeface.BOLD); textView .setGravity(Gravity.CENTER);
//enter TextView Data for (TimeTable timeTable :TimeTableSchrduleList){ if (timeTable.getStarttime().toString().equals(uniqueStartTimes.get(RowPosition)) ) { if (timeTable.getDay().equals(uniqueDays.get(i))) { textView.setText(timeTable.getDiscipline()+"\n"+timeTable.getCourseName()+"\n"+timeTable.getVenue()); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(context, ""+textView.getText().toString(), Toast.LENGTH_SHORT).show(); } }); } } }
//end enter TextView Data // Add a ScrollView to the parent layout ScrollView scrollView = new ScrollView(itemView.getContext()); scrollView.setLayoutParams(params); scrollView.setVerticalScrollBarEnabled(true);
// Add the TextView to the ScrollView scrollView.addView(textView); scrollView.setBackgroundResource(R.drawable.teacher_recyclerview_background_cell); // tableRow.addView(scrollView); // textView.setMovementMethod(new ScrollingMovementMethod()); binding.linearLayoutTimeTableSchedule.addView(scrollView);
// binding.linearLayoutTimeTableSchedule.addView(textView); } // binding.linearLayoutTimeTableSchedule.addView(tableRow); }
// End start Stat Time And End Time } }
|
This is an Android RecyclerView adapter written in Java programming language. The adapter displays a timetable schedule with days of the week as columns and time slots as rows. Each cell in the table represents a particular class with information such as the course name, course code, instructor name, and venue.
The adapter has two constructors, the first one takes an ArrayList of TimeTable objects and a context, while the second one takes two ArrayLists of TimeTable objects and a context. The TimeTable object represents a single class in the schedule. The first constructor is used to display the complete timetable schedule, while the second constructor is used to display a filtered timetable schedule based on some user input.
The adapter creates a RecyclerView.ViewHolder that holds the RecyclerviewTimetableScheduleCellBinding object, which contains the layout for each cell in the timetable schedule. The onBindViewHolder method is used to bind the data to the view holder. The RecyclerView is populated by iterating through the start times of each class in the schedule, and for each start time, a row is created with cells representing each day of the week.
The adapter uses a linear layout to display the days of the week at the top of the timetable schedule. The start time and end time are displayed in the first cell of each row, and the cells representing each day of the week are displayed next to the start time and end time cell. The cells are created dynamically using TextView objects, and each cell is set with appropriate values from the TimeTable object.
Overall, this adapter is used to display a timetable schedule in a RecyclerView in an Android app. It is written in Java and uses the Android SDK to create the user interface.