1

I have generic problem,

I have LiveData<List<User>>, object User implement IEntity.

How to cast LiveData<List<User>> to LiveData<List<IEntity>>?

  package cz.roomlivedata.entity;
  import  android.arch.persistence.room.Entity;
  import android.arch.persistence.room.Index; 
  import android.arch.persistence.room.PrimaryKey;

  @Entity(indices = {@Index(value = "id", unique = true)}) 
  public class User implements IEntity {

  @PrimaryKey(autoGenerate = true) public int id;

  private String name;

  private String name; public User(String name) { this.name = name; } 

  public String getName() { return name; } 

  public void setName(String name) { this.name = name; } 
}

code

Radesh
  • 13,084
  • 4
  • 51
  • 64
motorcb
  • 1,043
  • 3
  • 10
  • 19
  • add User class code – Radesh Nov 11 '18 at 10:21
  • You say that "User *implements* IEntity" not "... *extends* IEntitiy". But without the latter, you can't add anything from one List to the other – Bö macht Blau Nov 11 '18 at 14:34
  • @Radesh `
    package cz.roomlivedata.entity;
    
    import android.arch.persistence.room.Entity;
    import android.arch.persistence.room.Index;
    import android.arch.persistence.room.PrimaryKey;
    
    @Entity(indices = {@Index(value = "id", unique = true)})
    public class User implements IEntity {
    
        @PrimaryKey(autoGenerate = true)
        public int id;
    
        private String name;
    
        public User(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    `
    – motorcb Nov 11 '18 at 18:07
  • @motorcb did your problem solved? – Radesh Nov 17 '18 at 14:22
  • This is a good question sadly with no good answer – Richard Muvirimi Oct 24 '20 at 16:16

1 Answers1

1

This is how I removed while using kotlin.

abstract class X

class Y: X()

fun getValues(): MutableLiveData<out List<X>> {
    return MutableLiveData<List<Y>>()
}

out keyword helped me. So basically what in and out keyword does have to be done in java manually.

niketshah09
  • 470
  • 3
  • 8