Skip to main content

RecyclerView in Android

#Java class

#In this file we connect model class and viewholdw class to show in recyclerview
public class AdminProductShow extends AppCompatActivity {


    private Query ProductsRef;
    private RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_product_show);


        ProductsRef = FirebaseDatabase.getInstance().getReference("Products");

        recyclerView = findViewById(R.id.allProduct_recyclerview);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);


    }

    protected void onStart() {
        super.onStart();

        final DatabaseReference AdminProductsRef = FirebaseDatabase.getInstance().getReference("Products");


        FirebaseRecyclerOptions<Products> options =
                new FirebaseRecyclerOptions.Builder<Products>()
                        .setQuery(ProductsRef, Products.class)
                        .build();



        FirebaseRecyclerAdapter<Products, AdminViewHolder> adapter =
                new FirebaseRecyclerAdapter<Products, AdminViewHolder>(options) {
                    @Override                    protected void onBindViewHolder(@NonNull AdminViewHolder holder, int position, @NonNull final Products model) {

                        holder.txtProductName.setText(model.getPname());
                        holder.txtProductPrice.setText("Price = " + model.getPrice() + "");
                        holder.txtProductDescription.setText(model.getDescription());
                        holder.txtproductTopping.setText(model.getPtopping());
                        holder.txtProductDate.setText(model.getDate());
                        holder.txtProductTime.setText(model.getTime());

                        Picasso.get().load(model.getImage()).into(holder.imageView);

                        holder.itemView.setOnClickListener(new View.OnClickListener() {
                            @Override                            public void onClick(View v)
                            {
                              // Intent intent = new Intent(AdminProductShow.this, ShowProduct.class);                                //intent.putExtra("pid",model.getPid());                                //startActivity(intent);
                                CharSequence options[] = new CharSequence[]
                                        {

                                                "Delete"                                        };

                                AlertDialog.Builder builder = new AlertDialog.Builder(AdminProductShow.this);
                                builder.setTitle("Product Options");


                                builder.setItems(options, new DialogInterface.OnClickListener() {
                                    @Override                                    public void onClick(DialogInterface dialog, int i) {


                                        if(i==0)
                                        {

                                            AdminProductsRef.child(model.getPid())
                                                    .removeValue()
                                                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                                                        @Override                                                        public void onComplete(@NonNull Task<Void> task)
                                                        {
                                                            Toast.makeText(AdminProductShow.this, "Product Removed sucessfully..", Toast.LENGTH_SHORT).show();

                                                        }
                                                    });
                                        }

                                    }
                                });
                                builder.show();

                            }
                        });

                    }

                    @NonNull                    @Override                    public AdminViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
                        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.admin_product_list,parent,false);
                        AdminViewHolder holder = new AdminViewHolder(view);
                        return holder;
                    }
                };

        recyclerView.setAdapter(adapter);
        adapter.startListening();


    }



}


#Products model class

#here create new java class names products.
#this model class holds product information.

public class Products {

    private String pname, description, price, image, category, pid, date, time,ptopping;


    public Products()
    {

    }

    public Products(String pname, String description, String price, String image, String category, String pid, String date, String time, String ptopping) {
        this.pname = pname;
        this.description = description;
        this.price = price;
        this.image = image;
        this.category = category;
        this.pid = pid;
        this.date = date;
        this.time = time;
        this.ptopping = ptopping;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getPtopping() {
        return ptopping;
    }

    public void setPtopping(String ptopping) {
        this.ptopping = ptopping;
    }
}



#Products ViewHolder class

#this Class holds information about cardview


public class ProductViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener

{

    public TextView txtProductName,txtProductPrice;
    public ImageView imageView;
    public ItemClickListner listner;


    public ProductViewHolder(@NonNull View itemView) {
        super(itemView);

        imageView = (ImageView) itemView.findViewById(R.id.product_img);
        txtProductName = (TextView) itemView.findViewById(R.id.product_name);
        txtProductPrice = (TextView) itemView.findViewById(R.id.product_price);
    }

    public void setItemClickListner(ItemClickListner listner)
    {
        this.listner = listner;

    }

    @Override    public void onClick(View v) {
        listner.onClick(v, getAdapterPosition(), false);
    }

}


Comments

Popular posts from this blog

Register page in Android Studio using Firebase Databse

#Register page in android Studio #Connect your project to firebase database #Here typed add data in store in your firebase storage as node public class RegisterActivity extends AppCompatActivity { private Button CreateAccountButton; private EditText InputName,InputPhoneNumber, InputPassword; private ProgressDialog loadingBar; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_register); CreateAccountButton = (Button) findViewById(R.id.register_btn); InputName = (EditText) findViewById(R.id.register_username_input); InputPassword = (EditText) findViewById(R.id.register_password_input); InputPhoneNumber = (EditText) findViewById(R.id.register_phone_number_input); loadingBar = new ProgressDialog( this ); CreateAccountButton.setOnClickListener( new View.OnClickListener() { @Override ...