Creating Activity for Visualizing Recorded Sensor Data from List Items
In previous blog Using RealmRecyclerView Adapter to Show Recorded Sensor Experiments[2], I have created a DataLoggerActivity in PSLab Android app containing RecyclerView showing a list having all the recorded experiments where every list item shows the date, time and the sensor instrument used for recording the data, but there arises below questions:- What if the user wants to see the actual recorded data in form of a graph? How the user can see the location recorded along with the data on the map? How can the user export that data? There is no way I could show all of that information just on a list item so I created another activity called “SensorGraphViewActivity” the layout of that activity is shown in the figure below: The layout contains three views:- At the top there is graph view which I created using Android MP chart which will show the recorded data plotted on it forming the exact same curve that was created while recording it, this way it is useful to visualize the data and also there is also a play button on the top which simulates the data as it was being plotted on the graph in real time. In the middle, there is a Card view containing two rows which will simply show the date and time of recording. At the bottom, there is a Map view which shows the location of the user which would be fetched when the user recorded the data. This is the gist of the layout file created for the activity. But now the question arises:- How to show the data in the activity for the item that the user wanted? For that, I implemented click listener on every list item by simply adding it inside the onBindViewHolder() method @Override public void onBindViewHolder(@NonNull final ViewHolder holder, int position) { SensorLogged temp = getItem(position); holder.sensor.setText(temp.getSensor()); Date date = new Date(temp.getDateTimeStart()); holder.dateTime.setText(String.valueOf(sdf.format(date))); holder.cardView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ... }); } and inside the click listener I performed following three steps:- First I stored the position of the item clicked inside a variable. int positionVar = holder.getAdapterPosition(); Then I used that position from the variable to fetch related data from the Realm database by simply using getItem() method which returns the SensorLogged[1] RealmObject at that position as I used a special type of RecyclerView Adapter called as RealmRecyclerViewAdapter[2]. int positionVar = holder.getAdapterPosition(); Then I created an Intent to open the SensorGraphViewActivity and passed the related data (i.e., sensortype, foreignkey, latitude, longitude, timezone, date, time) from SensorLogged[1] object to activity in form of extras. Intent intent = new Intent(context, SensorGraphViewActivity.class); intent.putExtra(SensorGraphViewActivity.TYPE_SENSOR, item.getSensor()); intent.putExtra(SensorGraphViewActivity.DATA_FOREIGN_KEY, item.getUniqueRef()); intent.putExtra(SensorGraphViewActivity.DATE_TIME_START,item.getDateTimeStart()); intent.putExtra(SensorGraphViewActivity.DATE_TIME_END,item.getDateTimeEnd()); intent.putExtra(SensorGraphViewActivity.TIME_ZONE,item.getTimeZone()); intent.putExtra(SensorGraphViewActivity.LATITUDE,item.getLatitude()); intent.putExtra(SensorGraphViewActivity.LONGITUDE,item.getLongitude()); context.startActivity(intent); And, in the SensorGraphViewActivity, I used getIntent() method to fetch all those extra data in the form of Bundle. For showing the data in the graph I used the foreign key fetched from the intent and queried all the LuxData[1] RealmObject containing that foreignkey in the form of RealmResult<LuxData>[2] ArrayList and used that list to populate the…
