RecyclerView不从Firebase Datasnapshot检索数据

时间:2017-09-30 21:57:21

标签: android database firebase firebase-realtime-database android-recyclerview

我正在尝试从Firebase填充recyclerView。将项目添加到数据库并最终使recyclerView正常工作(我可以通过控制台和Android监视器上的标签看到它)。 RV被填充并且在listItems中我有两个TextView,它们应该从数据库中检索它们的文本。

我的问题是这两个textViews没有出现。我做了很多研究,我认为我的问题是反序列化。

从这里我通过片段添加一个项目:

  public void addLanguage(){

    //ADDING A LANGUAGE COLOR CAN BE DONE HERE THROUGH AN ARRAY ON MANUALLY

    String mUserId= fUser.getUid();
    final FirebaseDatabase database= FirebaseDatabase.getInstance();
    final DatabaseReference fDatabase= FirebaseDatabase.getInstance().getReference();
    //String key= database.getReference("languagesList").push().getKey();
    String key= fDatabase.child("user").child(mUserId).child("languagesList").push().getKey();

    Languages language= new Languages();
    language.setSourceLanguage(sources_edit.getText().toString());
    language.setTargetLanguage(target_edit.getText().toString());

    Map<String, Object> childUpadate= new HashMap<>();
    childUpadate.put(key, language.toFirbaseObject());
    fDatabase.child(mUserId).child("languagesList").updateChildren(childUpadate, new DatabaseReference.CompletionListener() {
        @Override
        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
            if (databaseError == null) {
                dismiss();
            }else{
                Toast.makeText(getActivity(),  databaseError.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    });

}

这是RV所在的活动

 public void loadData(){
    databaseRef.child(mUserId).child("languagesList").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            languagesList.clear();

            Log.w("langApp", "getUser:onCancelled " + dataSnapshot.toString());
            Log.w("langApp", "count = " + String.valueOf(dataSnapshot.getChildrenCount()) + " values " + dataSnapshot.getKey());

            for(DataSnapshot data : dataSnapshot.getChildren()){
               // GenericTypeIndicator<Map<String, Languages >> languageMap= new GenericTypeIndicator<Map<String, Languages>>(){};
              //  Map<String, Languages> language= dataSnapshot.getValue(languageMap);
                Languages languages= data.getValue(Languages.class);
                languagesList.add(languages);
            }
            adapter.notifyDataSetChanged();
            spinKitView.setVisibility(View.GONE);


        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w("langApp", "getUser:onCancelled", databaseError.toException());

        }
    });

}

这是我的Object类

public class Languages implements Serializable {

public String sourceLanguage, targetLanguage;

public Languages(){

}

public String getSourceLanguage() {
    return sourceLanguage;
}

public void setSourceLanguage(String sourceLanguage) {
    this.sourceLanguage = sourceLanguage;
}

public String getTargetLanguage() {
    return targetLanguage;
}

public void setTargetLanguage(String targetLanguage) {
    this.targetLanguage = targetLanguage;
}

public HashMap<String, String> toFirbaseObject(){
    HashMap<String, String> language= new HashMap<String, String>();
    language.put("source", sourceLanguage);
    language.put("target", targetLanguage);

    return language;
}
}

RV适配器,它是活动中的内部类

 public class RecyclerAdapter extends RecyclerView.Adapter{


   @Override
   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
       RecyclerAdapter.mViewHolder cVH= new mViewHolder(v);

       return cVH;
   }

   @Override
   public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
       mViewHolder mViewHolder=  (mViewHolder)holder;
       mViewHolder.position= position;
       Languages languages= languagesList.get(position);
       ((mViewHolder) holder).sourceText.setText(languages.getSourceLanguage());
       ((mViewHolder) holder).targetText.setText(languages.getTargetLanguage());

   }

   @Override
   public int getItemCount() {
       return languagesList.size();
   }

   public final class mViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

       TextView sourceText, targetText;
       public int position;


       public mViewHolder(View itemView) {
           super(itemView);
           itemView.setOnClickListener(this);

           sourceText= (TextView)itemView.findViewById(R.id.source_language);
           targetText= (TextView)itemView.findViewById(R.id.target_language);
       }

       @Override
       public void onClick(View v) {

       }
   }
}

JSON

{

{ "kVRxDib8SJdQWRc1TULthqY7S6e2" : {

"languagesList" : {

  "-Kv2fKiiV5XAjo6lW9Eg" : {

    "source" : "Kejan",
    "target" : "Dachn"
  },


  "-Kv2hoV2eIEMcme6Akje" : {
    "source" : "Se",
    "target" : "Se"
  },

  "-Kv2iPJqEn7l6hP4gEzI" : {
    "source" : "Ay ",
    "target" : "Way"
  },


  "-Kv38J06mO8o1YudzXXY" : {
    "source" : "Aaa",
    "target" : "Zdsss"
  },


  "-Kv38d9b5ihF2sMADk3t" : {
    "source" : "Madri ",
    "target" : "Algomar"
  }


}

}

}

清单:`

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity android:name=".Authentication.SignupActivity" />
    <activity android:name=".Authentication.ResetPasswordActivity" />
    <activity android:name=".Authentication.LoginActivity" />
    <activity android:name=".Authentication.ProfileSettings" />
    <activity
        android:name=".DrawerActivity"
        android:label="@string/title_activity_drawer"
        android:theme="@style/AppTheme" />
    <activity
        android:name=".LanguagesActivity"
        android:label="@string/title_activity_languages"
        android:theme="@style/AppTheme" />
    <activity android:name=".Settings" />
    <activity android:name=".Authentication.SignupActivityTwo"></activity>
</application>

`

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:1)

