This is I tried
public class AppInfo extends RealmObject {
public String packageName;
public RealmList<CategoryInfo> categoryList;
}
public class CategoryInfo extends RealmObject {
public String categoryName;
}
This is my DB Table.
I wanna delete categoryName.
AppInfo result = realm.where(AppInfo.class).equalTo("packageName",info.getPackageName()).findFirst();
result.categoryList.remove(category);
but the code can't delete categoryName.
Instead of
result.categoryList.remove(category);
You should do
result.categoryList.where().equalTo("categoryName", category).findAll().deleteAllFromRealm();
With Realm 3.4.0, you can also do
public class AppInfo extends RealmObject {
public String packageName;
public RealmList<CategoryInfo> categoryList;
}
public class CategoryInfo extends RealmObject {
public String categoryName;
#LinkingObjects("categoryList")
public final RealmResults<AppInfo> appInfos = null;
}
and
realm.where(CategoryInfo.class)
.equalTo("appInfos.packageName", packageName)
.equalTo("categoryName", category)
.findAll()
.deleteFromRealm();
Related
I have the following class that I want to store in firestore.
public class User extends Model {
#PropertyName("user_auth_id")
public String authUid;
#PropertyName("first_name")
public String firstName;
#PropertyName("last_name")
public String lastName;
#PropertyName("picture_url")
public String pictureUrl;
#PropertyName("email")
public String email;
#PropertyName("companies")
public ArrayList<UserCompany> companies;
public User() {}
}
public class UserCompany extends Model {
public String name;
public String role;
public String position;
public UserCompany() {
super();
}
public UserCompany(Company company, String role, String position) {
this();
name = company.name;
this.role = role;
this.position = position;
}
public Map<String, Object> toObject() {
Map<String, Object> object = new HashMap<>();
object.put("id", id);
object.put("name", name);
object.put("role", role);
object.put("position", position);
return object;
}
}
#IgnoreExtraProperties
public class Model {
#Exclude
public String id;
public <T extends Model> T withId(#NonNull final String id) {
this.id = id;
return (T) this;
}
}
And I want to use a transaction to update an user entry with it's newly created company list. (appUser instanceOf User)
transaction.update(userRef, "companies", appUser.companies);
If I do so...I get
Invalid data. Unsupported type: ro.exemple.model.UserCompany
How can I serialise an User object so that I can deserialise it as such
User appUser = queryDocumentSnapshots.getDocuments().get(0).toObject(User.class);
Where queryDocumentSnapshots is the result of a query in my firestore db.
I know I can change from ArrayList to HashMap, but I wish to keep the List, and try to serialise and deserialise it, in order to obtain in firestore an array of objects.
if this serialization you must tag your class as Serializable..
public UserCompany extends Model implements Serializable
public class User extends Model implements Serializable
plus tag the class Model as well
I've already tried to use
String mystring = getResources().getString(R.string.mystring);
And I found the same theme in this article How can I convert the android resources int to a string. eg.: android.R.string.cancel?. But it doesn't work in my situation.
This is my code:
public class Film{
private int image;
private String name;
private String schedule;
private String description;
public Film() {
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
...
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
public class FilmList {
public static ArrayList<Film> getFilms(){
ArrayList<Film>films=new ArrayList<>();
Film film=null;
film =new Film();
film.setName(getResources().getString(R.string.name1));
film.setImage(R.drawable.img1);
film.setDescription(getResources().getString(R.string.desc1));
films.add(film);
film =new Film();
film.setName(getResources().getString(R.string.name2));
film.setImage(R.drawable.img2);
film.setDescription(getResources().getString(R.string.desc2));
films.add(film);
return films
}
}
If you don't have access to a Context (e.g. via an Activity, Service, ContentProvider, or BroadcastReceiver), you cannot get a string resource.
If you have a class that is not one of the above and it needs to get a string resource, you must either have pass a Context to that class so it can retrieve the resource, or pass that class an already-resolved resource.
Make an application class and add a hold its reference in a static varialble. then you can access context anywhere in the app
public class MyApp extends Application {
private static AppLWP instance;
#Override
public void onCreate() {
super.onCreate();
instance = this;
}
}
Then you can access this instance for context anywhere in the app
public static ArrayList<Film> getFilms(){
ArrayList<Film>films=new ArrayList<>();
Film film=null;
film =new Film();
film.setName(MyApp.instance.getResources()
.getString(R.string.name1));
film.setImage(R.drawable.img1);
film.setDescription(MyApp.instance.getResources()
.getString(R.string.desc1));
films.add(film);
film =new Film();
film.setName(MyApp.instance.getResources()
.getString(R.string.name2));
film.setImage(R.drawable.img2);
film.setDescription(MyApp.instance.getResources()
.getString(R.string.desc2));
films.add(film);
return films
}
}
First, provide context to the class Film via the activity you are calling the object of Film.
If you call from Main Activity you will have to do something like this:
public class MainActivty extends AppCompatActivity {
private static MainActivty obj;
onCreate() {
//usual functions
obj = MainActivity.this;
}
}
Once you have the context, simply try this:
String mystring = context.getString(R.string.mystring);
Or, if you floowed my MainActivty method:
String mystring = MainActivty.obj.getResources().getString(R.string.mystring);
Here's the Object I'm working with:
public class NavigationMenuModule extends RealmObject implements Parcelable {
#PrimaryKey
public String sectionKey;
public RealmList<ItemModule> modules;
public RealmList<Article> spotlightSponsored;
public RealmList<Article> items;
}
The child Article Object:
public class Article extends RealmObject {
#PrimaryKey
public String contentId;
public String leadImageURL;
public String summary;
public String headline;
}
How would I structure this realm call:
get NavigationMenuModule item by sectionKey
get spotlightSponsored within that NavigationMenuModule that matches the article's contentId
The method below works but I feel like there's probably a "neater" way:
public static Article getArticle(String sectionKey, String articleId) {
Realm realm = Realm.getDefaultInstance();
NavigationMenuModule navigationMenuModule = realm.where(NavigationMenuModule.class).equalTo("sectionKey", sectionKey).findFirst();
if (navigationMenuModule != null && !navigationMenuModule.spotlightSponsored.isEmpty()) {
for (Article article : navigationMenuModule.spotlightSponsored) {
if (article.getContentId().equals(articleId)) {
Article ret = realm.copyFromRealm(article);
realm.close();
return ret;
}
}
}
realm.close();
return null;
}
Theoretically this should work with Realm 3.5.0
public class NavigationMenuModule extends RealmObject implements Parcelable {
#PrimaryKey
public String sectionKey;
public RealmList<ItemModule> modules;
public RealmList<Article> spotlightSponsored;
public RealmList<Article> items;
}
public class Article extends RealmObject {
#PrimaryKey
public String contentId;
public String leadImageURL;
public String summary;
public String headline;
#LinkingObjects("spotlightSponsored")
public final RealmResults<NavigationMenuModule> spotlightSponsoredOf = null;
#LinkingObjects("items")
public final RealmResults<NavigationMenuModule> itemsOf = null;
}
public static Article getArticle(Realm realm, String sectionKey, String articleId) {
return realm.where(Article.class)
.equalTo("contentId", articleId)
.equalTo("spotlightSponsoredOf.sectionKey", sectionKey)
.findFirst();
}
Can I define a field with unknown type in Realm model?
Sample classes :
public class Model1 extends RealmObject {
#PrimaryKey
private String _id;
private ? field1;
}
public class Model2 extends RealmObject {
#PrimaryKey
private String _id;
}
public class Model3 extends RealmObject {
#PrimaryKey
private String _id;
}
Now, the field1 in Model1 can be of type Model2 or Model3 which will be determined during run time. Is there any way I can achieve this?
No, you cannot do that. Dalinaum's comment is correct.
One way to achieve it is like;
public class Model1 extends RealmObject {
#PrimaryKey
private String _id;
private Model2 model2;
private Model3 model3;
}
public class Model2 extends RealmObject {
#PrimaryKey
private String _id;
}
public class Model3 extends RealmObject {
#PrimaryKey
private String _id;
}
and access it via;
if (model1.getModel2() == null) {
Model2 model = model1.getModel2()
// do something
} else {
Model3 model = model1.getModel3()
// do something
}
How can I implement this functionality?
ApplicationConstants.phoneContacts.add(
new ContactNumberBean(nameOfContact,
contactNumber, contactNumberType));
ApplicationConstants and ContactNumberBean classes
ContactNumberBean :
package com.example.AddressBook;
public class ContactNumberBean
{
private String nameOfContact;
private String contactNumber;
private int contactNumberType;
public String getnameOfContact()
{
return nameOfContact;
}
public String getcontactNumber()
{
return contactNumber;
}
public int getcontactNumberType()
{
return contactNumberType;
}
public ContactNumberBean(String nameOfContact, String contactNumber,int contactNumberType)
{
this.nameOfContact=nameOfContact;
this.contactNumber=contactNumber;
this.contactNumberType=contactNumberType;
}
}
ApplicationConstants :
package com.example.AddressBook;
import java.util.ArrayList;
public class ApplicationConstants
{
//String[] phoneContacts =new String[10];
//ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();
/*String s1,s2;
int i1;
ContactNumberBean cb =new ContactNumberBean(str1,str2,i2);
static ArrayList<String> phoneContacts = new ArrayList<String>();
phoneContacts.add(s1);
phoneContacts.add(s2);
phoneContacts.add(i1);*/
how can implemented in this class functionality ................................
**ApplicationConstants.phoneContacts.add(new ContactNumberBean(nameOfContact,
contactNumber, contactNumberType));**
}
hi to access been values in another class create a common class named as Constants.java in that declare and initialize been object like below:
public class Constants{
public static Bean userBeen=new Bean();
}
been class:
public class Been {
private string countryName;
public void setCountry(String s) {
this.countryName=s;
}
public String getCountry() {
return countryName;
}
}
set values:
public class A{
String s="India";
Constants.userBeen.setCountry(s);
}
}
get values:
public class B{
String s=Constants.userBeen.getCountry();
}
}
this will work fine.