RecyclerView中的项目重复

时间:2019-05-12 13:50:18

标签: android android-recyclerview

我正在使用RecyclerView的Android项目中进行工作。回收者视图中的项目计数是正确的,但是在一定间隔后这些项目将被重复。我已经在Internet上检查了该解决方案,并尝试使用它们,但是它没有帮助我。

该片段的Java代码如下:

package com.example.laundry1app;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ProgressBar;
import android.widget.TextView;


import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.Objects;
public class AllProductsFragment extends Fragment {
    private ProgressBar progress;
    private ArrayList<AllProductsData> data;
    private TextView products;
    private View view;
    private RecyclerView recyclerView;
    private FloatingActionButton add, cart;
    private boolean flag;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_all_products, container, false);
        progress = view.findViewById(R.id.progress);
        progress.setVisibility(View.VISIBLE);
        add = view.findViewById(R.id.add);
        cart = view.findViewById(R.id.cart);
        if (FirebaseAuth.getInstance().getCurrentUser() != null) {
            DatabaseReference reference = FirebaseDatabase.getInstance().getReference("admins");
            reference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        if (Objects.equals(snapshot.child("email").getValue(String.class), FirebaseAuth.getInstance().getCurrentUser().getEmail())) {
                            flag = true;
                            add.show();
                            add.setEnabled(true);
                            cart.hide();
                            cart.setEnabled(false);
                            add.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (getFragmentManager() != null) {
                                        startActivity(new Intent(getContext(), AddProductActivity.class));
                                        Objects.requireNonNull(getActivity()).finish();
                                    }
                                }
                            });
                            break;
                        }
                    }
                    if (!flag) {
                        add.hide();
                        add.setEnabled(false);
                        cart.show();
                        cart.setEnabled(true);
                        cart.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                startActivity(new Intent(getContext(), CartActivity.class));
                                Objects.requireNonNull(getActivity()).finish();
                            }
                        });
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                }
            });
        }
        products = view.findViewById(R.id.products);
        data = new ArrayList<>();
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("products");
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                data.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    data.add(new AllProductsData(snapshot.child("english").getValue(String.class), snapshot.child("url").getValue(String.class)));

                }
                progress.setVisibility(View.GONE);
                if (data.isEmpty()) {
                    products.setVisibility(View.VISIBLE);
                } else {
                    products.setVisibility(View.GONE);
                }//
                recyclerView = view.findViewById(R.id.recyclerView);
                recyclerView.setHasFixedSize(true);
                recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                recyclerView.setAdapter(new AllProductsAdapter(data));
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });
        return view;
    }
}

数据类的代码为:

package com.example.laundry1app;

public class AllProductsData {
    private String english, url;

    AllProductsData(String english, String url) {
        this.english = english;
        this.url = url;

    }

    public String getEnglish() {
        return english;
    }

