How do I not repeat data from a ListView in an Android application?

0

When you use a ListView to display the name and image of some data that is in the Cloud Storage for Firebase , an Android device is starting to repeat itself images and names, even ListVIew referring to different data.

Researching this is due to the fact that the Adapter , responsible for passing the data to the LIstView, recycles the first data displayed on the screen, placing them in the ListView positions that are not being displayed on the screen at first. So by scrolling the screen instead of displaying new data, it displays the recycled data.

What do I do so that this data is not recycled and ListVIew displays correctly with the data in order?

Following is part of the Fragment code that displays the ListView on your Android device

    View view = inflater.inflate(R.layout.fragment_home, container, false);

    listaReceitas = (ListView) view.findViewById(R.id.listaReceitas);

    //Configurando altura e largura para cada imagem
    DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
    int widht = displayMetrics.widthPixels - 16;
    int height = displayMetrics.heightPixels/3;

    //exibição do listView
    receitasArrayList = new ArrayList<>();
    adaptador = new AdaptadorExibicaoReceitas(getContext(), receitasArrayList, widht, height);
    listaReceitas.setAdapter(adaptador);

    //referebciando o local das receitas no FirebaseDatabase
    databaseReference = FirebaseDatabase.getInstance().getReference()
            .child("Receitas");

    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            //limpando ArrayList para a mesma receita nao aparecer mais de uma vez
            receitasArrayList.clear();

            for(DataSnapshot dados : dataSnapshot.getChildren()){
                //adicionando as informaçoes obtidas do Firebase no ArrayList para ser configurado e assim podendo ser exibido no ListView
                Receitas receitas = dados.getValue(Receitas.class);
                receitasArrayList.add(receitas);
            }

            //notificando o adaptador que houve mudanças no ArrayList
            adaptador.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

It also follows the Adapter code responsible for passing the data to each ListView position

//Pegando os dados para serem inseridos no listView
public AdaptadorExibicaoReceitas(@NonNull Context c, @NonNull ArrayList<Receitas> objects, @NonNull int largura, @NonNull int altura) {
    super(c, 0, objects);
    this.context = c;
    this.receitasAdap = objects;
    this.widht = largura;
    this.height = altura;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    View view = convertView;

    if(view ==  null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

        view = inflater.inflate(R.layout.lista_receitas, parent, false);

        if(receitasAdap.size() > 0){
            Receitas receita = receitasAdap.get(position);

            final ImageView imagemReceita = (ImageView) view.findViewById(R.id.imagemReceitaId);
            TextView tituloReceita = (TextView) view.findViewById(R.id.tituloReceitaId);

            FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
            StorageReference storageReference = firebaseStorage.getReferenceFromUrl("gs://receitas-pantaneiras.appspot.com/").child(receita.getUrl());


            storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    Picasso.with(getContext()).load(uri.toString()).resize(widht, height).centerCrop().into(imagemReceita);
                }
            });
            tituloReceita.setText(receita.getNome());
            tituloReceita.setVisibility(View.VISIBLE);
        }
    }

    return view;
}
    
asked by anonymous 22.09.2018 / 23:36

0 answers