Android Java:内联对象初始化无法正常工作
我的应用中有一个AWTrack类,如下所示:
import android.database.sqlite.SQLiteDatabase;
import androidx.annotation.NonNull;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import kaaes.spotify.webapi.android.models.Track;
public class AWTrack implements Serializable {
private int id;
private String idSpotify;
private String name;
private String album;
private String author;
private String spotifyPreviewUrl;
private int musicType;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getIdSpotify() {
return idSpotify;
}
public void setIdSpotify(String idSpotify) {
this.idSpotify = idSpotify;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
private int getMusicType() {
return musicType;
}
public void setMusicType(int musicType) {
this.musicType = musicType;
}
public String getSpotifyPreviewUrl() {
return spotifyPreviewUrl;
}
public void setSpotifyPreviewUrl(String spotifyPreviewUrl) {
this.spotifyPreviewUrl = spotifyPreviewUrl;
}
public AWTrack(){
}
public AWTrack(int id)
{
this.id = id;
}
public static AWTrack map(Track track, int trackId)
{
/*AWTrack awTrack = new AWTrack() {{
setId(trackId);
setSpotifyPreviewUrl(track.preview_url);
setName(track.name);
setAuthor(track.artists.get(0).name);
}};*/
AWTrack awTrack = new AWTrack();
awTrack.setId(trackId);
awTrack.setSpotifyPreviewUrl(track.preview_url);
awTrack.setName(track.name);
awTrack.setAuthor(track.artists.get(0).name);
return awTrack;
}
}
在此类中,我创建了一个静态地图方法,旨在铸造kaaes.spotify.webapi.android.models.models.track对象进入AWTRACK(仅是所需的属性),我正在尝试要以下面的方式使用内联对象初始化:
public static AWTrack map(Track track, int trackId)
{
AWTrack awTrack = new AWTrack() {{
setId(trackId);
setSpotifyPreviewUrl(track.preview_url);
setName(track.name);
setAuthor(track.artists.get(0).name);
}};
}
但是我面临一个奇怪的问题,而所产生的AWTRACK对象中有一个嵌入式(且意外的)跟踪对象。
相反,如果我确实以传统方式反对初始化 - 就像下一个:
public static AWTrack map(Track track, int trackId)
{
AWTrack awTrack = new AWTrack();
awTrack.setId(trackId);
awTrack.setSpotifyPreviewUrl(track.preview_url);
awTrack.setName(track.name);
awTrack.setAuthor(track.artists.get(0).name);
return awTrack;
}
它正常工作,但事实是我希望能够像第一个方法中所示的那样进行对象初始化,那么绝对喜欢第一个语法。
编辑:为了清楚起见,我的主要问题是在创建对象后,我会与gson序列化:
ArrayList<AWTrack> list = .....
Gson gson = new Gson();
String json = gson.toJson(list);
如果我将序列化应用于以经典方式实例化的对象,我会得到下一个字符串:
[{"author":"Philip Wesley","id":70,"musicType":0,"name":"Loves Crush","spotifyPreviewUrl":"http://xxx"}]
但是,使用内联固化在与Gson序列化之后,我得到的是:
"[null]"
任何帮助都将不胜感激。
I have a AWTrack class in my app as follows:
import android.database.sqlite.SQLiteDatabase;
import androidx.annotation.NonNull;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import kaaes.spotify.webapi.android.models.Track;
public class AWTrack implements Serializable {
private int id;
private String idSpotify;
private String name;
private String album;
private String author;
private String spotifyPreviewUrl;
private int musicType;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getIdSpotify() {
return idSpotify;
}
public void setIdSpotify(String idSpotify) {
this.idSpotify = idSpotify;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
private int getMusicType() {
return musicType;
}
public void setMusicType(int musicType) {
this.musicType = musicType;
}
public String getSpotifyPreviewUrl() {
return spotifyPreviewUrl;
}
public void setSpotifyPreviewUrl(String spotifyPreviewUrl) {
this.spotifyPreviewUrl = spotifyPreviewUrl;
}
public AWTrack(){
}
public AWTrack(int id)
{
this.id = id;
}
public static AWTrack map(Track track, int trackId)
{
/*AWTrack awTrack = new AWTrack() {{
setId(trackId);
setSpotifyPreviewUrl(track.preview_url);
setName(track.name);
setAuthor(track.artists.get(0).name);
}};*/
AWTrack awTrack = new AWTrack();
awTrack.setId(trackId);
awTrack.setSpotifyPreviewUrl(track.preview_url);
awTrack.setName(track.name);
awTrack.setAuthor(track.artists.get(0).name);
return awTrack;
}
}
In this class I've created a static map method intended to cast kaaes.spotify.webapi.android.models.Track object into AWTrack (only the needed properties) and I'm trying to use inline object initialization in the following way:
public static AWTrack map(Track track, int trackId)
{
AWTrack awTrack = new AWTrack() {{
setId(trackId);
setSpotifyPreviewUrl(track.preview_url);
setName(track.name);
setAuthor(track.artists.get(0).name);
}};
}
But I'm facing a weird problem, and it's that the resulting AWTrack object has an embedded (and unexpected) Track object in it.
If, on the contrary, I do object initialization in the traditional way -like the next-:
public static AWTrack map(Track track, int trackId)
{
AWTrack awTrack = new AWTrack();
awTrack.setId(trackId);
awTrack.setSpotifyPreviewUrl(track.preview_url);
awTrack.setName(track.name);
awTrack.setAuthor(track.artists.get(0).name);
return awTrack;
}
it works correctly, but the fact is I'm expecting to be able to do object initialization like the one shown in the first method, definitely prefer the first syntax.
EDIT: To make it clear, my main problem is after creating the object I serialise it with Gson like this:
ArrayList<AWTrack> list = .....
Gson gson = new Gson();
String json = gson.toJson(list);
And if I apply serialisation to the object instantiated in the classic way I get a string like the next:
[{"author":"Philip Wesley","id":70,"musicType":0,"name":"Loves Crush","spotifyPreviewUrl":"http://xxx"}]
But with the inline instantiation what I get after serialisation with Gson is:
"[null]"
Any help will be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我会回答自己的问题。
答案很简单,据我阅读了{{}}的内联初始化,创建一个匿名的内部类( Java中的双支架初始化是什么?在许多情况下,它可以起作用,我喜欢该语法,但是Gson不会接受该对象作为序列化的参数。
我最终经历了传统的构造方法。最后,这很明显,只有一行代码:
然后是:
Ok, I'll answer my own question.
The answer is simple, as far as I've read the inline initialisation with {{}} creates an anonymous inner class (What is Double Brace initialization in Java?), so it's not the way you would expect it to work and may produce memory leaks if you do lots of initialisations. In many cases it could work, and I like that syntax, but Gson won't accept that object as a parameter for serialisation.
I've ended up going through the traditional constructor way. In the end it's clear and only one line of code:
And then: