How can i make single instance of all model classes in android? - android

what options do i have to make single instance of all the models classes in Android application?
I have added below one of the sample model class
public class User
{
private String email;
private String name;
public String getEmail() { return this.email; }
public void setEmail(String email) { this.email = email; }
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
}
I want once data stored in Model Class, it can be retrieved in any activity class or fragment. Should i use singleton or any better approach is available ?
Will dagger2 work in this scenario ? is dagger2 alternative to creating singleton ?
Thanks

Related

How can I pass ArrayList of bitmaps from one activity to other in android? [duplicate]

This question already has answers here:
Passing arraylist of objects between activities
(6 answers)
Closed 5 years ago.
In my First activity I have list of bitmaps stored in ArrayList of type Bitmap,
I need same list for my Second Activity.
How can I achieve that?
Please help!
Create Pojo class with required fields which u need to pass to next Activity as follows :
In Android Studio window use windows+Insert key to generate getter,setter and Parcelable methods.
public class PojoClass {
private String name;
private String id;
private String place;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
}
Set the values to pojo class:
Intent intent = new Intent(this,NextActivity.class);
intent.putExtra("Data", pojoclass);
startActivity(intent);
In Next Activity
ArrayList arrayList = getIntent().getParcelableExtra("Data");

Someone knows how to use card views with Realm?

I want to use card views in my app but I'm using Realm database, I really appreciate a brief explanation or an example. Thank you :)
Card View is User Interface that you use to show your content to the the user. For example, A card containing, the name and age of the user, image of the user etc. Whereas Realm is a mobile database, which stores the actual values of the user like name, age, path of the image and so on.
So, you use Card View to display the values and Realm to store the actual values.
More read up on Card View can be done here
and read up on Realm can be from here
public class User extends RealmObject {
#PrimaryKey
private String id;
private String firstName;
private String lastName;
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
In Your activity, You can store values to the Realm object like below,
User user = new User()
user.setFirstName("John")
user.setLastName("Kennedy")
user.setAge(40)
Realm realm = Realm.getInstance(this)
realm.executeTransaction {
realm.copyToRealmOrUpdate(user)
}
//Below is written in Kotlin language. You can find similar one in Java from the link given
val userJohn: User = realm.where(User::class.java)?.equalTo("firstName", "John").findFirst()
//Values of User John can be accessed like below
println(userJohn.firstName)
println(userJohn.lastName)
println(userJohn.age)
The above is Realm Example.
Below are some of the card view examples
http://code.tutsplus.com/tutorials/getting-started-with-recyclerview-and-cardview-on-android--cms-23465
http://javatechig.com/android/android-cardview-example
http://www.truiton.com/2015/03/android-cardview-example/
Because Realm makes the Objects for you, you simply use the getter and setter methods to populate the views on your cards. This would be done with the Adapter associated with your RecyclerView, or whatever List type View you are using.

Put interface in bundle to start a new activity

I need to start an activity from 2 different screens that have two different models but, both models have some shared information which is the one that I need in the new activity. The problem is that I cannot make those models to extend from the same parent, as one of the models already extends one parent. I have thought about creating an interface that contains the shared methods but, if I do that, then how can I put that interface in the bundle required to start the next activity?
I add some simplified code in order to clarify my situation:
public class A extends Model implements CustomInterface {
String name;
String address;
public String getName(){
return name;
}
public String getAddress() {
return address;
}
}
public class B implements CustomInterface {
String name;
public String getName() {
return name;
}
}
public interface CustomInterface {
String getName();
}
My problem is that I need to start an activity with a bundle with the shared information between both models. So, I would like to put CustomInterface in a bundle. How could I do that?
Thanks in advance.
So, I would like to put CustomInterface in a bundle
you could let CustomInterface extend Parcelable. E.g.
public interface CustomInterface extends Parcelable {
String getName();
}
this way the classes implementing CustomInterface will have to implements the method defined in the Parcelable interface. If implemented correctly, you will be able to pass those objects around without problems
Create a singleton class, then you can share data without passing it:
public class MySingleton
{
private static MySingleton instance;
public String customVar;
public static MySingleton getInstance()
{
if (instance == null)
{
// Create the instance
instance = new MySingleton();
}
// Return the instance
return instance;
}
private MySingleton()
{
// Constructor hidden because this is a singleton
}
public void getSomeData()
{
return something;
}
public void getSomeOtherData()
{
return somethingelse;
}
}
Then in your classes:
public class A extends Model {
String name;
String address;
public String getName(){
return name;
}
public String getAddress() {
return address;
}
public String doSomethingWithSharedData(){
MySingleton model = MySingleton.getInstance();
String somedata = model.getSomeData();
}
}
public class B {
String name;
public String getName() {
return name;
}
public String doSomethingDifferentWithSharedData(){
MySingleton model = MySingleton.getInstance();
String somedata = model.getSomeOtherData();
}
}

Realm database integration with Eclipse Android project

I need to integrate Realm database in my eclipse JUNO(Android Development Tool). I used realm 0.81.1 version. I had include all library files in my android project lib folder and configured realm-0.81.1.jar in buildpath.when "java.lang.IlleagaArgumentException:User is not part of the schema for this realmrun my project it throws error ".
Here User is an class that extends RealmObject.
The User class code is given below:
public class User extends RealmObject {
#PrimaryKey
private String name;
private int age;
#Ignore
private int sessionId;
// Standard getters & setters generated by your IDEā€¦
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public int getSessionId() { return sessionId; }
public void setSessionId(int dontPersist) { this.sessionId = sessionId; }
}
The error is produced when executing the following code:
Realm realm = Realm.getInstance(this);
realm.beginTransaction();
User user = realm.createObject(User.class);
user.setName("Jhon");
user.setAge(10);
realm.commitTransaction();
Please give me suggestion to work with Realm Database
Thanks in advance.
As you are using Eclipse you need to add the #RealmClass annotation manually as it doesn't support inherited annotations. So you model class must look like this:
#RealmClass
public class User extends RealmObject
{
// ...
}
Try this and let me know if it works. Thanks.

how to pass data from one activity to another activity with th use of Application class

import android.app.Application;
public class MyData extends Application{
private String name;
private String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
When I am using following line of code in caller Activity to send data, I get a ClassCastException:
MyData data=(MyData)getApplication();
How to declare global variables in Android?
or
https://www.google.com/#sclient=psy-ab&hl=en&source=hp&q=extending%20application%20android&pbx=1&oq=&aq=&aqi=&aql=&gs_sm=&gs_upl=&bav=on.2,or.r_gc.r_pw.r_cp.,cf.osb&fp=a1bc1589ec616832&biw=1680&bih=925&pf=p&pdl=500
Those two might help you. Personally, I don't know the answer, but the top link appears pretty helpful.

Categories

Resources