I'm quickly realizing this is going to be an issue in Android with a lot of boilerplate and as I started refactoring my code I'm now effectively writing my own half-#ssed version of data binding. I don't want to spend more time generalizing it and re-inventing the wheel. I was wondering if there are any good solutions out there as 3rd party libraries that the community uses.
I've found robo-bindings and I really liked their presentation (focus on unit testing their own stuff, robustness, etc) but it seems like they remain quite small and I'm worried about issues with their library and general support/evolution going forward.
Any other libraries people are using?
Thanks.
Android M will provide powerful library for data binding!
It's available now in dev-preview version.
It looks amazing inside xml and java files:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{user.firstName}"
/>
Java bean:
public class User {
private final String firstName;
private final String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
}
Binding:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);
User user = new User("Test", "User");
binding.setUser(user);
}
Related
There are two different apps that are connected to the same database. One is sort of an admin app that updates and deletes data in the database and the other is sort for the user app which only receives the data in the app. I have used the model class in the admin app to update and delete the data. I want to show this data in the user's app using firebase recycler but for which I require model class that was used how can I achieve that??
MY MODEL CLASS
package com.parth.iitktimes;
import java.io.Serializable;
public class Creator implements Serializable {
private String name,design,email,phone,downloadUrl,uniquekey;
public String getDownloadUrl() {
return downloadUrl;
}
public void setDownloadUrl(String downloadUrl) {
this.downloadUrl = downloadUrl;
}
public String getUniquekey() {
return uniquekey;
}
public void setUniquekey(String uniquekey) {
this.uniquekey = uniquekey;
}
public Creator(String name, String design, String email, String phone, String downloadUrl, String uniquekey) {
this.name = name;
this.design = design;
this.email = email;
this.phone = phone;
this.downloadUrl = downloadUrl;
this.uniquekey = uniquekey;
}
//empty constructor
public Creator() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesign() {
return design;
}
public void setDesign(String design) {
this.design = design;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
MY FIREBASE CONSOLE
images of firebase console
Architecture
enter image description here
Your question is not clear enough to answer correctly, but i think you are trying to access common classes of your app (like data model) from another.
You should consider using module for your project, generally speaking the code you write for your one app can't directly or indirectly access your other app's data and class even if both app is build by you.
(Technically it can access using reflection and other methods but you want to use same codebase)
There are many benefits of building modular app check out this 2019 IO talk
okay, so how you can access your common classes in both client and admin app?
create a new project (in android studio), create a new library module in same project, again create a new app module in same project.
what you got is two app and one library in one project, you can use that library as a dependency for both of your apps and write common classes in that library.
If I Right with the question,
In Firebase Project Overview......where you used firebase for Admin App and choose Add App...you can add User App to connect two app in single Firebase
I am working on a new Android project and am using the new Architecture components but could use some help in some scenarios, as I do not understand the best solution.
I have an object such as below
public class Team {
String name;
String location;
List<User> users;
}
public class User {
String firstName;
String lastName;
}
For the ViewModel should the mutableLiveData be the Team object or each individual property, the same for the users, I will be updating and users and wonder if how I should observe those changes
I am currently working on an app that uses Firebase's real-time database and data binding for displaying. To keep it simple, here's a simple version of the problem:
Given a model class:
public class User {
private String name;
private Date date;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Date getDate() { return date; }
public void setDate(Date date) { this.date = date; }
}
And a ViewModel class for the users:
public class UserViewModel {
private User user;
public void setUser(User user) {
this.user = user;
}
public String getName() { return user.getName() }
public void setName(String name) { user.setName(name); }
public String getDateAsString() { // ... }
}
Now, in the activity/fragment I have a RecyclerView rendering a list of users. So within the adapter's onCreateViewHolder() I inflate a layout using DataBindingUtils, create a new ViewHolder and a new UserViewModel instance which accesses the UI. In onBindViewHolder() the UserViewModel gets assigned with the according User instance.
So far, so good: Given a list of users, its items get rendered into the RecyclerView through the UserViewModel.
For the app, I also use Firebase to read and write to the Realtime database. So when I now get a callback that a User entry has been updated, I directly modify the infos in the according instance.
So now to the question: How do I inform the UserViewModel that the data has changed and that it needs to redraw the according views in the UI?
I know one step I need to do is to have UserViewModel extend BaseObservable, mark the methods with #Bindable and add calls to notifyPropertyChanged(int) in the setters of the ViewModel. But this doesn't solve the problem of how to inform the UserViewModel of an update to the model data.
Any help and example code is appreciated! Thx! :)
You don't need to extend UserViewModel with BaseObservable, but you can. I'll show another way how you can achieve this.
Personally, I prefer to create a ObservableField<User> in my UserViewModel, create getters and setters like:
private final ObservableField<User> userField = new ObservableField<User>();
public UserViewModel(User user){
userField.set(user);
}
public ObservableField<User> getUser(){
return userField;
}
pass it to the layout and reference the properties like this:
<variable
name="userViewModel"
type="your.package.UserViewModel" />
<EditText
android:text"#={userViewModel.user.name}" />
Whenever your user changes his Name in your EditText, the changes are also updated in your model. (Using two-way databinding with #={})
Updated to use the ObservableField, thanks for the heads up, #tynn. Correct me, if I'm still wrong.
If I understand you correctly, you can take a look at using RxJava/RxAndroid to subscribe your viewModel to changes in the model class or the firebase instance (they should be observable). So for example, as you want to let the viewModel know that the firebase has a new user and that a user entry has been updated, you can call the viewModels onNext method from that callback, which will notify the viewModel subscribed to it, and run the method you want to run (like fetching the data), then with base observable you can then notify your list.
What I'm trying to do is to parse an object into a String, and then , parse it into an XML so any other language can translate it.
Figure out this object:
public class DatosPac
{
private String nombre;
private String apellidos;
private String dni;
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getApellidos() {
return apellidos;
}
public void setApellidos(String apellidos) {
this.apellidos = apellidos;
}
public String getDni() {
return dni;
}
public void setDni(String dni) {
this.dni = dni;
}
}
What I want to do is, parse it into a common XML between Android and .Net so both languages can translate the same object. The way to communicate both languages will be using Web Services, so the Web Service will receive a String, transalte it into the object and then use the information. Bidirectionally. I mean, Android will be able to receive an object parsed from .Net, and .Net will be able to receive the same object from Android. To be able to do this, I think I need to convert them into the same XML, but I don't know how to do it in Android.
Thanks in advance.
There are several XML serializing and de-serializing libraries available for Android. And I am sure the same's the case with .NET.
You set up your objects as POJOs and with a few annotations, you can serialize/deserialize in a few lines of code. In the Android world, I personally prefer Simple, but there are various other libraries available.
A more compact, (and more efficient, in terms of parsing) data representation format is JSON. There are multiple libraries available for parsing and constructing JSON too. My preferred one for Android is Gson.
EDIT: I believe I was a bit too quick! I didn't notice the android tag and assumed a .net context. Still, one bit stands: You probably want to serialize, not to "parse" the object, into XML.
guys I'm very new to the Java word, but i share part of the knowledge because of my c# background, anyways i started developing for android and I'm running into a few snags like the following.
I usually program very OOP so i made all my objects and now i got a very common public class User with things like:
public void setId(long id) {
this.id = id;
}
public long getId() {
return id;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
and many other properties in it. Also i have a class UserHelper
where i have a populated array with all the users been pulled by the queries in the Helper
public ArrayList< User > getCurrentUsers() { return currentUsers;
}
well... the thing is, i want to be able to populate a spinner with a the value returned from getFirstName as the Display and getId obviously as the Id. I know exactly how to do this in C# but i been trying to fight with Cursors and doing some reading around but nothing, so i figured that it would be an interesting question.
ANYONE CAN SHOW ME HOW TO DO IT PLEASE?
Hi Alex look this examples
http://developer.android.com/resources/tutorials/views/hello-spinner.html
http://mobiforge.com/designing/story/understanding-user-interface-android-part-3-more-views