首页 \ 问答 \ 返回具有通用输入的嵌套对象(return nested object with generic input)

返回具有通用输入的嵌套对象(return nested object with generic input)

我有以下对象初始化。 它是RestfulService类的一部分。

ParameterizedTypeReference<RestResponsePage<ScheduledScanDto>> ptr =
            new ParameterizedTypeReference<RestResponsePage<ScheduledScanDto>>(){};

它在课堂上使用了好几次但有不同的参数。 例如

ParameterizedTypeReference<RestResponsePage<ScanDto>> ptr =
            new ParameterizedTypeReference<RestResponsePage<ScanDto>>(){};

要么

ParameterizedTypeReference<RestResponsePage<SomeOtherObject>> ptr =
            new ParameterizedTypeReference<RestResponsePage<SomeOtherObject>>(){};

它有点罗嗦......我试图创建一个私有方法来返回ParameterizedTypeReference对象作为私有方法的一部分。 这将减少类中的样板代码和代码重复。我正在努力使用私有​​方法的语法,特别是泛型的嵌套。

想通过执行以下操作获取ParameterizedTypeReference对象的实例

pageTypeReference(AnyClassObjectHere.class)

给出一些背景信息。 我计划通过在发出休息请求时调用private方法来减少样板代码。 所以看起来像以下..

private List<ScanDto> methodMakeScanRequest{
  List<ScanDto> = restTemplate.exchange("/localhost/scans-url",HttpMethod.GET, pageTypeReference(ScanDto.class)).getBody()
}

更新中...

我的尝试看起来像这样。 这种作品。 但是它现在只是映射到通用链表。 对象类型被忽略。

private <T> ParameterizedTypeReference<RestResponsePage<T>> pageTypeReference(Class<T> clazz) {
    return new ParameterizedTypeReference<RestResponsePage<T>>(){};
}

I have the following object initialization. Its part of a RestfulService class.

ParameterizedTypeReference<RestResponsePage<ScheduledScanDto>> ptr =
            new ParameterizedTypeReference<RestResponsePage<ScheduledScanDto>>(){};

It is used several times in the class but with different params. e.g.

ParameterizedTypeReference<RestResponsePage<ScanDto>> ptr =
            new ParameterizedTypeReference<RestResponsePage<ScanDto>>(){};

or

ParameterizedTypeReference<RestResponsePage<SomeOtherObject>> ptr =
            new ParameterizedTypeReference<RestResponsePage<SomeOtherObject>>(){};

Its a bit wordy... I am trying to create a private method to return the ParameterizedTypeReference object as part of a private method. This will reduce boilerplate code in the class and code duplication.. I'm struggling with the syntax on the private method, in particular the nesting of generics.

Would like to get an instance of ParameterizedTypeReference objects by doing the following

pageTypeReference(AnyClassObjectHere.class)

To give some context. I plan to reduce boilerplate code by calling the private method when making a rest request. So would look like the following..

private List<ScanDto> methodMakeScanRequest{
  List<ScanDto> = restTemplate.exchange("/localhost/scans-url",HttpMethod.GET, pageTypeReference(ScanDto.class)).getBody()
}

Update...

My attempt looks like this. This kind of works. However it now just maps to a generic linkedlist. The object type is ignored.

private <T> ParameterizedTypeReference<RestResponsePage<T>> pageTypeReference(Class<T> clazz) {
    return new ParameterizedTypeReference<RestResponsePage<T>>(){};
}

原文:https://stackoverflow.com/questions/45213604
更新时间:2023-08-27 20:08

最满意答案

我在尝试你的代码后发现了这个问题。 您必须将一些宽度设置为AutoCompleteTextView ,然后您将能够看到弹出窗口。 例如,将width设置为match_parent

<AutoCompleteTextView
    android:id="@+id/editColor"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:imeOptions="actionDone"
    android:inputType="text"
    android:maxLength="30"
    android:maxLines="1" />

这对我来说很完美。


I found the problem after I tried your code. You have to set some width to AutoCompleteTextView and then you will be able to see the pop up. For example set the width to match_parent.

<AutoCompleteTextView
    android:id="@+id/editColor"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:imeOptions="actionDone"
    android:inputType="text"
    android:maxLength="30"
    android:maxLines="1" />

This works perfectly for me.

相关问答

更多
  • 我在尝试你的代码后发现了这个问题。 您必须将一些宽度设置为AutoCompleteTextView ,然后您将能够看到弹出窗口。 例如,将width设置为match_parent 。
    根据http://api.jqueryui.com/autocomplete/#option-source ,从服务器返回的响应可以是: 字符串数组: [ "Choice1", "Choice2" ] 具有label和value属性的对象数组: [ { label: "Choice1", value: "value1" }, ... ] 但看起来服务器将数组包装在一个对象中。 如果服务器可以返回: ["Value 1", "Value 2"]而不是{"obj": ["Value 1", "Value 2"] ...
  • 您的问题似乎是您在使用while语句进行检查时进入无限循环的事实。 使用if语句如下所示: for(int i=0; i largest) ...
  • 你有两个选择: 使您的类实现Comparable ,并在不指定比较器的情况下调用Collections.sort 正确实施比较器 你好像采取了“我试过X并且它不起作用的方法,因此我需要尝试别的东西” - 但X(使用比较器)不起作用的原因似乎是你的比较器有错误。 ..它没有以你想要的方式比较项目。 实现Comparable时,您将遇到完全相同的问题。 你还有相同的基础工作要做(弄清楚如何比较两个项目) - 这实际上只是逻辑在哪里。 通常,实现Comparator是一种更灵活的方法,因为它允许根据您的需要以不同 ...
  • 在displayButtonActionPerformed方法中进行更改,如下所示。 private void displayButtonActionPerformed(java.awt.event.ActionEvent evt) { String str = ""; for (int i = 0; i < cdcollection.size(); i++) { str = str + cdcollection.get(i); } mainTe ...
  • 我们来看看这两个: public class Vector extends AbstractList implements List, RandomAccess, Cloneable, Serializable public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, Serializable 这是两个单独的类,不能互换。 您可以执行以下操作: Abstr ...
  • 在您的情况下,最佳选择是“for each”循环。 它看起来像那样: for(Job job: jobname) { if(job.isUrgent()) System.out.println(job); } In your situation the best option would be "for each" loop. It would look like that: for(Job job: jobname) { if(job.isUrgent()) System.out.p ...
  • if(bitmapArray.size() > 0) { int index = bitmapArray.size() -1; Bitmap lastbitmap = bitmapArray.get(index); SubImageView.setImageBitmap(lastbitmap); } if(bitmapArray.size() > 0) { int index = bitmapArray.size() -1; Bitmap lastbitmap = bitmapArr ...
  • 我相信你正在寻找JTextArea#append ,这将允许你将“附加”文本保留到文本区域的末尾,就像用新文本替换它一样,这就是setText会做的事情。 I believe you're looking for JTextArea#append, which will allow you to keep "appending" text to the end of the text area, as apposed to replacing it with new text, which is what ...
  • 每次调用menuCaller ,都会创建一个新列表。 也许您应该在main方法中创建它并将引用传递给menuCaller : public static void main(String[] args) { String con; List names = new ArrayList<>(); do{ menuCaller(names); System.out.println("Do you want to continue[Y/N]"); ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)