简体   繁体   中英

Proper way to extend ParseObject and have a generic query factory

I need a way to query different parse classes based on choices made by a user. I approached the problem as follows. Please advise how to fix the problem I have or suggest alternative/better approach.

I have classes A, B, C, ... that correspond to Parse classes. They are very similar. So, I created abstract class Q :

public abstract class Q extends ParseObject {
    // some shared methods

    public abstract ParseQuery<? extends Q> getQuery();
}

Each of A, B, C, ... is defined as:

@ParseClassName("A")
public class A extends Q {
    private static final A INSTANCE = new A();

    @Override
    public ParseQuery<A> getQuery() {
        return ParseQuery.getQuery(A.class);
    }

    public static A getInstance(){
        return INSTANCE;
    }
}

Than I have the following recycle adapter for my recycle view:

public class QAdapter extends ParseRecyclerQueryAdapter<Q,QAdapter.MyViewHolder>{ ... }

The error I am getting happens on the following line in my activty:

mRecyclerAdapter = new QAdapter(this, factory, true);

The error message is:

Error:(58, 70) error: incompatible types: QueryFactory<CAP#1> cannot be converted to QueryFactory<Q>
where CAP#1 is a fresh type-variable:
CAP#1 extends Q from capture of ? extends Q

I tried to make the definition of adapter class generic as public class QAdapter<T extends Q> extends ParseRecyclerQueryAdapter<T, QAdapter.MyViewHolder> {... } , yet this introduced another error saying that QAdapter did not implement onBindViewHolder even though it is implemented.

This may not work, but I would try replacing

public class QAdapter extends ParseRecyclerQueryAdapter<Q,QAdapter.MyViewHolder>{ ... }

with

public class QAdapter extends ParseRecyclerQueryAdapter<? extends Q,QAdapter.MyViewHolder>{ ... }

Try to replace

extends ParseRecyclerQueryAdapter<T, QAdapter.MyViewHolder> {... }

with

extends ParseRecyclerQueryAdapter<T extends ParseObject, QAdapter.MyViewHolder> {... }

This is how to make my ParseRecyclerQueryAdapter work

Well, as the old saying goes, "make sure everything is contiguous" or something like that. You've got a great idea for a abstract and you're doing it right, but maybe you've just got an inheritance issue by not having the same call issued by the same parameters as met by the processor. like <? extends Q,...> <? extends Q,...> So in that case you've got to make sure it's all redundant. Good luck!

You should make your base class Q generic:

public abstract class Q<T extends Q<T>> extends ParseObject {
    // some shared methods

    public abstract ParseQuery<T> getQuery();
}

Then your class A becomes

@ParseClassName("A")
public class A extends Q<A> {
    private static final A INSTANCE = new A();

    @Override
    public ParseQuery<A> getQuery() {
        return ParseQuery.getQuery(A.class);
    }

    public static A getInstance(){
        return INSTANCE;
    }
}

and ParseQuery is:

public class ParseQuery<T> {
    public static <T> ParseQuery<T> getQuery(Class<T> clazz) {
        return null;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM