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 ...

MyWaah Marathi

Privacy Policy Mihir Vaste built the Waah Marathi app as a Free app. This SERVICE is provided by Mihir Vaste at no cost and is intended for use as is. This page is used to inform visitors regarding my policies with the collection, use, and disclosure of Personal Information if anyone decided to use my Service. If you choose to use my Service, then you agree to the collection and use of information in relation to this policy. The Personal Information that I collect is used for providing and improving the Service. I will not use or share your information with anyone except as described in this Privacy Policy. The terms used in this Privacy Policy have the same meanings as in our Terms and Conditions, which is accessible at Waah Marathi unless otherwise defined in this Privacy Policy. Information Collection and Use For a better experience, while using our Service, I may require you to provide us with certain personally identifiable information, including but not limited to location,intern...