简体   繁体   English

正确的方法来扩展ParseObject并拥有一个通用的查询工厂

[英]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. 我有类, A, B, C, ...对应于Parse类。 They are very similar. 它们非常相似。 So, I created abstract class Q : 所以,我创建了抽象类Q

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

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

Each of A, B, C, ... is defined as: A, B, C, ...A, B, C, ...定义为:

@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. 我尝试将适配器类的定义设为通用的public class QAdapter<T extends Q> extends ParseRecyclerQueryAdapter<T, QAdapter.MyViewHolder> {... } ,但这引入了另一个错误,即QAdapter没有实现onBindViewHolder即使它是实现。

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 这是如何使我的ParseRecyclerQueryAdapter工作

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,...> <? extends Q,...> So in that case you've got to make sure it's all redundant. <? extends Q,...>所以在这种情况下,你必须确保它是多余的。 Good luck! 祝好运!

You should make your base class Q generic: 您应该使您的基类Q通用:

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

    public abstract ParseQuery<T> getQuery();
}

Then your class A becomes 然后你的A级就变成了

@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: 和ParseQuery是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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