从实时Firebase获取数据并在recyclerview中显示

时间:2019-09-06 07:14:56

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

如何检索所有数据(数据,用户数据)并将其显示在回收者视图中  但我在其他班级(用户)中需要它们

这是JSON数据:

var list = [1, 2, 3, 4];
List<int> temp = [];
List<List<int>> newList = list.fold<List<List<int>>>([], (newList, i) {
  if (temp.length < 2) {
     temp.add(i);
  } 
  if (temp.length >= 2) {
     List<int> newValue = new List<int>.from(temp);
     newList.add(newValue);
     temp.clear();
  }
  return newList;
});
print(newList);

我在此类中写了(“数据”)

{
  "data" : {
    "3XU5Z3Izm3MusCt8prrIRgNQkt52" : {
      "-LmodIxoJ9BtUk62gImX" : {
        "id" : "-LmodIxoJ9BtUk62gImX",
        "notes" : "description",
        "timee" : "3:40 PM",
        "title" : "title"
      },
      "-LmodK25AqrR07Cwoiww" : {
        "id" : "-LmodK25AqrR07Cwoiww",
        "notes" : "description",
        "timee" : "3:41 PM",
        "title" : "title"
      },
      "-LmodKX0SoAbD928REwT" : {
        "id" : "-LmodKX0SoAbD928REwT",
        "notes" : "description",
        "timee" : "3:41 PM",
        "title" : "title"
      }
    }
  },
  "userdata" : {
    "3XU5Z3Izm3MusCt8prrIRgNQkt52" : {
      "job" : "",
      "username" : "f2f."  }
}
    }

我在此类中写(“ userdata”):

class Note{
     var title:String?=null
      var notes:String?=null
     var timee :String?=null
    var id :String?=null
constructor(){}
    constructor(id:String?="",title:String?=""
         ,notes:String?="",timee :String?=""){
        this.id=id
        this.title=title
        this.notes=notes
        this.timee=timee }
}

,我想在班级(“用户”)中展示它们:

class Note2{
    var username: String?=null
    var job:String?=null
constructor(){}
constructor(username: String?="", job: String?="")  {
    this.username=username
    this.job=job
}
}

这是我的MainActivity.class:

data class User (var username:String,
                 var userphoto:Int,
                 var title:String,
                 var notes:String
)

这是我的CustomAdapter.class:

    class RecyclerActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener{
        var mylist2=ArrayList<Note2>()
        var mylist=ArrayList<Note>()
        var currentuser = FirebaseAuth.getInstance().currentUser!!
        var db= FirebaseDatabase.getInstance()
        var ref = db.getReference()
        var aa=db.getReference("userdata")

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_recycler)
       var myarray=ArrayList<User>()
        ref.child("data").child(currentuser.uid)
            .addValueEventListener(object: ValueEventListener {
                override fun onDataChange(p0: DataSnapshot) {
                            var va=p0.value as HashMap<Any?,Any?>
                            for(k in va.keys) {
                                var data = va[k] as HashMap<String?, Any?>               myarray.add(User("",0,data["title"]?.toString()!!,data["notes"]?.toString()!!))
}}
                override fun onCancelled(p0: DatabaseError) {
                    var error=p0.message
                    Toast.makeText(applicationContext,error, Toast.LENGTH_LONG).show()
                }
            })

        aa.child(currentuser.uid)
            .addValueEventListener(object: ValueEventListener {
                override fun onDataChange(p0: DataSnapshot) {
                    var r = p0.getValue(Note2::class.java)
                    if(r!!.username !=null){
                        myarray.add(User(r.username!!,0,"",""))
                    }
                    }
                override fun onCancelled(p0: DatabaseError) {
                    var error=p0.message
                    Toast.makeText(applicationContext,error, Toast.LENGTH_LONG).show()
                }
            })
        val c= CustomAdapter(myarray)
        idrecycler.layoutManager= LinearLayoutManager(this, LinearLayout.VERTICAL,false)
        idrecycler.adapter=c
        }}

我想在回收者视图中显示所有数据(User()中的Note2()。username.User()中的username和Note()。title.User()中的title和Note()。notes在User()。notes中),例如facebook回家,请帮助

REMARQUE:像这样工作,但显示1位用户的数据

0 个答案:

没有答案