two-way databinding - android

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.

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.

How to access value of parent and child both class from pojo using databinding android

My respone of pojo class is like:
{
"data": {
"first_name": "test",
"last_name": "test123",
"email": "test#gmail.com",
"user_id": "11"
},
"message": "login successfully.",
"status": "success."
}
and i want access both parent and child class like message and email from above response. How can i get using single pojo..?
Here i am facing null pointer exception.
My code is:
<data>
<variable name="MyParent"
type="com.package.ParentModel"/>
</data>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="#={MyParent.data.email}"
android:id="#+id/et_email"/>
how can get property of parent and child both class of same pojo..?
I tested it. Strange! It was working fine. I am posting sample code here, you have to match & find issue your own.
activity_sample.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="response"
type="com.innovanathinklabs.sample.data.Response" />
</data>
<LinearLayout
...
>
<EditText
...
android:text="#={response.data.email}" />
</LinearLayout>
</layout>
Response.java (parent model)
public class Response extends BaseObservable {
private Data data;
private String message;
private String status;
public void setData(Data data) {
this.data = data;
}
public Data getData() {
return data;
}
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setStatus(String status) {
this.status = status;
}
public String getStatus() {
return status;
}
}
Data.java (child model)
public class Data extends BaseObservable {
private String userId;
private String lastName;
private String firstName;
private String email;
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserId() {
return userId;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
public void setEmail(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
}
ActivitySample.java
public class ActivitySample extends AppCompatActivity {
ActivitySampleBinding binding;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_sample);
// make dummy response data
Response response = new Response();
Data data = new Data();
data.setEmail("khermaj#gmail.com");
response.setData(data);
// pass dummy data in layout
binding.setResponse(response);
}
}
*Things to check :
Check your model should extend BaseObservable to use two-way data binding. Or you can use ObservableField too.
variable names should start from lower letter. Must check Java Naming Convensions. You have taken variable name="MyParent"
You will get NullPointerException if you have not passed model to Layout. Like I did binding.setResponse(response); in onCreate().
Please refer this model class. It will help you.
public class ParentModel {
private String name;
private ChildModel childModel;
public ParentModel(String name) {
this.name = name;
childModel=new ChildModel();
childModel.setName("Apple");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ChildModel getChildModel() {
return childModel;
}
public void setChildModel(ChildModel childModel) {
this.childModel = childModel;
}
public class ChildModel {
public String name ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}

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

Can RealmClass setter use method chaining?

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

How to be with identical in content classes using by Realm

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

Categories

Resources