将泛型类和对象传递给Java方法

时间:2017-08-15 14:59:56

标签: java android generics

我想用泛型替换以下内容:

  • for (Post post : postResponse.getResults()) {,其中Post可以是任何POJO。
  • List<Post> posts = postResponse.getResults();其中List<Post>可以是我传入其中的任何内容的列表。

我的方法调用和方法体看起来是什么样的?

方法调用的不同示例:

retrieveData(mCardAdapter, new Post(), Post.class);
retrieveData(mCardAdapter, new Contact(), Contact.class);
retrieveData(mCardAdapter, new Product(), Product.class);
retrieveData(mCardAdapter, new Booking(), Booking.class);

方式:

private void retrieveData(final CardAdapter mCardAdapter, final Object<T> postObject, Class<T> postClass) {
        RetrofitService service = ServiceFactory.createRetrofitService(RetrofitService.class, RetrofitService.SERVICE_ENDPOINT);
        service.getPosts()
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<PostResponse>() {
                    @Override
                    public final void onCompleted() {
                        setRefreshingFalse();
                        Log.e("RetrofitService", "Retrofit Request Completed!");
                    }

                    @Override
                    public final void onError(Throwable e) {
                        setRefreshingFalse();
                        Log.e("RetrofitService", e.getMessage());
                    }

                    @Override
                    public final void onNext(PostResponse postResponse) {
                        if (postResponse != null) {

                            Log.e("RetrofitService", "Returned objects: " + postResponse.getResults());

                            for (postObject post : postResponse.getResults()) {
                                Log.e("RetrofitService", post.getObjectId() + ": " + post.getText());
                            }

                            setRefreshingFalse();

                            mCardAdapter.clear();
                            List<postClass> posts = postResponse.getResults();
                            mCardAdapter.addData(posts);

                        } else {
                            Log.e("RetrofitService", "Object returned is null.");
                        }

                    }

                });
    }

我收到了Unknown class: 'tClass'Unknown class: 'postClass'。显然这不是这样做的方式,所以也许把我上面展示的内容视为pseduo-code。我正在尝试的是否有意义?我真的想要生成这个retrieveData方法,以便我可以用来查询差异类。

帮助理解。 我想避免的事情:

retrievePosts(mCardAdapter);
retrieveUsers(mCardAdapter);

private void retrievePosts(final CardAdapter mCardAdapter) {
        RetrofitService service = ServiceFactory.createRetrofitService(RetrofitService.class, RetrofitService.SERVICE_ENDPOINT);
        service.getPosts()
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<PostResponse>() {
                    @Override
                    public final void onCompleted() {
                        setRefreshingFalse();
                        Log.e("RetrofitService", "Retrofit Request Completed!");
                    }

                    @Override
                    public final void onError(Throwable e) {
                        setRefreshingFalse();
                        Log.e("RetrofitService", e.getMessage());
                    }

                    @Override
                    public final void onNext(PostResponse postResponse) {
                        if (postResponse != null) {
                            Log.e("RetrofitService", "Returned objects: " + postResponse.getResults());

                            for (Post post : postResponse.getResults()) {
                                Log.e("RetrofitService", post.getObjectId() + ": " + post.getText());
                            }

                            /*for (Post post : postResponse.getResults()) {
                                mCardAdapter.addData(post);
                            }*/

                            setRefreshingFalse();

                            mCardAdapter.clear();
                            List<Post> posts = postResponse.getResults();
                            mCardAdapter.addData(posts);

                        } else {
                            Log.e("RetrofitService", "Object returned is null.");
                        }

                    }

                });
    }

    private void retrieveUsers(final CardAdapter mCardAdapter) {
        RetrofitService service = ServiceFactory.createRetrofitService(RetrofitService.class, RetrofitService.SERVICE_ENDPOINT);
        service.getUsers()
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<PostResponse>() {
                    @Override
                    public final void onCompleted() {
                        setRefreshingFalse();
                        Log.e("RetrofitService", "Retrofit Request Completed!");
                    }

                    @Override
                    public final void onError(Throwable e) {
                        setRefreshingFalse();
                        Log.e("RetrofitService", e.getMessage());
                    }

                    @Override
                    public final void onNext(PostResponse postResponse) {
                        if (postResponse != null) {
                            Log.e("RetrofitService", "Returned objects: " + postResponse.getResults());

                            for (User user : userResponse.getResults()) {
                                Log.e("RetrofitService", user.getObjectId() + ": " + user.getText());
                            }

                            /*for (Post post : postResponse.getResults()) {
                                mCardAdapter.addData(post);
                            }*/

                            setRefreshingFalse();

                            mCardAdapter.clear();
                            List<User> users = userResponse.getResults();
                            mCardAdapter.addData(users);

                        } else {
                            Log.e("RetrofitService", "Object returned is null.");
                        }

                    }

                });
    }

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,您希望为不同的类提供通用方法。我没有看到你在列表中添加了一些东西,所以这可能适合你。

private <clazz> void retrieveData(final Class<?> clazz) {
 for (clazz post : postResponse.getResults()) {
    // you can't do anything here since clazz can be anything
  }
  List<clazz> posts = postResponse.getResults();
}