java - firebase 数据库或我的 ListView 在前几项之后复制数据

标签 java android firebase-realtime-database

我正在使用 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

这是我为我的适配器类转换为数组的地方

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


                }

这是我的适配器类

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

**编辑 我今天已经尝试过这些答案。这些都没有用。

Why did the ListView repeated every 6th item?

这也是我的应用程序的图像,用于显示正在发生的事情 enter image description here

最佳答案

这是一个如此简单的修复,我可能会因为没有得到它而踢自己 我的适配器中的 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;
}

更正了问题,哈哈

关于java - firebase 数据库或我的 ListView 在前几项之后复制数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73359874/

相关文章:

ios - swift 不能在 firebase 数据库中创建一个 child

node.js - Firebase 通过 ref 限制子级返回

java - 使用 JAXRS 将 XML 编码为 Restful 服务的 JAVA 对象

java - 注释是否会使运行程序变慢?

android - 两个编辑文本上的 TextWatcher

android - Android Studio 中的 "cannot resolve symbol R"

java - HTTP POST 方法返回状态代码 404

ios - 在 firebase Root 上的 IndexOn 查询整个数据库 swift 3

java - 如果任何静态变量被销毁,它们是否都被销毁?

java - SimpleDateFormat 对同一日期字符串的解析和格式化给出了不同的结果