Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Is there any way to insert multiple data in firebase using loop in android studio.
As
for (DataSnapshot ds : dataSnapshot.getChildren())
{
String Name = ds.child("Name").getValue().toString();
list.add(Name);
}
DataSnapshot loop is used to get the multiple children. How can i insert multiple data using loop.
I am guessing you want to update various children of your database at once and for that you do not need to actually use a loop to insert multiple data at once in Firebase.
You may use maps for that, which can update multiple fields of your database in one go. The code for using maps, looks something like this:
Map<String,Object> taskMap = new HashMap<>();
taskMap.put("age", "12");
taskMap.put("gender", "male");
taskMap.put("name", "Someone");
taskMap.put("surname", "no-one");
reference.updateChildren(taskMap);
This depends a lot on your database structure, and you can edit it for your need, according to your database structure.
To know more about, how to update database using maps, go through the following links:
Link1
and
Link2.
Also, if you could make the question a bit more precise, you can get more help from here.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have successfully read all my user data into cloud firestore but unable to see all my data in one go.
As you can see in the screen capture, "Sara" has been read. Beforehand, "Mira" and "Kiena" has also been read.
How do I see "Mira" and "Kiena" together with "Sara" in one screen? I want to see all the data that has been read in Cloud Firestore.
I need to have all this as a record for my thesis.
I set the user data with this code:
private DocumentReference mDocRef = FirebaseFirestore.getInstance().document("sampleData/userdata");
You need to add this
SetOptions.merge()
inside the set function in your code
what is happening now is that firestore takes a key = name and values = sara/mira/etc and keeps overriding the values since the key is the same, but when you use the merge options it will not override and will display all in a list
It looks like you're setting the user data with something like:
db.collection("sampleData").document("userData").set(...)
Any time you execute this code, you're overwriting whatever data already exists in userData.
If you want to create a new document each time you run the code, do:
db.collection("sampleData").add(...)
This will generate a new document with a unique ID each time you execute it.
If the document needs to be associated with the current user from Firebase Authentication, you'll want to use the user's UID as the document name. That'd look like this:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
db.collection("sampleData").document(uid).set(...)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am creating a Classified application, I am stuck at Post Activity, where the user needs to select the Category to post their products. Below is the rough sketch for Post Activity.
Firebase Database Structure :
How to let the user select the Category from the Category list and based on that Category the item/products needs to be saved under that Structure.
For example : If the user needs to post an Advertisement for Audi, he will select "Cars" from the spinner list and that info(image,price,name) should be saved under "Cars" structure in the Firebase Database. Any help would be appreciated, I need to know the logic behind this:
You can get spinner text like this.
Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();
After you got selected text you can set DatabaseReference location.
databaseReference = FirebaseDatabase.getInstance().getReference().child("All Categories").child(text);
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Both of the codes below give me the same exact answers. I was just wondering which would be better programming practice for readability and maintainability. Would less lines of code be best? Would one affect the functionality of the program more than the other? any suggestions would be very much appreciated as I just want to learn the best practices for programming.
Here are the codes:
for (int i = 0; i < db.getAllDecks().size(); i++)
{
String CardCount = String.format("(%s)",db.getCardsForDeck(i+1).size());
adapter2.add(db.getAllDecks().get(i).getDeck_name());
adapter3.add(CardCount);
}
or
for (Deck deck: deckList) {
String deckName = deck.getDeck_name();
adapter2.add(deckName);
int count = db.getCardIds(deck).length;
String strCount = Integer.toString(count);
adapter3.add(strCount);
}
Overall, I think the second code is clearer, and more readable.
It contains moe variable names that is able to tell what exactly it is used for, such as deckName, count and strCount. I can clearly see that you are getting every deck's name and card count and put them in different (list?) adapters.
For the first one, I apparently needed more time to comprehend what it is doing. So IMO, the second one!
Also if you could just rename getDeck_name to getDeckName that would be better for people to read. getDeckName follows the naming convention for naming Java methods i.e. camelCase.
if you want to get data from simple list thnn foreach loop is good to use but,,, if you want to data from exact position or to store from id than for-loop is better ..
and there is NO difference by performance wise both are same as well, as i know.
as my suggestion use for loop :)
As per this book Code Complete - Steve McConnell's
for loop is good choice when you need a loop that executes a specified number of times.
foreach loop is useful for performing an operation on each member of an array or the container.
for more visit : Google books - Code Complete
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to get data from http://schedule.sumdu.edu.ua/index/json?method=getTeachers, parse it and load into AutoCompleteTextview. Any suggestions?
You need to access the service URL using an AsyncTask<>, then later on get its response into a String object.
And, parse it, using JSONObject/Json Array present in android. You will get many examples for this.
Later on you can create a String array, load your data in it, and set it for auto complete for text view.
Here is an example for this.
String[] listTeachers; // initialize this with teachers JSON data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.layout_teacher_list, listTeachers);
tvTeacher.setAdapter(adapter);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am a beginner in Android, and understand only very basically that HashMap class enables key/value pairs. But how does this translate into actually using this in an Android app? Could someone provide a simple, plain English example of what case you might want to use HashMap in an app? I cannot imagine a case where I might need it. Make up an Android app idea, if needed. Thanks in advance.
I am looking for a "big picture" analysis that will give some examples where you might use HashMap with certain Android functionalities you are trying to implement.
HashMap or Map interface is not new on android, This is Java Collections framework.
Java collection are meant to be used in several cases to hold data and contain 3 interfaces:
List - Basically simple list,or linked list implementations
Set - The same as list but won't hold 2 equal obejcts(You need to implement you own equals and hashcode)
Map - as you said key value pair.
Uses:
List - For anything, just to hold data
Set - For list of data that we want that all of the items will be unique.
Map - Key value and the most common example is the use for DB items, or something with ids.. for example:
bookId, Book.. I that case you can take the object by id.. This is the most common
I attached link for Java collection tutorial.. It is very important framework that you have to know if you are going to develop java/android
http://tutorials.jenkov.com/java-collections/index.html
Hope that helps
We could use HashMap to keep a list of employess together with their respective salaries.
We can do:
HashMap<String, Float> emplMap = new HashMap<String, Float>();
emplMap.put("fred", 1.000);
for(String name : emplMap.keySet()) {
System.out.print(name + "'s salary is" + emplMap.get(name));
}
Should print
"fred's salary 1.000"