-3

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.enter image description here

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 ?

Seb
  • 2,929
  • 4
  • 30
  • 73
  • can you please add the code for compareTo() method.. how you have overridden that method? to sort you need to implement comparable in your user class and need to compare arrival time.. then you can simply sort list by arrival time using Collections.sort(list); – DhaRmvEEr siNgh Nov 29 '17 at 06:58
  • one question per question! –  Nov 29 '17 at 07:16

1 Answers1

-2

compareTo will only return 1 or -1 or 0 to indicate if comparable have to swap or not so I think there must not b problem with comparable. may b problem is somewhere else.

You can correct me or give your logic another look.

in comparable function return positive(1) instead of real number and for negative(-1)

public int compareTo(O o){ 
 if(o.long>long){
  return 1// instead of long number
 }
 else if(o.long<long){
 return -1 // instead of long number
 }
 else(return 0)// instead of long number
}

i think this can help

Rajan Lagah
  • 2,373
  • 1
  • 24
  • 40
  • 1
    nope `Comparator.naturalOrder()` is the correct way to do it. –  Nov 29 '17 at 07:15
  • okay thanku, i am not aware of that. but can u tell what is wrong with this way. that would b much more helpfull. – Rajan Lagah Nov 29 '17 at 07:36
  • @JarrodRoberson I understand that my question is a duplicate but none of the duplicate show an example of using Comparator.naturalOrder() on my List. it could be great to have an example – Seb Nov 29 '17 at 09:16
  • @RajanLagah thanks, your solution works fine. I have customized it a little bit and implements in the object class – Seb Nov 29 '17 at 09:35
  • hurray!! glad i can help u – Rajan Lagah Nov 29 '17 at 09:41