How to save a data retrieved from database into a string? - android

Is it possible to save a data retrieved from the database into a string?
For my case I want to save the TAG_NAME which is a data from the database into a string. Can I do it the same way as done for the Textview?
// display product data in EditText
txtwelcome.setText(product.getString(TAG_NAME));
String name = (product.getString(TAG_NAME));

Its absolutely possible,as product.getString(TAG_NAME) always returns a string either empty or with value,you can store it in a String.
And even you can store any value from DB into String using .toString() extension

try with the toString()
String name = (product.getString(TAG_NAME).toString());

Related

Realm Android run query of if field contain value in a List<String>

This is My RealmObject:
public class Channel extends RealmObject{
private String id = "";
private String name = "";
...
I am trying to construct a query where each string in a list of string contains (not equal) to the "name" field.
I am aware I can use the in() method with a ArrayList, but the in() method validate if the string EQUALS and rather need CONTAINS.
I have tried the following:
realm.where(Channel.class).rawPredicate("name CONTAINS[c] $0",list.toArray()).findAll() This perform the query only on the first String in "list"
realm.where(Channel.class).in("name", list.toArray()).findAll() is performing there search only if the field "name" EQUALS to any of the Strings in the list, where I need it to search if the field CONTAINS any of the values in the List of Strings
Any ideas of how to use a method just like in() but that will compare if the string equals or contain the field value?
Thanks
try this
RealmQuery<Channel> tasksQuery = realm.where(Channel.class).beginGroup();
for(String name: names){
tasksQuery.equalTo("name", name)
}
tasksQuery.endGroup().findAll()

Saving TimeStamp as a negative value using ServerValue.TIMESTAMP in FireBase Database

I want to save the data in firebase database with a descending order and I found that the solution is to add a timeStamp field with a negative value, but using ServerValue.TIMESTAMP save only the value in a positive way,so how can I save a negative timeStamp in my FireBase :
Code of model:
public class Book{
Object createdTimestamp;
String nom_livre;
String desc_livre;
String prix_livre;
String id_book;
String id_user;
public Book() {
super();
}
public Book(String nom_livre, String desc_livre, String prix_livre, String id_book,String id_user, Object createdTimestamp) {
super();
this.nom_livre = nom_livre;
this.desc_livre = desc_livre;
this.prix_livre = prix_livre;
this.id_book = id_book;
this.id_user=id_user;
this.createdTimestamp= createdTimestamp;
}
#Exclude
public long getCreatedTimestampLong(){
return (long)createdTimestamp;
}
//other getters and setters
}
Code of adding data in fireBase , im my case I'm creating a Book on addBookActivity:
private void createBook(String nom_livre, String desc_livre,String prix_livre,Object createdTimestamp) {
bookInfos=new Book(nom_livre,desc_livre,prix_livre,idLivre,id, ServerValue.TIMESTAMP );
myRefBook.child(idBook).setValue(bookInfos);
}
You have two options, and they both require a second write to the database after you initially write the regular timestamp number as a positive number.
If you only want to write data on the client app, what you can do is write your createdTimestamp as you are right now, then read that value back into the client by listening to the location that you just wrote. After you read it back in, you'll have the actual timestamp value. Then, you can easily compute the negative it and write it back where you want it (maybe revCreatedTimestamp, if you're using it to sort in reverse chronological order).
Your other option is to use Cloud Functions for Firebase to write a Realtime Database trigger to respond to writes that match the location /books/{book_id} where book_id is that string you're generating for the book. That trigger can then capture the timestamp and write back the negative version at the same location.

how to store custom object with list in sqlite database in android

how to create table for a custom object that contain a list of like this
public class Mobel implements Parcelable {
int thumbnail;
List<Integer> pics;
int price;
String joziat;
String code;
}
as you know there is no problem with int and string columns but how should i store my list of integer ? i was thinking to make a separate table for it but as i don't know ho many pic i have for particular object have no idea how should i join them
While indeed you can not store objects in sqlite, you can serialize it and store the serialized object:
Gson gson = new Gson();
String toStoreObject = gson.toJson(modelObject, Model.class);
And when you get the string back just do:
Model modelObject = gson.fromJson(storedObjectString, Model.class);
Or just serialize the property of the object (in your case the List).
Different things I would do:
Create another table for the images and link that table to the table in which you store your Model object (n to 1 relationship)
Don't store images in sqlite. Store them in the internal storage and in sqlite save just the path to the file.
If you really really want to store the images in sqlite, convert the image to a base64 string and store that one.

Save generic object in database

My model is like:
public class MyModel{
private String mId;
private String mName;
T mAnObject;
}
How can I store T object in database.
If you need to save specific properties, just do that, otherwise you could serialize it to JSON, as an option (as demonstrated here).
Example:
Object myObj has three properties: String title, String subtitle, String text
1 - You can save them as separate database rows and then when you read from the database you recombine them to create your Object.
2 - You can also use Json to save the properties, this way you only need to store a single String in the database and when you read from the database convert the Json String to your Object.

How can i get few characters from String?

I want to retrieve few characters from string i.e., String data on the basis of first colon (:) used in string . The String data possibilities are,
String data = "smsto:....."
String data = "MECARD:....."
String data = "geo:....."
String data = "tel:....."
String data = "MATMSG:....."
I want to make a generic String lets say,
String type = "characters up to first colon"
So i do not have to create String type for every possibility and i can call intents according to the type
It looks like you want the scheme of a uri. You can use Uri.parse(data).getScheme(). This will return smsto, MECARD, geo, tel etc...
Check out the Developers site: http://developer.android.com/reference/android/net/Uri.html#getScheme()
Note: #Alessandro's method is probably more efficient. I just got that one off the top of my head.
You can use this to get characters up to first ':':
String[] parts = data.split(":");
String beforeColon = parts[0];
// do whatever with beforeColon
But I don't see what your purpose is, which would help giving you a better solution.
You should use the method indexOf - with that you can get the index of a certain char. Then you retrieve the substring starting from that index. For example:
int index = string.indexOf(':');
String substring = string.substring(index + 1);

Categories

Resources