    public void setEnglish(String english) {
        this.english = english;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

适配器的Java代码如下:

package com.example.laundry1app;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.Objects;

class AllProductsAdapter extends RecyclerView.Adapter<com.example.laundry1app.AllProductsAdapter.ViewHolder> {
    private ArrayList<AllProductsData> data;
    private DatabaseReference reference, databaseReference;
    private boolean flag;
    private Double p;
    AllProductsAdapter(ArrayList<AllProductsData> data) {
        setHasStableIds(true);
        this.data = data;
    }
    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }
    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_products, viewGroup, false));
    }
    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, @SuppressLint("RecyclerView") final int i) {
        viewHolder.english.setText(data.get(i).getEnglish());
        viewHolder.quantity.setText(viewHolder.quantity.getText().toString());
        GlideApp.with(viewHolder.image.getContext()).load(data.get(i).getUrl()).into(viewHolder.image);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(viewHolder.spinner.getContext(),R.array.Services, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        viewHolder.spinner.setAdapter(adapter);

        if (FirebaseAuth.getInstance().getCurrentUser() != null) {
            reference = FirebaseDatabase.getInstance().getReference("admins");
            reference.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        if (Objects.equals(snapshot.child("email").getValue(String.class), FirebaseAuth.getInstance().getCurrentUser().getEmail())) {
                            flag = true;
                            viewHolder.add.setEnabled(false);
                            viewHolder.add.setVisibility(View.GONE);
                            viewHolder.quantity.setEnabled(false);
                            viewHolder.quantity.setVisibility(View.GONE);
                            viewHolder.spinner.setEnabled(false);
                            viewHolder.spinner.setVisibility(View.GONE);

                            break;
                        }
                    }
                    if (!flag) {
                        viewHolder.add.setEnabled(true);
                        viewHolder.add.setVisibility(View.VISIBLE);
                        reference = FirebaseDatabase.getInstance().getReference("users/" + FirebaseAuth.getInstance().getCurrentUser().getUid() + "/cart");
                        reference.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                                    if (Objects.equals(snapshot.child("english").getValue(), data.get(i).getEnglish())  && Objects.equals(snapshot.child("quantity").getValue(String.class), viewHolder.quantity.getText().toString())) {
                                        viewHolder.add.setEnabled(false);
                                        viewHolder.add.setText(R.string.added);
                                    }
                                }
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {
                            }
                        });
                        viewHolder.add.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (viewHolder.english.length() == 0 ) {
                                    viewHolder.english.requestFocus();
                                    viewHolder.english.setError("This Is A Required Field");
                                } else if (viewHolder.spinner.getSelectedItem() == "Wash" || viewHolder.spinner.getSelectedItem() == "Laundry" || viewHolder.spinner.getSelectedItem() == "Steam Press" || viewHolder.spinner.getSelectedItem() == "Wash + Steam Press"){
                                    viewHolder.spinner.requestFocus();
                                    Toast.makeText(viewHolder.spinner.getContext(), "Select a service", Toast.LENGTH_SHORT).show();
                                }else {
                                    databaseReference = reference.push();
                                    databaseReference.child("english").setValue(data.get(i).getEnglish());
                                    databaseReference.child("quantity").setValue(viewHolder.quantity.getText().toString());
                                    databaseReference.child("image").setValue(data.get(i).getUrl());
                                    databaseReference.child("service").setValue(viewHolder.spinner.getSelectedItem().toString());
                                    if (viewHolder.spinner.getSelectedItem().toString().equals("Wash")) {
                                        p = Double.parseDouble(viewHolder.quantity.getText().toString()) * 5;

                                    } else if (viewHolder.spinner.getSelectedItem().toString().equals("Laundry")) {
                                        p = Double.parseDouble(viewHolder.quantity.getText().toString()) * 8;

                                    } else if (viewHolder.spinner.getSelectedItem().toString().equals("Steam Press")) {
                                        p = Double.parseDouble(viewHolder.quantity.getText().toString()) * 3;

                                    } else if (viewHolder.spinner.getSelectedItem().toString().equals("Wash + Steam Press")) {
                                        p = Double.parseDouble(viewHolder.quantity.getText().toString()) * 7;

                                    }
                                    databaseReference.child("price").setValue(p);
                                }

                            }
                        });
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                }
            });
        }
    }
    @Override
    public int getItemCount() {
        return data.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView english, quantity;
        private Button add;
        Spinner spinner;
        private ImageView image;
        ViewHolder(View itemView) {
            super(itemView);
            this.english = itemView.findViewById(R.id.english);
            this.quantity = itemView.findViewById(R.id.quantity);
            this.add = itemView.findViewById(R.id.add);
            this.spinner = itemView.findViewById(R.id.typeof_service);
            this.image = itemView.findViewById(R.id.img);
        }
    }
}

有人可以帮我解决问题吗?预先感谢。

2 个答案:

答案 0 :(得分:1)

您是说您的商品具有稳定的ID(setHasStableId(true)),但您并未返回商品的稳定/唯一ID,而是返回了super.getItemId(),该商品返回了NO_ID每个位置。

请参阅setHasStableId()getItemId()的文档。

答案 1 :(得分:0)

1)您的类AllProductsData并不为每个项目都包含唯一的ID,因此您可能要添加:

public class AllProductsData {
    private int id;
    private String english, url;
    //...

2您的getItemId方法不在AllProductsData列表中查找ID,而是在超类(即:RecyclerView.Adapter)中查找方法。 在将ID添加到类之后,您的getItemId应该如下所示:

@Override
public long getItemId(int position) {
    return data[position].id;
}