Hot to convert filled object into an Array of Strings - android

Currently I have this object:
CurrencyType(Stuff1=null, Stuff2=0.61952, Stuff3=1.2117, Stuff4=2.9687, Stuff51=0.95025, Stuf6=0.69852, Stuff7=4.9222, Stuff8=15.931, Stuff9=4.6196, Stuff10=0.55648, Stuff11=5.6577, Stuff12=4.6056, Stuff13=202.27, Stuff14=10732.0, Stuff15=2.5838, Stuff16=51.865, Stuff17=79.175, Stuff18=80.258, Stuff19=808.33, Stuff20=13.856)
Since I want to display this results in a Recycler View and the JSON does not give me an Array I need to split these values into an Array. Can someone show me how to do it?

maybe you can use reflection like in these answers:
Object Attributes in Java as Array or List Elements
Getting a list of all fields in an object
As they say in the answers, if you could avoid using reflection and try to have your array directly, don't hesitate (reflection raises performance issues).

Convert Object to String(.toString())
String datam="CurrencyType(Stuff1=null, Stuff2=0.61952, Stuff3=1.2117, Stuff4=2.9687, Stuff51=0.95025, Stuf6=0.69852, Stuff7=4.9222, Stuff8=15.931, Stuff9=4.6196, Stuff10=0.55648, Stuff11=5.6577, Stuff12=4.6056, Stuff13=202.27, Stuff14=10732.0, Stuff15=2.5838, Stuff16=51.865, Stuff17=79.175, Stuff18=80.258, Stuff19=808.33, Stuff20=13.856)";
String[] arr_one=datam.replace("CurrencyType(","").replace(")","").split(",");
List<String> stringList = new ArrayList<String>(Arrays.asList(arr_one));
This is not perfect answer but it may help you

Related

How to append an array into an array field in firestore?

