簡體   English   中英

firebase 數據庫或我的列表視圖在前幾項之后重復數據

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

我正在使用 firebase 數據庫和 glide 訪問存儲在 firebase 存儲中的圖像。 但是當我運行代碼時,它似乎在前 6 個之后重復了這些項目。這是我的數據庫參考

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

在我的數據庫中看起來像這樣

{
  "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"
}

那么這是我的編碼

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);

這些是我的日志中返回的值

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

這是我為我的適配器 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();


                }

這是我的適配器 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);
}

**編輯我今天嘗試了這些答案。 這些都沒有奏效。

為什么 ListView 每 6 項重復一次?

這也是我的應用程序的圖像,以顯示正在發生的事情在此處輸入圖像描述

這是一個如此簡單的解決方法,我可能會因為沒有得到它而自責,我的適配器中的 imageview 被初始化在錯誤的位置。 更換

 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);
}

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;
}

糾正了問題哈哈

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM