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; }
}
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.
Related
I have a problem with two-way databindig. Here are my classes:
public class User extends BaseObservable {
private String name;
private String surname;
private Address address;
public User() {
}
public User(String name, String surname) {
this.name = name;
this.surname = surname;
}
#Bindable
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
notifyPropertyChanged(BR.address);
}
#Bindable
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
notifyPropertyChanged(BR.name);
}
#Bindable
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
notifyPropertyChanged(BR.surname);
}
}
public class Address extends BaseObservable {
private String name;
public Address(String name) {
this.name = name;
}
#Bindable
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
I am trying to bind User in xml file, but also i want to bind address name. Unfortunately when I use address.setName("abc") my UI doesn't change. I was using notifyPropertyChanged(BR.address) in address setter but it didn't help. It is possible to do it in that way? Here are snippets of my xml file:
<data>
<variable name="user" type="com.rolnik.test.User"/>
</data>
android:text="#={user.name}"
android:text="#={user.surname}"
android:text="#={user.address.name}"
Ok I solved the problem. I put notifyPropertyChanged(BR.name) in address setter and rebuild project. Without the last one it doesn't work.
I am new to using room persistence and I have this error whenever I try running my code. It shows no error when editing but I get build error on gradle. It failed me that I had to copy paste codes but none seemed to work. The error is below
error: An entity must have at least 1 field annotated with #PrimaryKey
How do I work it out?
My code is below;
package com.revosleap.dummy.DatabaseMov;
import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.Ignore;
import android.arch.persistence.room.PrimaryKey;
#Entity
public class TodoListItem {
#Ignore
#PrimaryKey(autoGenerate = true)
private int id;
#ColumnInfo(name = "time")
private String time;
#ColumnInfo(name="title")
private String title;
public TodoListItem(){
}
public TodoListItem(String time, String title) {
this.time = time;
this.title = title;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
You shouldn't have primary key as private or static
Change private int id; this line to
#PrimaryKey
public int id;
more details Room entities
You should not use #Ignore with #PrimaryKey
#Ignore annotation Ignores the marked element from Room's processing
logic.
I have a list specialties having 75 items that has two values id and name.
private List specialties;
I would like to get the name without using a loop something like below
specialties.get(0).name;
I get an error saying can't resolve name. Is there any way to retrieve name from the values list.
Thanks.
Create a class (Model) to get and set the ID and Name property:-
public class ClassName {
private String id;
private String name;
public ClassName(String mId, String mName){
this.id=mId;
this.name=mName;
}
public String getName() {
return name;
}
public void setName(String sName) {
this.name = sName;
}
public String getId() {
return id;
}
public void setId(String sId) {
this.id = sId;
}
}
In your Activity:-
Define a List having the ClassName type of objects.
List<ClassName> mList = new ArrayList<>();
Now access the property name like this:-
mList.get(0).getName();
mList.get(0).getId();
try the following:
public class Clone {
private int Id;
private String name;
public int getId() {
return Id;
}
public String getName() {
return name;
}
public void setId(int id) {
Id = id;
}
public void setName(String name) {
this.name = name;
}
}
and use
private List<Clone> arrayList = new ArrayList();
for (int i = 0; i < 10; i++) {
Clone helloItem = new Clone();
helloItem.setId(i);
helloItem.setName("I'm Clone no - " + i);
arrayList.add(helloItem);
}
Log.d("check", "get item - " + arrayList.get(0).getName());
hope it help.
I want to suggest you. You should declare name with public access. If it is not public, use public getter and setter method.Call getName() method.
public class YourClass{
private int id;
private String name;
public YourClass(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
private List<YourClass> specialties = new ArrayList<YourClass>;
...//add data into list...
specialties.add(new YourClass( 1 , "John"));
...// retrieve data
specialties.get(position).getName();
You have declared your list as private List specialties; you can not access it like this specialties.get(0).name;
You need declare your list like this private List<YourModelClass> specialties;
SAMPLE DEMO
If you want add model class in your list than than check this example
create model class like this
public class User {
String name, email;
public User(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Now use like this in your list
List<User> userArrayList= new ArrayList<>();
To add data inside list like this
userArrayList.add(new User("Nilesh","abc#gmail.com"));
To get data from your list use like this
userArrayList.get(0).getEmail();
userArrayList.get(0).getEmail();
or
for (int i=0;i<userArrayList.size();i++){
userArrayList.get(i).getName();
userArrayList.get(i).getEmail();
}
I wasn't able to find clear documentation on this for the Java SDK. I'm working with Android.
The example say to do something like
#RealmClass
public class User {
#PrimaryKey
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
but can I instead have my setter return the class itself?
public User setName(String name) {
this.name = name;
return this;
}
This way I can do something like
User user = new User().setName("Bob");
instead of
User user = new User();
user.setName("Bob")
Is Realm going to process the setter properly?
Yes, a model class like the below is perfectly valid:
#RealmClass
public class User implements RealmModel {
#PrimaryKey
private String name;
public String getName() {
return name;
}
public User setName(String name) {
this.name = name;
return this;
}
}
I need to create many identical in content classes like the class below
public abstract class AbstractListModel extends RealmObject {
#PrimaryKey
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
If I will extend this class in another, it seems I'll have a lot of empty classes, 'cause they have only 2 fields (id and name) wich contains in the mother-class.
public class LectureHallListModel extends AbstractListModel {
//#PrimaryKey
//private String id;
//private String name;
//public String getId() {
// return id;
//}
//public void setId(String id) {
// this.id = id;
//}
//public String getName() {
// return name;
//}
//public void setName(String name) {
// this.name = name;
//}
}
Are the any way to add to a DB several identical in content tables without creation empty classes?Thank you!
Inheritance of fields (technically from any class that is not directly RealmObject) is not supported by Realm.
You would need the following setup:
public interface AbstractListModel {
String getId();
void setId(String id);
String getName();
void setName(String name);
}
And
public class LectureHallListModel extends RealmObject implements AbstractListModel {
#PrimaryKey
private String id;
private String name;
#Override
public String getId() {
return id;
}
#Override
public void setId(String id) {
this.id = id;
}
#Override
public String getName() {
return name;
}
#Override
public void setName(String name) {
this.name = name;
}
}