MainActivity Xml Code:-
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/productrec"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Resourse File Xml Code:-
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="6dp"
android:elevation="6dp"
app:cardUseCompatPadding="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/ic_launcher_background"
android:layout_centerVertical="true"
android:id="@+id/img1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/nametext"
android:text="Student Name"
android:textStyle="bold"
android:textSize="25sp"
android:layout_toRightOf="@+id/img1"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/coursetext"
android:text="Course Name"
android:textSize="25sp"
android:layout_toRightOf="@+id/img1"
android:layout_below="@+id/nametext"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/emailtext"
android:text="Email Id"
android:textSize="25sp"
android:layout_toRightOf="@+id/img1"
android:layout_below="@+id/coursetext"
android:layout_marginLeft="10dp"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
MainActivity Java Code:-
package com.niranjan.ecomerceapp.ui.gallery;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import com.niranjan.ecomerceapp.ProductAdapter;
import com.niranjan.ecomerceapp.R;
import com.niranjan.ecomerceapp.productmodel;
import java.util.ArrayList;
import java.util.List;
public class GalleryFragment extends Fragment {
RecyclerView productrec;
FirebaseFirestore db;
List<productmodel> productmodelList;
ProductAdapter productAdap;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_gallery, container, false);
db= FirebaseFirestore.getInstance();
productrec = root.findViewById(R.id.productrec);
productrec.setLayoutManager(new LinearLayoutManager(getActivity(),RecyclerView.HORIZONTAL,false));
productmodelList=new ArrayList<>();
productAdap = new ProductAdapter(getActivity(),productmodelList);
productrec.setAdapter(productAdap);
db.collection("popularproduct")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
productmodel productmodel = document.toObject(com.niranjan.ecomerceapp.productmodel.class);
productmodelList.add(productmodel);
productAdap.notifyDataSetChanged();
}
}
}
});
return root;
}
}
java Adapter class:-
package com.niranjan.ecomerceapp;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.List;
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> {
private Context context;
private List<productmodel> productmodelList;
public ProductAdapter(Context context, List<productmodel> productmodelList) {
this.context = context;
this.productmodelList = productmodelList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.popularproduct,parent,false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Glide.with(context).load(productmodelList.get(position));
holder.name.setText(productmodelList.get(position).getName());
holder.course.setText(productmodelList.get(position).getCourse());
holder.email.setText(productmodelList.get(position).getEmail());
}
@Override
public int getItemCount() {
return productmodelList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView name,course,email;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView= itemView.findViewById(R.id.img1);
name= itemView.findViewById(R.id.nametext);
course= itemView.findViewById(R.id.coursetext);
email= itemView.findViewById(R.id.emailtext);
}
}
}
Model class :-
package com.niranjan.ecomerceapp;
public class productmodel {
String name;
String course;
String email;
public productmodel() {
}
public productmodel(String name, String course, String email) {
this.name = name;
this.course = course;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
No comments:
Post a Comment