【问题标题】:Android Studio: Room: error: Cannot find getter for fieldAndroid Studio:房间:错误:找不到字段的吸气剂
【发布时间】:2020-02-24 08:38:31
【问题描述】:

我正在使用房间数据库进行本地缓存。在我的实体中,我有其他对象,它们的 getter 具有与其用途相关的接口的返回类型。但是房间找不到字段的吸气剂。我的目标是将这些接口用作返回类型,以便我可以将来自多个远程源的数据添加到一个数组中。如何在实现灵活和良好抽象的同时做到这一点?

public interface Venue {
  Location getLocation();
  Contact getContact();
}

public interface Location {
  double getLongitude();
  double getLatitude();
}

public interface Contact {
  String getPhoneNumber();
  String getFacebookUsername();
}

public class VenueSourceOne implements Venue{
   private VenueSourceOneLocation venueSourceOneLocation;
   private VenueSourceOneContact venueSourceOneContact;

   Location getLocation(){
      return venueSourceOneLocation;
   }

   Contact getContact(){
      return venueSourceOneContact
   }
}

public class VenueSourceTwo implements Venue{
   private VenueSourceTwoLocation venueSourceTwoLocation;
   private VenueSourceTwoContact venueSourceTwoContact;

   Location getLocation(){
      return venueSourceTwoLocation;
   }

   Contact getContact(){
      return venueSourceTwoContact
   }
}


public class Main{
   ....add to a List<Venue> and do something on the UI
}

【问题讨论】:

    标签: android android-room


    【解决方案1】:

    出于我提出的问题以外的原因,我将场地接口更改为抽象类:

    来自

    public interface Venue {
      Location getLocation();
      Contact getContact();
    }
    

    public abstract class Venue {
          public abstract Location getLocation();
          public abstract Contact getContact();
    }
    

    我的问题的真正解决方案如下:

    来自

    public class VenueSourceOne implements Venue{
       private VenueSourceOneLocation venueSourceOneLocation;
       private VenueSourceOneContact venueSourceOneContact;
    
       Location getLocation(){
          return venueSourceOneLocation;
       }
    
       Contact getContact(){
          return venueSourceOneContact
       }
    }
    
    public class VenueSourceTwo implements Venue{
       private VenueSourceTwoLocation venueSourceTwoLocation;
       private VenueSourceTwoContact venueSourceTwoContact;
    
       Location getLocation(){
          return venueSourceTwoLocation;
       }
    
       Contact getContact(){
          return venueSourceTwoContact
       }
    }
    

    public class VenueSourceOne extends Venue{
       private VenueSourceOneLocation location;
       private VenueSourceOneContact contact;
    
       @Override
       VenueSourceOneLocation getLocation(){
          return location;
       }
    
       @Override
       VenueSourceOneContact getContact(){
          return contact;
       }
    }
    
    public class VenueSourceTwo extends Venue{
       private VenueSourceTwoLocation location;
       private VenueSourceTwoContact contact;
    
       @Override
       VenueSourceTwoLocation getLocation(){
          return location;
       }
    
       @Override
       VenueSourceTwoContact getContact(){
          return contact;
       }
    }
    

    列表在从源中检索数据时会使用抽象类指定的通用类型,而房间会使用特定类型存储数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多