Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AndroidFundamentalsMai2021">
<activity android:name=".ConstraintLayoutSampleActivity"></activity>
<activity android:name=".MainActivity" />
<activity android:name=".EmailsActivity" />
<activity android:name=".StartActivity">
<activity android:name=".StudentActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void startMainActivityOnClick(View view) {

public void startRecyclerViewActivityOnClick(View view) {
// starts from the current activity, and moves the user to the EmailsActivity screen
Intent intentToStartEmailsActivity = new Intent(StartActivity.this, EmailsActivity.class);
Intent intentToStartEmailsActivity = new Intent(StartActivity.this, StudentActivity.class);
startActivity(intentToStartEmailsActivity);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.magdamiu.androidfundamentalsmai2021;

public class Student {
private int id;
private String firstName;
private String lastName;

public Student(int id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.magdamiu.androidfundamentalsmai2021;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

public class StudentActivity extends AppCompatActivity {

private List<Student> students;
private static Context context;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private StudentAdapter studentAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StudentActivity.context = getApplicationContext();
setContentView(R.layout.activity_students);

// data source for our RecyclerView
populateListOfStudents();

// init the UI elements
initView();

// set the linear layout manager to our recycler view
setLayoutManager();

// set the adapter to our recycler view
setAdapter();
}

private void initView() {
recyclerView = findViewById(R.id.recyclerViewStudents);
}

private void populateListOfStudents() {
students = new ArrayList<>();
int index;
for (int i = 0; i < 20; i++) {
index = i + 1;
students.add(new Student(index, "FirstName " + index, "LastName " + index));
}
}

private void setLayoutManager() {
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
}

private void setAdapter() {
studentAdapter = new StudentAdapter(students);
recyclerView.setAdapter(studentAdapter);
}

public static Context getAppContext() {
return StudentActivity.context;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.magdamiu.androidfundamentalsmai2021;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class StudentAdapter extends RecyclerView.Adapter<StudentViewHolder> {
private final List<Student> students;

public StudentAdapter(List<Student> students) {
this.students = students;
}

@NonNull
@Override
public StudentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.student_item, parent, false);
return new StudentViewHolder(itemView);
}

// fills the item with the corresponding element from the list (data source)
@Override
public void onBindViewHolder(@NonNull StudentViewHolder holder, int position) {
Student currentStudent = students.get(position);
if (position % 2 == 1){
holder.getStudentView().setBackgroundColor(ContextCompat.getColor(StudentActivity.getAppContext(), R.color.grey));
}
holder.getTextFirstName().setText(currentStudent.getFirstName());
holder.getTextLastName().setText(currentStudent.getLastName());
}

// decides how many items(total number) to create in our recycler view
@Override
public int getItemCount() {
return students.size();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.magdamiu.androidfundamentalsmai2021;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

// the class associated to the item of the recycler view
// each ui element from the item should have a dedicated field in this class
public class StudentViewHolder extends RecyclerView.ViewHolder {
final private LinearLayout studentView;
final private TextView textFirstName;
final private TextView textLastName;

public StudentViewHolder(@NonNull View itemView) {
super(itemView);
studentView = itemView.findViewById(R.id.student);
textFirstName = itemView.findViewById(R.id.textFirstName);
textLastName = itemView.findViewById(R.id.textLastName);
}


public TextView getTextFirstName() {
return textFirstName;
}

public TextView getTextLastName() {
return textLastName;
}

public LinearLayout getStudentView() {
return studentView;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".EmailsActivity">
tools:context=".StudentActivity">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewEmails"
android:id="@+id/recyclerViewStudents"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Expand Down
Loading