28-10-22, 01:42 AM
قمت ببرمجة تطبيق لكنه لا يعمل مع انه لا يظهر اي خطأ
package com.example.ghitamode;
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.ArrayList;
import java.util.List;
public class MyProductsAdapter extends RecyclerView.Adapter<MyProductsAdapter.MyViewHolder> {
public Context context;
public List<Product> productList;
public MyProductsAdapter(Context context){
this.context =context;
productList=new ArrayList<>();
}
public void add(Product product){
productList.add(product);
notifyDataSetChanged();
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_product_view,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Product product =productList.get(position);
holder.name.setText(product.getpName());
holder.price.setText(product.getPrice());
Glide.with(context).load(product.getpImage())
.into(holder.imageView);
}
@Override
public int getItemCount() {
return productList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public TextView price;
public ImageView imageView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
name=itemView.findViewById(R.id.name);
price=itemView.findViewById(R.id.price);
imageView=itemView.findViewById(R.id.image);
}
}
}
""""""""
package com.example.ghitamode;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.example.ghitamode.databinding.ActivityAllProductsBinding;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.List;
public class AllProducts extends AppCompatActivity {
ActivityAllProductsBinding binding;
MyProductsAdapter myProductsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding=ActivityAllProductsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
myProductsAdapter=new MyProductsAdapter(this);
binding.productsRecycler.setAdapter(myProductsAdapter);
binding.productsRecycler.setLayoutManager(new LinearLayoutManager(this));
loadProduct();
}
private void loadProduct() {
FirebaseFirestore.getInstance()
.collection("PRODUCTS")
.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
List<DocumentSnapshot> dsList = queryDocumentSnapshots.getDocuments();
for (DocumentSnapshot ds : dsList) {
Product product = ds.toObject(Product.class);
myProductsAdapter.add(product);
}
}
});
}
}
""""""""""
public class Product {
private String pImage;
private String pName;
private int price;
public Product(String pImage, String pName, int price){
this.pImage = pImage;
this.pName = pName;
this.price = price;
}
public Product(){
}
public String getpImage() {
return pImage;
}
public void setpImage(String pImage) {
this.pImage = pImage;
}
public String getpName() {
return pName;
}
public void setpName(String pName) {
this.pName = pName;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
package com.example.ghitamode;
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.ArrayList;
import java.util.List;
public class MyProductsAdapter extends RecyclerView.Adapter<MyProductsAdapter.MyViewHolder> {
public Context context;
public List<Product> productList;
public MyProductsAdapter(Context context){
this.context =context;
productList=new ArrayList<>();
}
public void add(Product product){
productList.add(product);
notifyDataSetChanged();
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_product_view,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Product product =productList.get(position);
holder.name.setText(product.getpName());
holder.price.setText(product.getPrice());
Glide.with(context).load(product.getpImage())
.into(holder.imageView);
}
@Override
public int getItemCount() {
return productList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public TextView price;
public ImageView imageView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
name=itemView.findViewById(R.id.name);
price=itemView.findViewById(R.id.price);
imageView=itemView.findViewById(R.id.image);
}
}
}
""""""""
package com.example.ghitamode;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.example.ghitamode.databinding.ActivityAllProductsBinding;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.List;
public class AllProducts extends AppCompatActivity {
ActivityAllProductsBinding binding;
MyProductsAdapter myProductsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding=ActivityAllProductsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
myProductsAdapter=new MyProductsAdapter(this);
binding.productsRecycler.setAdapter(myProductsAdapter);
binding.productsRecycler.setLayoutManager(new LinearLayoutManager(this));
loadProduct();
}
private void loadProduct() {
FirebaseFirestore.getInstance()
.collection("PRODUCTS")
.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
List<DocumentSnapshot> dsList = queryDocumentSnapshots.getDocuments();
for (DocumentSnapshot ds : dsList) {
Product product = ds.toObject(Product.class);
myProductsAdapter.add(product);
}
}
});
}
}
""""""""""
public class Product {
private String pImage;
private String pName;
private int price;
public Product(String pImage, String pName, int price){
this.pImage = pImage;
this.pName = pName;
this.price = price;
}
public Product(){
}
public String getpImage() {
return pImage;
}
public void setpImage(String pImage) {
this.pImage = pImage;
}
public String getpName() {
return pName;
}
public void setpName(String pName) {
this.pName = pName;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}