首页 > 解决方案 > RecyclerView 正在复制项目可能的解决方案是什么?

问题描述

RecyclerView r1;
LocationCallback callback;
Location location;
FusedLocationProviderClient client;
public String name, car, number;
ArrayList<MyModel> list = new ArrayList<>();
Boolean flag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recycler);
    r1 = findViewById(R.id.r1);
    client = LocationServices.getFusedLocationProviderClient(this);
    callback = new LocationCallback() {
        @Override
        public void onLocationResult(@NonNull LocationResult locationResult) {
            super.onLocationResult(locationResult);
            if (locationResult == null) {
                return;
            }
            location = locationResult.getLastLocation();

            getdriver();

            Toast.makeText(Recycler.this, "Location is:" + location.getLongitude() + location.getLatitude(), Toast.LENGTH_SHORT).show();
        }
    };
    showupadatelocation();
}
private void showupadatelocation() {
    LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(4000)
                .setFastestInterval(3000);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        client.requestLocationUpdates(locationRequest, callback, Looper.myLooper());
    } else {
        startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    }
}
MyAdapter myAdapter;
private void getdriver() {
    DatabaseReference driver = FirebaseDatabase.getInstance().getReference().child("Drivers Available");
    GeoFire geoFire = new GeoFire(driver);
    GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(location.getLatitude(), location.getLongitude()), 100);
    geoQuery.removeAllListeners();
    geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
        @Override
        public void onKeyEntered(String key, GeoLocation location) {
            DatabaseReference reff = FirebaseDatabase.getInstance().getReference().child("Users").child("Drivers").child(key);
            reff.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    if (snapshot.exists()) {
                        flag = true;
                        Map<String, Object> map = (Map<String, Object>) snapshot.getValue();
                        if (map.get("Name") != null) {
                            name = map.get("Name").toString();
                        }
                        if (map.get("Number") != null) {
                            number = map.get("Number").toString();
                        }
                        if (map.get("Car") != null) {
                            car = map.get("Car").toString();
                        }
                        list.add(new MyModel(name,number,car));
                        /*
                        int count=list.size();
                        int count1=myAdapter.getItemCount();
                        if(count==count1){
                            list.add(new MyModel(name,number,car));
                            flag=true;
                        }
                    Toast.makeText(Recycler.this, "list size"+count, Toast.LENGTH_SHORT).show();
                    Toast.makeText(Recycler.this, "Adapter size"+count1, Toast.LENGTH_SHORT).show();

                         */
                    }
                }
                @Override
                public void onCancelled(@NonNull DatabaseError error) {
                }
            });
        }
        @Override
        public void onKeyExited(String key) {

        }
        @Override
        public void onKeyMoved(String key, GeoLocation location) {

        }
        @Override
        public void onGeoQueryReady() {

        }
        @Override
        public void onGeoQueryError(DatabaseError error) {

        }
    });
    myAdapter = new MyAdapter(list, Recycler.this);
    r1.setAdapter(myAdapter);
    r1.setLayoutManager(new LinearLayoutManager(Recycler.this));
}
@Override
public void onBackPressed() {
    super.onBackPressed();
    list.clear();
    client.removeLocationUpdates(callback);
}

这是我的适配器:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.viewholder> {
ArrayList <MyModel> list;
Context context;
public MyAdapter(ArrayList<MyModel> list, Context context) {
    this.list = list;
    this.context = context;
}

@NonNull
@Override
public viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view= LayoutInflater.from(context).inflate(R.layout.driverinfo,parent,false);
    return new viewholder(view);
}

@Override
public void onBindViewHolder(@NonNull viewholder holder, int position) {
    MyModel myModel=list.get(position);
    holder.t1.setText(myModel.getName());
    holder.t2.setText(myModel.getNumber());
    holder.t3.setText(myModel.getCar());
}

@Override
public int getItemCount() {
    return list.size();
}
public class viewholder extends RecyclerView.ViewHolder{
    TextView t1,t2,t3;
    public viewholder(@NonNull View itemView) {
        super(itemView);
        t1=itemView.findViewById(R.id.namber);
        t2=itemView.findViewById(R.id.naame);
        t3=itemView.findViewById(R.id.caar);
    }
}

这是 RecyclerView 中的数据重复虽然我的 firebase 数据库中只有 2 个详细信息

这是我的数据库的结构

这就是我的问题是回收器视图不断在无限循环中添加数据什么可能是阻止这种情况发生的条件?

标签: javaandroidfirebase-realtime-databaseandroid-recyclerviewgeofire

解决方案


尝试这个

geoQuery.getCache().clear();

推荐阅读