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();
Related
I will try to be brief:
I have three classes that have to interact in this:
The server, which receives the message (in a thread).
The contact, which stores messages in its class (in each object).
The chat activity belonging to each user, which has to show the
messages of the corresponding object, the idea is that it is a RecyclerView.(an activity)
The server receives a message, this message would be added to the chat of the specific contact and then the RecyclerView must be updated.
This is the basic Contact class code:
public class Contact implements Serializable {
private String name;
private String ip;
//Here would be a variable that contains the chat strings
public Contact (String name, String ip){
this.name = name;
this.ip = ip;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
}
And then there is the activity of the chat that is launched when selecting a specific user and it contains a RecyclerView where I want the messages to be.
My problem is that, how to make the messages that are stored in the
variable of a specific object appear in the list.
From already thank you very much.
you can use data class instead of serialization
public class Data {
private String text;
private String uid;
private String tnviews;
private String numberlikes;
public Data(String text,String tnviews,String numberlikes,String uid) {
this.text = text;
this.tnviews = tnviews;
this.numberlikes = numberlikes;
this.uid = uid;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
/////////// VIEWS
public String getViews() {
return tnviews;
}
public void setViews(String tnviews) {
this.tnviews = tnviews;
}
//////// likes
public String getLikes() {
return numberlikes;
}
public void setLikes(String numberlikes) {
this.numberlikes = numberlikes;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
you can Update RecyclerView Adapter Data
mAdapter.notifyDataSetChanged();
Reference link
I am new to Realm. Actually, Realm doesn't provide us functionality for Foreign key and I want to use the functionality of Foreign key.
So Here is my model :
public class CategoryInfo extends RealmObject {
#SerializedName("name")
String name;
#SerializedName("id")
int id;
#SerializedName("sub_category")
RealmList<SubCategoryInfo> subCategoryList;
public Data() {
}
public Data(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setSubCategory(RealmList<SubCategoryInfo> subCategoryList) {
this.subCategoryList = subCategoryList;
}
public RealmList<SubCategoryInfo> getSubCategoryList() {
return subCategoryList;
}
}
public class SubCategoryInfo extends RealmObject {
#SerializedName("sub_cat_name")
String subCatName;
#SerializedName("sub_id")
int subId;
int catId;
public String getSubCatName() {
return subCatName;
}
public void setSubCatName(String subCatName) {
this.subCatName = subCatName;
}
public int getSubId() {
return subId;
}
public void setSubId(int subId) {
this.subId = subId;
}
public void setCatId(int catId) {
this.catId = catId;
}
}
So now when I am getting a response from API side I am updating my object by below code to add catId in SubCatInfo class.
List<CategoryInfo> catInfoList = catResult.getData();
for(CategoryInfo catInfo:catInfoList){
List<SubCategoryInfo> subCatList = catInfo.getSubCategoryList();
if (subCatList != null) {
for (SubCategoryInfo subCatInfo:SubCategoryInfo) {
subCatInfo.setCatId(catInfo.getId());
}
}
}
So What I want is that when I will delete any category from the main table, subCategory should be automatically deleted. So By what way I can achieve this functionality?
Thank you in advance.
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;
}
}
I recently heard about Realm for android (also available for iOS) in this talk by Joaquim Verques . Very interesting and very powerful tool for persistent data.
I decided to give it a try after the video, researching and reading the documentation.
I found it very easy to use and set up but i end up stuck in the middle of my project because i couldn't manage to successfully make a query with many to many relationships.
I have created a small example for this topic.
My models:
public class Feed extends RealmObject {
#PrimaryKey
private int id;
private String title;
private String description;
private String link;
private Terms terms;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public Terms getTerms() {
return terms;
}
public void setTerms(Terms terms) {
this.terms = terms;
}
}
public class Terms extends RealmObject {
private String tag;
private RealmList<Category> categories;
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public RealmList<Category> getCategories() {
return categories;
}
public void setCategories(RealmList<Category> categories) {
this.categories = categories;
}
}
public class Category extends RealmObject {
#PrimaryKey
private int id;
private String 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;
}
}
So far so good, now im going to save a list of feeds to realm (make it persistent) and then try to make some query.
public static void test(final Context context,final List<Feed> feedList) {
Realm realm = null;
try {
realm = Realm.getInstance(context);
realm.beginTransaction();
realm.copyToRealmOrUpdate(feedList);
realm.commitTransaction();
RealmResults<Feed> realmResults = realm.where(Feed.class).findAll();// return me all feeds
RealmResults<Feed> realmResults1 = realm.where(Feed.class).equalTo("id", 1).findAll(); // return feed with id 1
RealmResults<Feed> realmResults2 = realm.where(Feed.class).equalTo("terms.tag", "tech").findAll(); // return feeds //with tag = "tech"
RealmResults<Feed> realmResults3 = realm.where(Feed.class).equalTo("terms.category.name", "test").findAll(); //exception here
}
} catch (Exception e) {
//Empty
Log.d("Test","",e);
} finally {
if (realm != null)
realm.close();
}
}
}
Everything run well untill the last query and i get this exception:"java.lang.IllegalArgumentException: Invalid query: category does not refer to a class."
So my question is How do i do that kind of query successfully, as i will like realm to return me every feed with terms which has at least 1 category with name = "test"
Thank you very much
Your field is called "categories" and not "category". The third query should be:
RealmResults<Feed> realmResults3 = realm.where(Feed.class).equalTo("terms.categories.name", "test").findAll();
I have an Object that I need to pass via the Intent to another Activity via the onclick method.
I was following this answer here How to send an object from one Android Activity to another using Intents?
Which works fine however my Object has within it an Array of objects.
How do I pass this object with its Array of Objects?
Below are the classes before using Parcelable
List (the object to be passed)
public class List {
private String Name;
private ArrayList<ListItem> items;
public List(){
items = new ArrayList<ListItem>();
}
public void addItem(String title, String d, String s, int p){
ListItem i = new ListItem();
i.setDecription(d);
i.setPrice(p);
i.setSite(s);
i.setTitle(title);
items.add(i);
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public int getCount() {
return items.size();
}
public ArrayList<ListItem> getList(){
return items;
}
}
ListItem
public class ListItem {
private String title;
private String decription;
private String site;
private int price;
public void setTitle(String title) {
this.title = title;
}
public void setDecription(String d){
this.decription = d;
}
public void setSite(String s){
this.site = s;
}
public void setPrice(int i){
this.price = i;
}
public String getTitle(){
return title;
}
public String getDecription(){
return decription;
}
public String getSite(){
return site;
}
public int getPrice(){
return price;
}
}
So how would I use Parcelable on List to send the ArrayList as well.
THank you and if you need any more info please ask!
You're trying to pass around data that shouldn't normally be passed around. A list is an ideal candidate for an SQLite Database. Try that or another way to persist data in android: http://developer.android.com/guide/topics/data/data-storage.html
If you insist on using Parcelable:
How can I make my custom objects Parcelable?
Also don't use List as your own type, it's standard JAVA.