I am new to parse SDK services using Android Studio and I have to ask that how to store more than one row in parse database . I am using back4app parse server whenever I want to put data on data base I.e more than one row then it does not store the first row but the second one as from the code below
ParseObject obj = new ParseObject("Table");
obj.put("Name","John");
obj.put("Score","40");
obj.put("Name","Jack");
obj.put("Score","50");
obj.saveInBackground();
So when I run app then only name and score for Jack stored in database creating one row and I want rows for both John and Jack, Kindly help me resolving this issue
You override your object fields ("name" and "score"), i.e. you delete John before save. Make a method that save your person:
private void savePerson(String name, String score){
ParseObject obj = new ParseObject("Table");
obj.put("Name",name);
obj.put("Score",score);
obj.saveInBackground();
}
and in code where you want to save person, call created method to do that:
savePerson("John", "40");
savePerson("Jack", "50);
Related
I have an API from which I receive JSON object response, which is dynamic (meaning response JSON object contains different key-values time to time), so that the database for my Android application need to delete current table and create new table based on JSON keys.
For simple example, today I get JSON object response like
`{"name":"STRING", "age":"INTEGER"},
next week I will get response like
{"name":"STRING", "id":"INTEGER","Country":"STRING"}`
I checked Room and Realm, but the problem is that I can't able not get any clear examples or tutorials to make use of it.
FYI - I am new to Android database concept.
Edit #1:
What I need is to create DB Table columns based on JSON keys (i.e, by default table X has two columns A and B and receiving JSON object contains three keys A,B and C, so that DB need to include column C dynamically)
For storing values in Android Sqlite database
check his https://androidjson.com/save-store-php-mysql-json-parsing-data-sqlite/
it will be helpful to you
I develop an android application for students attendance according to their seat numbers. If the student entered his seat number, it will be stored into real time database in fire-base.
Now, what I need to do is to retrieve the empty seat numbers to a list view according to students' information. for example: database has 50 seat numbers entered, but seat number (25) is not in the database then it will be retrieved to the list view.
In general how can I do that with fire-base real time database?
This is how database looks like for one student
You will need to mark seat empty in your db to later only fetch the empty seats. In current situation there is no way to put a query which can retrieve data only for absent students.
Your screenshot is limited so I suggest this structure.
{lectureId}/
seatNumber: {
isEmpty: boolean,
.... your other data
}
Then you will use orderByChild("isEmpty").equalTo(false);
And for querying a set of data from realtime database, have a look at the docs:
https://firebase.google.com/docs/database/web/lists-of-data
you can use a boolean if student is present (present = true).
put this variabble in te object
then you can run a loop on datasnashot for
i < noOfSeats
and show in te ListView with a condition
if (!present){
showInList();
}
I'm trying to migrate an app I had built which used Parse as its cloud backend. In my Parse backend database, I had a table which stored data as shown below:
Device ID | Contacts
xxxxx001 | "(800)-888-8888"
xxxxx002 | "(800)-888-8858"
xxxxx003 | "(800)-888-8868"
Over here, device ID is the android device ID and the Contacts are an ArrayList of strings which was generated through logic on the device. Basically, the user would select a contact (multiple in future iterations, hence it being an ArrayList, for testing I'm just keeping one item in the list) and that contact is saved for that DeviceID in the backend database. If the same DeviceID changes the contact, the contacts ArrayList in the database corresponding to its DeviceID would be replaced with the new ArrayList.
I'm trying to get something similar set up on Firebase, however right now it seems I only have a global variable on my databse which seems to get updated each time I press my button.
Here is my code for the button:
DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference mContactsRef = mRootRef.child("contacts");
#Override
protected void onStart() {
super.onStart();
mButtonContactSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mContactsRef.setValue(contacts); //contacts is an arraylist with 1 item
}
});
}
Here is how it looks in Firebase after clicking the button 3 times:
Instead of updating the value, it seems to add another row(?) of to store the current phone number selected. How can I go about setting up a DeviceID->(Objects to be stored per device)
sort of setup?
You're looking for push(), which generates a unique ID for new items.
From the Firebase documentation on reading and writing lists of data:
// Create a new post reference with an auto-generated id
var newPostRef = postListRef.push();
newPostRef.set({
// ...
});
The new items will have complex-looking keys of the form -KTTHEScy82fpfNSCoYN. Read this article on why those are preferred over array indices and (if you're interested) this article that explains the format of these keys.
Consider a different data model
In general though you might want to consider a different data model. What you're storing is a collection of phone numbers. At first sight, storing those in an array-like list seems fine.
But typically you'll want these behaviors for this contact list:
each phone number can only be present once
you need to find whether a given phone number is already in the list
With your current structure you can only see if a number is already in the list by scanning all items in the list. Whenever that is the case, it is a good moment to consider using a set data structure instead.
In Firebase you'd model a set of phone numbers like this:
"contacts": {
"(800)-888-8858": true
"(800)-888-8868": true
"(800)-888-8888": true
}
While this structure initially looks less efficient, it is actually stored more efficiently than the array list in Firebase. And looking up whether an item exists is now a simple existence check instead of having to scan the array. And with this structure it is impossible to store the same number twice.
I need opinion regarding rendering of data in android app. I have all the data stored in a json file abc.json which is in res > raw folder. i have a class that then reads data from that json file and build SQLite database when the app runs and later on i'm performing all operations like searching the data using sql queries for that database. But i am afraid if thats not a good option and the code is not optimized because code now contains so many functions for adding the items to database.
For example, json file has Authors, books, keywords, references, acknowledgements, subauthors and when the database is built, data is read and a specific function is called for each item. I'm just concerned because of too many functions as one for each item. Like whenever json is parsed for an item, e.g author, it calls addAuthors function to add that to database. Following are 2 of the functions for example.
//Sample function code for adding authors to db
public void addAuthors(Integer id, String Name, String is_corresponding) {
ContentValues value = new ContentValues();
value.put("_id", id);
value.put("NAME", Name);
value.put("IS__CORRESPONDING", is_corresponding);
authors_id = database.insert(TABLENAME_AUTHOR, null, value);
}
//example function for adding keywords to db
public void addKeyWord(String KeyWords, Integer id) {
ContentValues values = new ContentValues();
values.put("KEYWORDS", KeyWords);
values.put("_id ", id);
database.insert(TABLENAME_ABSTRACT_KEY_WORDS, null, values);
}
I need help with optimizing my code. Is there any way to optimize the current code ? Kindly help me with this and suggest some improvements for it. Thanks in advance
I would recommend bundling a sqlite database as an asset in your APK instead of bundling the JSON file and then inserting the data into a database. If your data isn't changing, you can then get rid of all your insert functions. You will also save the cost of creating and populating your database dynamically.
You can use the methods described here to create your database and to copy it from the assets of your APK. Be sure to copy it first before you try to open in in your app -- you can't open it directly as an asset.
In my project I am getting the employee id values and employee names from the database. I am displaying the names of employees in listview. When I click on the name(here the name textview is a hyperlink) in the listview all the details of that particular employee will be displayed. Based on the employee id when we click on the name that employee details should display. Please help me how to do this in android.
For ex: In general we give like this ----http://sample.com?id=1
How to do that in android?
So based on your question and follow-up comment, what you're gonna want to do is as follows:
Grab the employee name and id as you have already done.
Make an Object that will hold both the employee name and id. Make a toString() method that will return the employee name as a String. The reason for this is because when the ListView is inflated, it defaults to calling the toString() method of its objects (each list entry). Make sure to also have a way to get the employee id from the Object via either a public getter or making the id a public variable itself.
Make the ListView's ArrayList use this Object and set it accordingly.
Then you must use onItemClick to query the site you were referencing in your question. However, when you do query this site, make sure to concatenate the employee id to the end of the URL. Do what you will once you query the site.