Can RealmClass setter use method chaining? - android

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;
}
}

Related

How to Populate Spinner in Android using retrofit?

I am new to android app development and I am working on my final year project. I have been trying to get the spinner to populate from an SQL server using retrofit but I can't find any help that could solve my problem can anyone guide me? Thanks
POjO CLASS
public class Category2 {
#SerializedName("Name")
#Expose
private String name;
#SerializedName("Date")
#Expose
private String date;
#SerializedName("Deleted")
#Expose
private String deleted;
#SerializedName("Category_id")
#Expose
private int categoryId;
public Category2(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDeleted() {
return deleted;
}
public void setDeleted(String deleted) {
this.deleted = deleted;
}
public int getCategoryId() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}
#NonNull
#Override
public String toString() {
return name;
}
}
Well, It would be helpful if you could share more info about your project. Things like the activity / fragment where your spinner is at.
Well, the content of a spinner can be passed as argument when creating the adapter.
You can make the request using retrofit. Save the data as a Array/List. Pass it to the adapter. Set the spinner adapter.

Android LiveData<List<User>> cast to LiveData<List<?extednds IEntity>>

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.

two-way databinding

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.

How to retrieve a value from list android

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();
}

get a reference to a list inside another class

In my app I have a list of Trucks that are displayed in a lisview, and every Truck has a list of cases inside it. My question is how do I get a reference to that list of cases inside the truck from the main activity.
public class Truck {
private String name;
public List<Roadcase> cases;
public Truck() {
cases = new ArrayList<Roadcase>();
}
public Truck(String name) {
this.name = name;
this.cases = new ArrayList<Roadcase>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addRoadcase(Roadcase roadcases) {
cases.add(roadcases);
}
public void deleteRoadcase(Roadcase roadcases){
cases.remove(roadcases);
}
public int numberOfRoadcases(){
return cases.size();
}
public List<Roadcase> getList() {
return cases;
}
}
my Truck class.
Thanks in advance.
You should call the method getList from your activity (
public List<Roadcase> getList() {
return cases;
}
)
For example:
Truck truck = new Truck();
List<Roadcase> list = truck.getList();

Categories

Resources