markharrop
markharrop

Reputation: 876

firebase database or my listview is duplicating data after first few items

I'm using firebase database and glide to access images stored in firebase storage. but when i run the code it seems to duplicate the items after the first 6. here is my database ref

ref = db.getReference().child("LOGOS");

Which in my database looks like this

{
  "70s": "My FirebaseStorage https web address",
  "80s": "My FirebaseStorage https web address",
  "90s": "My FirebaseStorage https web address",
  "COMMERCIAL": "My FirebaseStorage https web address",
  "DANCE": "My FirebaseStorage https web address",
  "HIPHOP": "My FirebaseStorage https web address",
  "MASHUP": "My FirebaseStorage https web address",
  "NEWS": "My FirebaseStorage https web address"
}

then here is my coding

ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        Genre_Adapter adapter;
        imageUrl.clear();
        
        for (DataSnapshot snapa : snapshot.getChildren()) {
      

            String logo = snapa.getValue(String.class);

            imageUrl.add(logo);

// I've added a Log here to see that the data returns the right values and it 
// does, but when loaded in to the list view it repeats.

            String key = snapa.getKey();

            Log.e("KEYS", key);

these are the values returned in my log

this is what is returned when i log.e the string key

2022-08-15 11:47:08.958 19751-19751/com.p9p.radioify E/KEYS: 70S
2022-08-15 11:47:08.959 19751-19751/com.p9p.radioify E/KEYS: 80S
2022-08-15 11:47:08.959 19751-19751/com.p9p.radioify E/KEYS: 90S
2022-08-15 11:47:08.959 19751-19751/com.p9p.radioify E/KEYS: COM
2022-08-15 11:47:08.959 19751-19751/com.p9p.radioify E/KEYS: DANCE
2022-08-15 11:47:08.960 19751-19751/com.p9p.radioify E/KEYS: DRUM & BASS
2022-08-15 11:47:08.960 19751-19751/com.p9p.radioify E/KEYS: HIPHOP
2022-08-15 11:47:08.960 19751-19751/com.p9p.radioify E/KEYS: MASHUP
2022-08-15 11:47:08.960 19751-19751/com.p9p.radioify E/KEYS: NEWS
2022-08-15 11:47:08.960 19751-19751/com.p9p.radioify E/KEYS: TRANCE
2022-08-15 11:47:08.960 19751-19751/com.p9p.radioify E/KEYS: UNITEDKINGDOM

this is where i convert to an array for my adapter class

            imageurl = imageUrl.toArray(new String[imageUrl.size()]);
            adapter = new Genre_Adapter(getContext(), imageurl);
        }#

  adapter = new Genre_Adapter(getContext(), imageurl);
            mlist.setAdapter(adapter);
            
            mlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    genre_list_item gli = new genre_list_item();
                    Bundle bundle = new Bundle();
                    bundle.putString("Genre", btntitle.get(position));
                    Log.i("genre_title", btntitle.get(position));
                    gli.setArguments(bundle);
                    getActivity().getSupportFragmentManager().beginTransaction()
                            .replace(R.id.frame1, gli, "genre_list_item")
                            .addToBackStack(null).commit();


                }

this is my adapter class

public Genre_Adapter(Context context,String[]logo){
    this. context = context;
    this.logo = logo;
}
@Override
public int getCount() {
    return logo.length;
}

@Override
public Object getItem(int position) {
    return logo[position];
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final View row;

    if (convertView == null) {
        row = LayoutInflater.from(parent.getContext()).inflate(R.layout.genre, parent, false);
        imageView = row.findViewById(R.id.image);
    } else {
        row = convertView;
    }
    Glide.with(context).load(logo[position]).into(imageView);

    return (row);
}

**EDIT I have tried these answers today. None of which have worked.

Why did the ListView repeated every 6th item?

Also this is an image of my app to show what is happening enter image description here

Upvotes: 3

Views: 90

Answers (1)

markharrop
markharrop

Reputation: 876

this i such an easy fix i could kick my self for not getting it the imageview in my adapter was being initialised in the wrong place. replacing

 if (convertView == null) {
    row = LayoutInflater.from(parent.getContext()).inflate(R.layout.genre, parent, false);
    imageView = row.findViewById(R.id.image);
} else {
    row = convertView;
}
Glide.with(context).load(logo[position]).into(imageView);

return (row);
}

with

if (convertView == null) {

        row = mLayoutInflater.inflate(R.layout.genre, null);

    } else {
       row = convertView;
    }

    imageView = (ImageView) row.findViewById(R.id.image);
    Glide.with(context).load(logo[position]).into(imageView);
    return row;
}

corrected the issue lol

Upvotes: 1

Related Questions