Question

I'm making an android project and don't have much experience in Java.So I'm now able to store objects in db4o database, I can also retreive all of them. But if i would like to get a list of specific items, only 1 item returns (even if there are more).

I'll show you the code I have:

this is the code to get to the database, and this should work. It is a function in a class db4oHelper

public ObjectContainer db() {

    Log.i(Db4oHelper.class.getName(), "Accessing Database...");

    if ((Db4oHelper.objectContainer == null) || ((Db4oHelper.objectContainer != null) && Db4oHelper.objectContainer.ext().isClosed())) {

        Toast.makeText(this.context, "Creating DB Config", Toast.LENGTH_SHORT).show();

        EmbeddedConfiguration c = Db4oEmbedded.newConfiguration();
        c.common().objectClass(Event.class).cascadeOnUpdate(true);
        c.common().objectClass(Event.class).cascadeOnActivate(true);
        c.common().allowVersionUpdates(true);
        c.common().detectSchemaChanges(true);
        c.common().messageLevel(2);

        Db4oHelper.objectContainer = Db4oEmbedded.openFile(c, db4oDBFullPath(this.context));

    }
    return Db4oHelper.objectContainer;
}

and I also have a class EventProvider wich extends db4oHelper class. Where al the code is written to store and get objects. here is the function wich return only 1 object even if there are more. (I made it from the example of db4o tutorial pdf)

public List eventsFriday()
    {
        //function to get events by day (ex all events of friday)

        List <Event> events = db().query(new Predicate<Event>() {
             public boolean match(Event event) {
             return event.getDay().equals("friday");
             }
            });
        return events;
    }

So now i Made the function to get the events of friday, for the moment there are 2. but it only returns 1. anyone an idea?

also I would like to make this function to work for more days , for example friday, saturday and sunday. but if i pass a String with the function I can't use it in the new Predicate function.

something like this:

public List eventsByDay(**String day**)
    {
        //function to get events by day (ex all events of friday)

        List <Event> events = db().query(new Predicate<Event>() {
             public boolean match(Event event) {
             return event.getDay().equals(**day**);
             }
            });
        return events;
    }
Was it helpful?

Solution 2

Ok so i have been searching and trying alot, and I have found something that works for now. gonna have to do some further testing in the future.

 public List eventsByDayTest(final String day)
    {
        //function to get events by day (ex all events of friday)

        Query query=db().query();
        query.constrain(Event.class);
        query.descend("Day").constrain(day);
        ObjectSet result=query.execute();
        return result;
    }

I then catch the return result in a List in the activity class on android.

OTHER TIPS

You can use day if it is declared final :

public List eventsByDay(final String day)
{
    //function to get events by day (ex all events of friday)

    List <Event> events = db().query(new Predicate<Event>() {
         public boolean match(Event event) {
             return event.getDay().equals(day);
         }
    });
    return events;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top