Android Firebase 奇怪的数据映射

Posted

技术标签:

【中文标题】Android Firebase 奇怪的数据映射【英文标题】:Android Firebase strange data mapping 【发布时间】:2017-02-18 02:48:57 【问题描述】:

我在调试模式下收到此错误,但我不知道为什么会发生,因为一切都应该没问题。有些数据不为空,有些数据为空,有谁知道为什么会这样?

这是我的 Firebase 代码:

 mDatabase = FirebaseDatabase.getInstance();

    mReference = mDatabase.getReferenceFromUrl("https://basketball-training-app.firebaseio.com/article");

    mReference.addValueEventListener(new ValueEventListener() 

  @Override
   public void onDataChange(DataSnapshot dataSnapshot) 

    articleModel = new ArrayList<>();
    for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) 

        articleModel.add(postSnapshot.getValue(ArticleModel.class));
    



    adapter = new ArticleAdapter(getActivity().getApplicationContext(), R.layout.fragment_article_list_items, articleModel);

    mListView.setAdapter(adapter);
    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    @Override
    public void onItemClick(AdapterView<?> parent, View view, final int position, long id) 
        Intent intent = new Intent(getActivity(), ArticleDetailsActivity.class);
        final String postKey = mReference.getKey();
        intent.putExtra("post_key", postKey);
        getActivity().startActivity(intent);

    
);
  
        @Override
        public void onCancelled(DatabaseError databaseError) 
            throw databaseError.toException();
        
    );

这是我来自 Firebase 的数据:

[ 
"body" : "Becoming a professional basketball player take ",
"id" : "2",
"photo" : "http://img.aws.livestrongcdn.com/ls-article-image-640/cme/cme_public_images/www_livestrong_com/photos.demandstudios.com/getty/article/108/235/489314780_XS.jpg",
"published_date" : "2016-09-19",
"thumb" : "http://img.aws.livestrongcdn.com/ls-article-image-640/cme/cme_public_images/www_livestrong_com/photos.demandstudios.com/getty/article/108/235/489314780_XS.jpg",
"title" : "What Training Is Necessary to Become a Professional Basketball Player?",
"video" : ""
, 
"body" : "It's basketball; these are the best movies of",
"id" : "0",
"photo" : "http://images.contentful.com/7h71s48744nc/1w3KueQHLi2G0oyguoScQE/74af0a5222728503eff818ddcea6865e/coach-carter.jpg",
"published_date" : "2016-09-19",
"thumb" : "http://ecx.images-amazon.com/images/I/51FYWuWGPLL._SL160_.jpg",
"title" : "Top Ten Greatest Basketball Movies",
"video" : ""
 , 
"body" : "Basketball is a very popular sport played all around the ",
 "id" : "1",
 "photo" : "http://mba.org.mt/wp-content/uploads/2015/12/basketball-wallpaper-1280x768-1180x768.jpg",
 "published_date" : "2016-09-19",
 "thumb" : "http://mba.org.mt/wp-content/uploads/2015/12/basketball-wallpaper-1280x768-1180x768.jpg",
  "title" : "Popularity of Basketball Around the World",
 "video" : ""
  ]

这是模型:

 @PropertyName("thumb")
private String thumb;

@PropertyName("title")
private String title;

@PropertyName("published_date")
private String published_date;

@PropertyName("photo")
private String photo;

@PropertyName("body")
private String body;

@PropertyName("id")
private String id;

@PropertyName("video")
private String video;

public ArticleModel() 


public ArticleModel(String id, String title, String thumb, String published_date, String photo, String body, String video) 
    this.id = id;
    this.title = title;
    this.thumb = thumb;
    this.published_date = published_date;
    this.photo = photo;
    this.body = body;
    this.video = video;




public String getBody() 
    return body;


public void setBody(String body) 
    this.body = body;


public String getId() 
    return id;


public void setId(String id) 
    this.id = id;


public String getThumb() 
    return thumb;


public void setThumb(String thumb) 
    this.thumb = thumb;


public String getTitle() 
    return title;


public void setTitle(String title) 
    this.title = title;


public String getData() 
    return published_date;


public void setData(String published_date) 
    this.published_date = published_date;


public String getImage() 
    return photo;


public void setImage(String photo) 
    this.photo = photo;


public String getVideoURI() 
    return video;


public void setVideoURI(String video) 
    this.video = video;

【问题讨论】:

【参考方案1】:

setter 和 getter 方法的名称应与属性名称相同。

photopublished_date 的getter 和setter 方法添加或更改为

public void setPhoto(String photo) 
    this.photo = photo;


public String getPhoto() 
    return photo;


public void setPublished_date(String published_date) 
    this.published_date = published_date;


public String getPublished_date() 
    return published_date;

【讨论】:

这很奇怪,因为在 asynctask 上这不会导致问题...非常感谢! @TomasMaksimavicius 您不需要使用异步任务,ValueEventListener 已经在单独的线程中运行。 :)

以上是关于Android Firebase 奇怪的数据映射的主要内容,如果未能解决你的问题,请参考以下文章

如何将数据从 Firebase 获取到哈希映射中?

google-services 插件无法检测到 com.google.android.gms 或 com.google.firebase 的任何版本 - 奇怪的行为

将 Firebase 身份验证用户映射到 Firestore 文档

无法使用 Android 将数据写入 Firebase 数据库时如何获取错误消息

Android 应用程序包上传会在 Firebase 身份验证中创建奇怪的帐户 (<characters>.<5 digits>@gmail.com)

Android:Firebase 实时数据库有连接池吗?