根据Firebase文档:

此方法用于将此快照中包含的数据编组到您选择的类中。该类必须符合2个简单约束:

  1. 该类必须具有不带参数的默认构造函数
  2. 该类必须为要分配的属性定义公共getter。没有公共getter的属性将在反序列化实例时设置为默认值
  3. 您还需要为字段和getter命名,以便它们与JSON属性名称匹配。

    所以你应该有类似的东西:

        private String source, target;
    
        public String getSource() { return source; }
    
        public String getTarget() { return target; }
    

答案 1 :(得分:0)

尝试在<ol class="breadcrumb"> <li><%= link_to '/' do %> <i class="fa fa-home"></i> Home <% end %> </li> <i class="fa fa-angle-double-right" style="color: #ccc; padding: 0 5px;"></i> <% nav_list.each_with_index do |nav, i| %> <% if i != nav_list.length-1 %> <li><%= link_to nav['title'], nav['url'] %></li> <% else %> <li class="active"><%= nav['title'] %></li> <% end %> <% end %> </ol> 可能正常工作后在加载数据中添加 <!DOCTYPE html> <html> <head> <style> .clearfix:after { display:block; clear:both; } /*----- Menu Outline -----*/ .menu-wrap { width:100%; box-shadow:0px 1px 3px rgba(0,0,0,0.2); background:#3e3436; } .menu { width:1000px; margin:0px auto; } .menu li { margin:0px; list-style:none; font-family:'Ek Mukta'; } .menu a { transition:all linear 0.15s; color:#919191; } .menu li:hover > a{ text-decoration:none; color:#ffffff; background:#696969; } .menu .arrow { font-size:11px; line-height:0%; } /*----- Top Level -----*/ .menu > ul > li { float:left; display:inline-block; position:relative; font-size:19px; } .menu > ul > li > a { padding:10px 40px; display:inline-block; text-shadow:0px 1px 0px rgba(0,0,0,0.4); } .menu > ul > li:hover > a{ background:#2e2728; } /*----- Bottom Level -----*/ .menu li:hover .sub-menu { z-index:1; opacity:1; } .sub-menu { width:160%; padding:5px 0px; position:absolute; top:100%; left:0px; z-index:-1; opacity:0; transition:opacity linear 0.15s; box-shadow:0px 2px 3px rgba(0,0,0,0.2); background:#2e2728; } .sub-menu li { display:block; font-size:16px; } .sub-menu li a { padding:10px 30px; display:block; text-decoration: none; } .menu-title{ text-decoration: none; background-color: #fc575e; color: #000000; } a.menu-title{ color: #000000; } </style> </head> <body> <div class="menu_wrap"> <nav class="menu"> <ul class="clearfix"> <li><a class="menu-title" href="#">Home <span class="arrow">&#9660;</span></a> <ul class="sub-menu"> <li><a href="#">All</a></li> <li><a href="#">New Arrival</a></li> <li><a href="#">Casual</a></li> <li><a href="#">Gown</a></li> <li><a href="#">Bridesmade</a></li> </ul> </li> <li><a class="menu-title" href="#">Woman <span class="arrow">&#9660;</span></a> <ul class="sub-menu"> <li><a href="#">All</a></li> <li><a href="#">New Arrival</a></li> <li><a href="#">Casual</a></li> <li><a href="#">Gown</a></li> <li><a href="#">Bridesmade</a></li> </ul> </li> <li><a class="menu-title" href="#">Men <span class="arrow">&#9660;</span></a> <ul class="sub-menu"> <li><a href="#">All</a></li> <li><a href="#">New Arrival</a></li> <li><a href="#">Casual</a></li> <li><a href="#">Gown</a></li> <li><a href="#">Bridesmade</a></li> </ul> </li> <li><a class="menu-title" href="#">Kids <span class="arrow">&#9660;</span></a> <ul class="sub-menu"> <li><a href="#">All</a></li> <li><a href="#">New Arrival</a></li> <li><a href="#">Casual</a></li> <li><a href="#">Gown</a></li> <li><a href="#">Bridesmade</a></li> </ul> </li> <li><a class="menu-title" href="#">Flyers <span class="arrow">&#9660;</span></a> <ul class="sub-menu"> <li><a href="#">All</a></li> <li><a href="#">New Arrival</a></li> <li><a href="#">Casual</a></li> <li><a href="#">Gown</a></li> <li><a href="#">Bridesmade</a></li> </ul> </li> <li><a class="menu-title" href="#">Contact Us</a></li> </ul> </nav> </div> </body> </html> 。只需尝试一次