Home > Software design >  Sorting retrieved data from realtime database in row and columns and implement search view so every
Sorting retrieved data from realtime database in row and columns and implement search view so every

Time:08-18

the output I am getting

this my Excel class

public class Excel {
        public int Age;
        public String Country;
        public String Date;
        public String First_Name;
        public String Gender;
        public int Id;
        public String Last_Name;
        public String status;

        public Excel() {
        }

        @PropertyName("Age")
        public int getAge() {

            return Age;
        }

        @PropertyName("Country")
        public String getCountry() {

            return Country;
        }

        @PropertyName("Date")
        public String getDate() {

            return Date;
        }

        @PropertyName("First_Name")
        public String getFirst_Name() {

            return First_Name;
        }

        @PropertyName("Gender")
        public String getGender() {

            return Gender;
        }

        @PropertyName("Id")
        public int getId() {

            return Id;
        }


        @PropertyName("Last_Name")
        public String getLast_Name() {
            return Last_Name;
        }


        @PropertyName("status")
        public String getStatus() {
            return status;
        }

        @Override
        public String toString() {
            return "Excel{"  
                    "Age="   Age    ""   ", Country='"   Country   '\''  
                    ", Date='"   Date   '\''  
                    ", First_Name='"   First_Name   '\''  
                    ", Gender='"   Gender   '\''  
                    ", Id="   Id  
                    ", Last_Name='"   Last_Name   '\''  
                    ", status='"   status   '\''  
                    '}';
        }
    }


this my Main Activity class

class MainActivity : AppCompatActivity() {
    private var coursesLV: ListView? = null
    lateinit var coursesArrayList: ArrayList<Excel>
    lateinit var reference: DatabaseReference
   lateinit var Img_set:ImageView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Img_set = findViewById(R.id.Img_set)
        Img_set.setOnClickListener {
            intent= Intent(this,Admin_Info::class.java)
            startActivity(intent)
        }
        coursesLV = findViewById(R.id.idLVCourses)
        coursesArrayList = ArrayList()
        getdata()
    }

    private fun getdata() {
        val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, coursesArrayList)
        reference = FirebaseDatabase.getInstance().getReference("Excel")
        reference.addChildEventListener(object : ChildEventListener {
            override fun onChildAdded(snapshot: DataSnapshot, previousChildName: String?) {
                for (ignored in snapshot.children) {
                    val currentUser = snapshot.getValue(Excel::class.java)!!
                    coursesArrayList.add(currentUser)
                    adapter.notifyDataSetChanged()
                }
            }

            override fun onChildChanged(snapshot: DataSnapshot, previousChildName: String?) {
                adapter.notifyDataSetChanged()
            }

            override fun onChildRemoved(snapshot: DataSnapshot) {

            }

            override fun onChildMoved(snapshot: DataSnapshot, previousChildName: String?) {}
            override fun onCancelled(error: DatabaseError) {}
        })
        coursesLV!!.adapter = adapter
    }
    }


this is my database structure as json file

{
  "Excel": [
    {
      "Age": 32,
      "Country": "United States",
      "Date": "15/10/2017",
      "First_Name": "Dulce",
      "Gender": "Female",
      "Id": 1562,
      "Last_Name": "Abril",
      "status": "open "
    },
    {
      "Age": 25,
      "Country": "Great Britain",
      "Date": "16/08/2016",
      "First_Name": "Mara",
      "Gender": "Female",
      "Id": 1582,
      "Last_Name": "Hashimoto",
      "status": "close"
    },
    {
      "Age": 36,
      "Country": "France",
      "Date": "21/05/2015",
      "First_Name": "Philip",
      "Gender": "Male",
      "Id": 2587,
      "Last_Name": "Gent",
      "status": "open "
    },
    {
      "Age": 25,
      "Country": "United States",
      "Date": "15/10/2017",
      "First_Name": "Kathleen",
      "Gender": "Female",
      "Id": 3549,
      "Last_Name": "Hanner",
      "status": "close"
    },
    {
      "Age": 58,
      "Country": "United States",
      "Date": "16/08/2016",
      "First_Name": "Nereida",
      "Gender": "Female",
      "Id": 2468,
      "Last_Name": "Magwood",
      "status": "open "
    },
    {
      "Age": 24,
      "Country": "United States",
      "Date": "21/05/2015",
      "First_Name": "Gaston",
      "Gender": "Male",
      "Id": 2554,
      "Last_Name": "Brumm",
      "status": "close"
    },
    {
      "Age": 56,
      "Country": "Great Britain",
      "Date": "15/10/2017",
      "First_Name": "Etta",
      "Gender": "Female",
      "Id": 3598,
      "Last_Name": "Hurn",
      "status": "open "
    },
    {
      "Age": 27,
      "Country": "United States",
      "Date": "16/08/2016",
      "First_Name": "Earlean",
      "Gender": "Female",
      "Id": 2456,
      "Last_Name": "Melgar",
      "status": "open "
    },
    {
      "Age": 40,
      "Country": "United States",
      "Date": "21/05/2015",
      "First_Name": "Vincenza",
      "Gender": "Female",
      "Id": 6548,
      "Last_Name": "Weiland",
      "status": "open "
    }
  ]
}

I retrieved Data from realtime database and it came in json but I want to sort it in row and columns, can you help sorting this data into such manner that I get age in age row & column and so on , I want to implement search view on this so I can search data by row and columns like excel sheet, is that possible ?

CodePudding user response:

When you're using a ChildEventListener, there is no need to loop through the children. You should simply use:

override fun onChildAdded(snapshot: DataSnapshot, previousChildName: String?) {
    val currentUser = snapshot.getValue(Excel::class.java)!!
    coursesArrayList.add(currentUser)
    adapter.notifyDataSetChanged()
}

Besides that, don't also forget to handle errors:

override fun onCancelled(error: DatabaseError) {
    error.message?.let {
        Log.d("TAG", it)
    }
}
  • Related