I am writing an app in Java in which I want to show a full schedule of the shop and when the person comes in/out and when there is no person. please check the attached picture.
The list of persons are in a JSON file :
{
"venue": {
"id": 123456,
"name": "Foursquare HQ",
"openTime": 1479798000000,
"closeTime": 1479864600000,
"visitors": [
{
"id": 1,
"name": "Dave",
"arriveTime": 1479805200000,
"leaveTime": 1479816000000
},
{
"id": 2,
"name": "Elizabeth",
"arriveTime": 1479801600000,
"leaveTime": 1479819600000
},
{
"id": 3,
"name": "Ben",
"arriveTime": 1479826800000,
"leaveTime": 1479830400000
},...
this json is extracted and can be access by a method called getVisitors and it returned a List. The class Person is declared as below:
public final class Person{
private int id;
private String name;
private long arriveTime;
private long leaveTime;
public Person(int id, String name, long arriveTime, long leaveTime){
this.id = id;
this.name = name;
this.arriveTime = arriveTime;
this.leaveTime = leaveTime;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public long getArriveTime() {
return arriveTime;
}
public long getLeaveTime() {
return leaveTime;
}
}
I am looking for a way to first sort the List returned by the getVisitors by ascending order to get from the first to the last one. I was thinking using the arrival time and using
Collections.sort(list, new Comparable()...)
the problem is that I need to override compareTo method but it's rejected because compareTo is returning integer and my arrival time is a long.
My other issue is how to add the "No visitors" items ? the first one and last one can be added using
list.add(idx, element)
but I do not see an easy algorithm to find the empty spot during the day between visitors.
I am using a RecycledView to display the information as a list as shown below :
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private RecyclerView rvRecyclerView;
private static PersonAdapter personAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rvRecyclerView = findViewById(R.id.rvRecyclerView);
rvRecyclerView.setLayoutManager(new LinearLayoutManager(this));
if(personAdapter == null)
personAdapter = new PersonAdapter();
//set the adapter of the recycler view to push data.
rvRecyclerView.setAdapter(personAdapter);
Log.d(TAG, "ON CREATE CALL");
new VenueFetcher(this).execute();
}
VenueFetch is an AsyncTask who is in charge of extracting the json from the json file and use the call to
mainActivity.personAdapter.notifyDataSetChanged();
to update the list that part work
This is not a duplicate as I also looking for empty slot in time range. Based on the list of visitors and the open/closing time of the shop, I need to find all slot where there is no visitors
Any idea ?