Question
I have a collection named Users with a field named friendEmails which is an array that contains Strings.
I have a document with friendEmails = {joe#gmail.com, dan#gmail.com}, and I want to append newEmails = {mat#gmail.com, sharon#gmail.com} to it.
Problem
The only options I know for this are:
Reading the friendEmails array first, then adding the union of it and newEmails to the document.
Iterating over the elements of newEmails (let's and each iteration doing:
myCurrentDocumentReference.update(FieldValue.arrayUnion, singleStringElement);
(Since FieldValue.arrayUnion only lets me pass comma-separated elements, and all I have is an array of elements).
These two options aren't good since the first requires an unnecessary read operation (unnecessary since FireStore seems to "know" how to append items to arrays), and the second requires many write operations.
The solution I'm looking for
I'd expect Firestore to give me the option to append an entire array, and not just single elements.
Something like this:
ArrayList<String> newEmails = Arrays.asList("mat#gmail.com", "sharon#gmail.com");
void appendNewArray() {
FirebaseFirestore firestore = FirebaseFirestore.getInstance();
firestore.collection("Users").document("userID").update("friendEmails", FieldValue.arrayUnion(newEmails));
}
Am I missing something? Wouldn't this be a sensible operation to expect?
How else could I go about performing this action, without all the unnecessary read/write operations?
Thanks!
You can add multiple items to an array field like this using FieldValue.arrayUnion() as the value of a field update:
docRef.update("friendEmails", FieldValue.arrayUnion(email1, email2));
If you need to convert an ArrayList to an array for use with varargs arguments:
docRef.update("friendEmails", FieldValue.arrayUnion(
newEmails.toArray(new String[newEmails.size()])
));

How to read array data from the firestore using kotlin?

I am new in android development and when I read array data from firestore using following code
val variable = arrayOf(document.get("restaurant"))
and then loop over the variable using code
varibale.forEach {
Log.d("someTag", ${it.toString()} + " is your data")
}
I get the result with square brackets at log as following
[somedata, somedata2] is your data
my problem is that forEach loop runs only once and I am not able to get the result (without square brackets) as following
somedata is your data
somedata2 is your data
I have 2 elements in my restaurant array in firestore
I will be very thankfull to any one who will help me.
You are actually wrapping an array/list into another array when using arrayOf, that's why you see those brackets. Instead, try casting your document.get("restaurant") and then looping directly through it.
arrayOf doesn't parse an array. It creates a new array using the elements you pass to it. That's not what you want. You should instead cast document.get("restaurant") to the type that you expect to get from Firestore.
If a field is an array of strings, then the SDK will give you a List<*>, and you will need to make sure each item in the list is a String, if that's what you stored in the array.
val variable = document.get("restaurant") as List<*>
// Iterate variable here, make sure to check or convert items to strings

What is the benefit of turning a String array into an ArrayList before populating a AutoCompleteTextView with it?

On the material page I found the following example for AutoCompleteTextView:
int layoutItemId = android.R.layout.simple_dropdown_item_1line;
String[] dogArr = getResources().getStringArray(R.array.dogs_list);
List<String> dogList = Arrays.asList(dogsArr);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, layoutItemId, dogList);
AutoCompleteTextView autocompleteView =
(AutoCompleteTextView) findViewById(R.id.autocompleteView);
autocompleteView.setAdapter(adapter);
Source: https://materialdoc.com/components/autocomplete/
What is the point of this part:
List<String> dogList = Arrays.asList(dogsArr);
Why turning it into an ArrayList when the AutoCompleteTextView also takes a String array?
When you know only going to work with a fixed number of elements, you should Array. If not, use Lists.
My personal opinion is use list. Lists makes code very inflexible and easy to use.
You can initialize Java arrays at compile time, like:
String data[] = { "a", "b", "c" };
In old versions of Java there was also the case for type safety. ArrayList elements had to be casted to the original type whereas Java arrays where type safe.
Java arrays are part of the language and you will not be able to change them. ArrayList is part of the Java API. If you need (I do not recommend it though) you could substitute your own library to implement the ArrayList job
Check This Question For More Information
See This question
If you have an array, it has to have a fixed size. Dynamically adding and removing the elements are difficult to manage and you have to have new array created every time you add a new item. Similarly for removing item.
With ArrayList it is easy to manage as it doesn't get created with a static size. Thus at runtime you can easily add and remove elements.
ArrayList is the ideal datastructure to use here.

output text line by line

I write app for Android such gets data from server in JSON format. Now I get this value in string, but in my application it must look like:
Route:
1)first point
2)secon point
3).....
n) n point
I read that in Android in textView I can do it if string will be with html tags but I think it is not the best variant. After Android I must do it in iPhone now I don't know how to do that there. Send Routes as Array is not good variant too. Can you say what is the best way to decide this problem?
Have a look here you will have to find the good pattern .
Hence you have separated strings just use a list View with an ArrayAdapter.
I am not so good with regex but i think it should like : [1-9][0-9]) [[a-f][0-9]]+
I couldn't comment b/c of rep, sorry. Could you provide an example of returned JSON string. I think JSON format can be parsed with ease.
If this the case you can parse it in a loop (or another way. I'm not that good at it)
String[] parseIt (String JSON){
String[] list=JSON.split("\\d\\)");
String[] rlist=new String[list.length-1];
for(int i=0;i<list.length-1;i++){
rlist[i]=list[i+1].trim();
}
return rlist;
}
This might do trick. But you should edit result. I didn't test yet
Edit: I edited code. It simply return the address now with leading whitespace. You can get rid off them using. String trim() method like;
list[1].trim();
Do it in loop and don't care about first element (index 0).
Edit 2: Now it should work

android listview issue with more than 3 dynamic data

I have a doubt. I have 3 array list dynamic values. I need to display these dynamic values in a listview. can someone please tell me how can i achieve this.
I have name[] array, status[] array and image[] array. I need to dynamically display the values in listview in a android sample
This is what i have:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,NameList);
In this i am able to display i am able to display all the names in NameList[].
An array list only accepts an array of values, so as you see, you can only pass in the names. You have two options,
The simple option is to create a compound object, say Person, that has a name, status, and image. Then you create a Person[], and create the array list on that, and pass it into the array adapter. You must implement Person.toString() to print out the Person object as you'd like.
If you need to be able to lay out the Person fields in a more flexible way then you could simply by implementing toString() on Person, you need to create a custom list adapter. You can take a look at this post to get you started,
Categorise the listview

Categories

Resources