How to get a random object from firebase database in android? [closed] - java

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 6 years ago.
Improve this question
I am working on an Android app, where I need to get a random object from firebase from a child? How to do it in java?

This is how I would do it, assuming I had a child with n objects:
If I didn't know how the value of n, I would first do this in my listener to get the total number of objects:
long n = dataSnapshot.getChildrenCount();
Then I would generate a random integer, i, between 0 and n. If you don't know how to do this, Google it.
Finally, I would get the ith item from the child:
final ArrayList<MyObject> objects = new ArrayList<>();
for (DataSnapshot child : children) {
MyObject object = child.getValue(MyObject.class);
objects.add(object);
}
MyObject objectToUse = objects.get(i);
"MyObject" should obviously be whatever class you're using.
Am I missing something? Is there a better way to do this? I'm pretty new to Android and very new to Firebase so take what I have to say with a big grain of salt haha.

Related

Is there a way to do many similar tasks in Java? [closed]

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 5 days ago.
Improve this question
Say for instance I want to repeat the line of code
Integer int1 = new Integer(value1);
for many variables, such as int1 to int100. I am not asking about this exact task in particular - I am asking about any such situation where the code would be identical save for replacing small details like int1 and value1 with int2, value2. Is there a way to have the JVM complete this for me?
I am not even sure what approach to take on this or what term to search for more information. The only thing I can think to try is instead of typing "int1", having a loop that changes a string containing the name and attempting to pass the string as a symbol to the JVM but this of course does not work.
It was a little strange question and I don't know if I understood your meaning correctly or not.
But in this particular case, instead of repeating the code, you can use a data structure like an array. See Oracle tutorial.
int[] numvar = new int[arr.length];
for (int i = 0; i < args.length; i++) {
int someNumber = Integer.parseInt(args[i]);
numvar[i] = someNumber;
}

Can I store Int value inside Firebase database [closed]

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 2 years ago.
Improve this question
I have store resource values in variables, and now I want to store this variable inside the Firebase database but the data type is Integer (int) and I don't know can we store an integer in Firebase or not.
I want to store below two variables of Integer type inside Firebase Database:
int textColor = ResourcesCompat.getColor(getResources(),R.color.black, null);
int textSize = (int) (getResources().getDimension(R.dimen.text10sp));
Can I store Int value inside Firebase database?
Sure you can, in a property of type Long, which is a supported data-type. An int value will always fit in a Long.
You can directly cast your int as Integer
Integer.valueOf(textColor)
Integer.valueOf(textSize)

How to randomly select one from two integers? [closed]

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 6 years ago.
Improve this question
So lets say I want to randomly choose from the set of two numbers 1 and 3.
How do I go about doing that? Do I just assign int a to 1, int b to 3, and then do randomly select from a and b?
If there are only two numbers to choose from then you can use the value of a boolean, because it returns either true or false.
One-liner solution assuming that int a = 1 and int b = 3:
int randomOfTwoInts = new Random().nextBoolean() ? a : b;
If you have a specific list of numbers then put them in list structure (an array works well). Then you have the easier task of looking for a random index in the range from 0 to last array index. This post lists strategies for doing that:How do I generate random integers within a specific range in Java?

How to judge isInstance(ArrayList<>) in java [closed]

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 7 years ago.
Improve this question
How to judge whether a variable is an ArrayList in Java? Like the isInstance function.
Just use instanceof, blow is very simple example how to detect a variable is ArrayList:
List<String> strings = new ArrayList<>();
if (strings instanceof ArrayList) {
System.out.println("ArrayList");
}
a.getClass().getName() - will give you the type of the value of the variable, but not the variable's type.
boolean b = a instanceof String - will give you whether or not it is an instance of a specificclass.
I took this information from: How know a variable type in java?
This can happen. I'm trying to parse a String into an int and I'd like to know if my Integer.parseInt(s.substring(a, b) is kicking out an int or garbage before I try to sum it up.

ArrayList does not give the get option to retrieve the data [closed]

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 create customer and computer arraylist from the existing cutomer and computer classes as belows
ArrayList<Customer> customer = new ArrayList<Customer>();
ArrayList<Computer> computer = new ArrayList<Computer>();
But I don't available to get option to retrieve the data. Can anyone solve this problem?
customer.get(i) i is the index.
computer.get(i)
You have get like this.
USE THIS METHOD AS SPECIFIED HERE. Read up on this stuff please!
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
get(int index)
Returns the element at the specified position in this list.
For Customer :
Customer ithCustomer = customer.get(i);(retrieve ith item in the customer list)

Categories

Resources