Rex Yu
Rex Yu

Reputation: 41

Android Room, can only get 1 result from query but checked and there should be more

I checked the database, there should be 4 results with the same group. I checked the insertion everything works fine.

@Dao
public interface MemberDao {
  @Query("Select * from `Member` where id=:groupsId")
  List<Member> getMembers(int groupsId);
}

Here is my method to retrieve data,

public List<Member> getMembers(final Group group){
    LoadData d = new LoadData();
    d.onComplete = this;
    d.execute(group);

    return null;
}

@Override
public void onOutput(List<Member> result) {
    member = result;
    for(Member m : member){
        System.out.println(m.name);
    }
}

public class LoadData extends AsyncTask<Group, Void, List<Member>> {
    private OnTaskComplete onComplete =null;

    @Override
    protected List<Member> doInBackground(Group... groups) {
        List<Member> result = memberDao.getMembers(groups[0].id);
        return result;
    }

    @Override
    protected void onPostExecute(List<Member> result){
        onComplete.onOutput(result);

    }
}

But the result only has 1 in it and there should be 4 member in the List.

so My question is:

  1. if I can get 1 result back, the groupsId is not a problem. So why does it only return 1 result?

  2. I try to just return the whole list of member regardless of the groupsId, and I can get exactly 4 back. So it only happens when I try to specify a groups Id.

Does someone know why it behaves like that ?

P.S. Here is the Member class.

@Entity(foreignKeys = @ForeignKey(parentColumns  =
    "id", childColumns = "groupsId", entity = Group.class))
public class Member {
    @PrimaryKey(autoGenerate = true)
    public int id;
    public String name;
    public long groupsId;

public Member(String name) {
    this.name = name;
}

public void setGroupsId(long groupsId){
    this.groupsId = groupsId;
}

public String getName(){
    return name;
}

enter image description here I posted it here to show the verified data.

Upvotes: 0

Views: 50

Answers (1)

Martin Zeitler
Martin Zeitler

Reputation: 76879

add room annotations:

public class Member {

    @ColumnInfo(name = "user_id")
    @PrimaryKey(autoGenerate = true)
    private Long userId;

    @ColumnInfo(name = "group_id")
    private Long groupId;

    @ColumnInfo(name = "username")
    public String name;

    ...
}

Upvotes: 1

Related Questions