我正在从一个巨大的Jsonfile创建一个非常简单的自定义对象列表。这个简单的列表包含原始解析的JSON的大约1/8到1/10(大约400 kb,完整版本是3mb),我可以丢弃它。我只使用此列表中需要的数据,因此如果支持两种语言,在我的简单列表中我只放置了我需要的那种语言。这个列表我序列化,所以我不需要在启动时一直解析我的所有数据。
该对象应包含一些纯文本数据,以及两个包含查看自定义对象的列表。但是当我试图填写这些子列表时,我的问题就出现了。错误为No enclosing instance of type SimpleVenue is accessible. Must qualify the allocation with an enclosing instance of type SimpleVenue (e.g. x.new A() where x is an instance of SimpleVenue).
我以为我明白了,所以我创建了一个包含三个对象的顶级对象:
实际上额外的级别感觉有点不必要,我希望两个列表对象是普通数据对象的子级。 任何人都知道我需要改变什么?
我的班级:
import java.io.Serializable;
import java.util.Date;
import java.util.List;
public class SimpleVenue implements Serializable, Comparable<SimpleVenue>{
/**
*
*/
private static final long serialVersionUID = 1L;
public SimpleVenueData simplevenuedata;
public List <SimplePhotos> simplephotos;
public List <SimpleOpeningHours> openinghours;
public class SimpleVenueData implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public int ID;
public String name;
public String category;
public String address;
public String city;
public String tip;
public String phone;
public String website;
public String foursquare;
public float latitude;
public float longtitude;
public String theme;
public String icon;
public String exception;
//setters
public void setId(int id){
ID=id;
}
public void setName(String vname){
name=vname;
}
public void setCategory(String icon){
if(icon.equals("eten.png") || icon.equals("uitgaan.png")){
category="food";
}else if(icon.equals("monument.png")){
category="sights";
}else if(icon.equals("winkelen.png")){
category="shops";
}
}
public void setAdress(String vaddress){
address = vaddress;
}
public void setCity(String vcity){
city=vcity;
}
public void setTip(String vtip){
tip=vtip;
}
public void setPhone(String vphone){
phone=vphone;
}
public void setWebsite(String vwebsite){
website=vwebsite;
}
public void setFoursquare(String vfoursquare_link){
foursquare=vfoursquare_link;
}
public void setLatitude(float vlatitude){
latitude=vlatitude;
}
public void setLongitude(float vlong){
longtitude=vlong;
}
public void setTheme(String vtheme){
theme=vtheme;
}
public void setIcon(String vicon){
icon=vicon;
}
public void setException(String vexception){
exception = vexception;
}
}
public class SimplePhotos implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public String medium;
public String large;
public void setMedium(String vmedium){
medium=vmedium;
}
public void setLarge(String vlarge){
large=vlarge;
}
public String getMedium(){
return medium;
}
public String getLarge(){
return large;
}
}
public class SimpleOpeningHours implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public String day;
public boolean closed;
public Date starttime;
public Date endtime;
//setters
public void setDay(String vday){
day=vday;
}
public void setClosed(boolean vclosed){
closed=vclosed;
}
public void setStartime(Date vstarttime){
starttime=vstarttime;
}
public void setEndime(Date vendtime){
endtime=vendtime;
}
//getters
public String getDay(){
return day;
}
public boolean isClose(){
return closed;
}
public Date getStarttime(){
return starttime;
}
public Date getEndtime(){
return endtime;
}
}
@Override
public int compareTo(SimpleVenue another) {
// XXX Auto-generated method stub
return 0;
}
}
使用时:
List<SimpleVenue> sv = new ArrayList<SimpleVenue>();
int selectedPic;
JJsonResponse e;
int k =0; //indicator 1e SimpleVenue
JVenueThemes jtheme;
SimpleVenue tempSv;
SimpleVenueData tempSvd;
SimplePhotos tempPhoto;
int selectedCounter;
for(int i=0;i < jsonResponse.size() ;i++){
e = jsonResponse.get(i);
if(e.venue.hidden == false){ //staat aan
for(int j=0; j<e.venue.themes.size();j++){ //loop door alle themes
if (e.venue.themes.get(j).mobile == true){ //als theme is true
jtheme = e.venue.themes.get(j);
sv.add(new SimpleVenue());
tempSv=sv.get(k);
tempSvd=sv.get(k).simplevenuedata;
tempSvd.setId(k);
tempSvd.setName(e.venue.name);
tempSvd.setAdress(e.venue.address);
tempSvd.setCity(e.venue.city);
tempSvd.setPhone(e.venue.phone);
tempSvd.setWebsite(e.venue.website);
tempSvd.setFoursquare(e.venue.foursquare_link);
tempSvd.setLatitude(e.venue.latitude);
tempSvd.setLongitude(e.venue.longitude);
tempSvd.setCategory(jtheme.icon);
tempSvd.setIcon(jtheme.icon);
// language depending
if (language.equalsIgnoreCase("nld")){ //dutch
tempSvd.setTip(e.venue.tip);
tempSvd.setTheme(jtheme.name);
}else{ //english
tempSvd.setTip(e.venue.tip_en);
tempSvd.setTheme(jtheme.name_en);
}
// selecting pics
if (e.venue.venue_photos.isEmpty() == false){
for(int l=0; l < e.venue.venue_photos.size() ;l++){
selectedCounter=0;
if(e.venue.venue_photos.get(l).selected == true){
sv.get(k).simplephotos.add(new SimplePhotos());
// tempSv.simplephotos.add(new SimplePhotos());
// tempSv.simplephotos.get(selectedCounter).large=e.venue.venue_photos.get(selectedCounter).large;
// tempSv.simplephotos.get(selectedCounter).medium=e.venue.venue_photos.get(selectedCounter).medium;
// selectedCounter++;
}
}
}else{
tempSv.simplephotos.add(new SimpleVenue.SimplePhotos());
tempSv.simplephotos.get(0).large=null;
tempSv.simplephotos.get(0).medium=null;
}
// selecting opening hours
//testrun before entering opening hours
Log.i("testrun", "k= " + String.valueOf(k));
k++;
}
}
}
jsonResponse.remove(i); //
}
我甚至没有开始使它具有可比性,所以不要介意。但最终我会在名单和主题等一些项目上订购我的名单。
答案 0 :(得分:0)
你需要 SimpleVenueData
来引用外部类的实例吗?如果没有,只需将其设为 static 嵌套类即可。 (或者完全拉出所有这些嵌套类......)