0

how to retrieve all data (data, user data) and show them in recycler view but I need them in other class (User)

here is JSON data :

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

I write ("data")in this class

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

I write ("userdata")in this class :

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

and I wanted to show them in this class ("User"):

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

here is my MainActivity.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
        }}

here is my CustomAdapter.class :

class CustomAdapter (var mylist:ArrayList<User>)
    :RecyclerView.Adapter<CustomAdapter.viewHolde>(){
    val str= FirebaseStorage.getInstance().reference
    var currentuser = FirebaseAuth.getInstance().currentUser!!.uid
    var e = str.child(currentuser)
 var c:Context?=null
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): viewHolde {

        var v=LayoutInflater.from(parent.context)
            .inflate(R.layout.iktem_list,parent,false)
        return viewHolde(v)
    }

    override fun getItemCount(): Int {
        return mylist.size
    }

    override fun onBindViewHolder(holder: viewHolde, position: Int) {
       holder.username.setText(mylist.get(position).username)
        holder.userimage.setImageResource(R.drawable.user)
        holder.title.setText(mylist.get(position).title)
        holder.decription.setText(mylist.get(position).notes)

    }
    class viewHolde(item:View):RecyclerView.ViewHolder(item){
val username=item.findViewById(R.id.iduser)as TextView
        val userimage=item.findViewById(R.id.idcircleImageView)as CircleImageView
        val title=item.findViewById(R.id.idtitle)as TextView
        val decription=item.findViewById(R.id.iddes)as TextView
}}

I want to show all data in recycler view (Note2().username in User().username and Note().title in User().title and Note().notesin User().notes) like facebook home, pleaze help

REMARQUE: it is working like that but it shows data for 1 user

  • you are not parsing the hashmap correctly I guess. Please format your code and remove unnecessary spaces . – Pemba Tamang Sep 06 '19 at 07:58
  • I organized my code format please see the code again – Bilal Taghda Sep 06 '19 at 08:35
  • use this to parse your hashmap https://stackoverflow.com/a/47204401/8528047 log the values in your for loop and see the main thing is setup your recyclerview and set the adapter first and just call the notifydatasetchanged on the adapter from the ui thread. btw I cannot see any notifydatasetchanged in your code – Pemba Tamang Sep 06 '19 at 08:46
  • map.forEach { key, value -> println("$key = $value") } ,how I will do in my case – Bilal Taghda Sep 06 '19 at 09:01
  • he is just printing the string using `$` to print the variables u just log them in your way and make sure that the flow is correct, set up the recycyclerview, set the adapter, get the data and call onNotifydatasetchanged on the adapter – Pemba Tamang Sep 19 '19 at 04:33

0 Answers0