I need to implement a spinner, but instead of loading its content from an array I need to use a HashTable's values so that when the user selects an entry, I get the corresponding key.
it is simple.
you need to just make the object for hash map an add it to the array list.
then after reflect that array list object where ever you are needed.
e.g.
ArrayList> list;
HashMap hm = new HashMap();
hm.put("key","value");
list.add(hm);
Related
I want to create an array programmatically in firestore. Also, I want to update the array as shown in the image attached.
I want to store all reference Numbers in this array. And whenever there is a new reference number, I want to update the array. Please help.
This is what I have tried. I know it's wrong. It's not updating the array rather replacing it.
Map<String, Object> mapone= new HashMap<>();
Map<String, Object> maptwo = new HashMap<>();
mapone.put("Refnum", f_refrenceNum);
maptwo.put("RefNumber", mapone);
upiRefnum.set(maptwo,SetOptions.merge());
Your code is telling Firestore to the RefNumber.Refnum fields with the values you specify for is.
If you want to add f_refrenceNum to the array, you can use an array-union operation:
mapone.put("Refnum", FieldValue.arrayUnion(f_refrenceNum));
This will add f_refrenceNum if it isn't already in the array. If it is already in the array and you want to add a duplicate, you will have to read the document into your application code, add the number in your application code, and then write the entire array back to the database.
See the Firestore documentation on adding items to an array.
I have a list of objects that I needed to sort by category and also alphabetically. My idea was to take my ArrayList, convert it to a HashMap so that I could use a key to organize my data by category, and then createe a treemap to naturally alphabetize the data. It works, and I am not certain of a better/more efficient way of doing this. My TreeMap has the following structure:
TreeMap<String, ArrayList<CustomModel>>
How do I access the list of values from this treemap? I understand that I can get a specific value, if it were a String for example like the following:
for (Map.Entry<String, String> entry : treemap.entrySet()) {
Log.i(TAG, "key= " + entry.getKey());
Log.i(TAG, "value= " + entry.getValue());
}
However, what if you have a list of values? How do I retrieve everything within that list? Seems like I need another conversion.
It seems like you already have this solved. Entry.value() will return the ArrayList, instead of the String type. Try enumerating this and then logging each value
I got the answer. There are a couple of ways of doing this. You can retrieve an object from your list, or the entire list object.
1) Object From List
You need a key to do this option. Get the key using keySet()
Object key = treemap.keySet().toArray()[index];
Object value = treemap.get(key);
2) Entire List Object
ArrayList<CustomModel> alCustomModel = (ArrayList<CustomModel>) treemap.values().toArray()[index];
Some notes about this, is that you are retrieving the list of CustomModels per position. Use whichever methodology makes sense to you. Cheers!
I have already seen the code to store a single point in firebase. But what I want to know is how to store whole polylines in firebase database? A decoded polyline is basiclly List. I want to know how to store many such type of lists in firebase so that I can retrieve later and perform operations on whole polyline.
Firebase use JSON format Database
Pass types that correspond to the available JSON types as follows:
String
Long
Double
Boolean
Map
List
you can pass any of data type a value in setValue() method and Lists and Map ,too.
Example:
Firebase ref = new Firebase("<my-firebase-app>/names"):
String[] names = {"John","Tim","Sam","Ben"};
List nameList = new ArrayList<String>(Arrays.asList(names));
// Now set value with new nameList
ref.setValue(nameList);
you can pass your custome object list,too.
I want to store city names by country in an array.
This is my code
String cities[][]=new String[10][20];
I want to assign all cities of a country one time like this.
cities["USA"]={"NEW YORK","WASHINGTON"}
cities["UK"]={"LONDON","CAMBRIDGE","CARLISLE"}
then I want to use like this
String mycity=cities["UK"][2];
but eclipse shows error for assigning values. how can I use this arrays?
Try like this,
String cities[][]={
{"NEW YORK","WASHINGTON"},
{"LONDON","CAMBRIDGE","CARLISLE"}
};
And
cities[0][0]
will return NEW YORK
May be this helps you.
Better you can use a HashMap - List combination like this
HashMap<String,HashMap<String,List<String>>> cities = new HashMap<>();
Refer following links for more details Storing HashMap inside HashMap,
Storing a HashMap inside another HashMap and improving performance
I created a list based on this example:
http://jsharkey.org/blog/2008/08/18/separating-lists-with-headers-in-android-09/
In GetView method, need to access information that is stored only in the HashMap.
I can access using "adapter.getItem (position). ToString ()" but this way, keys and values come together in a single string, like this:
{date=2011-07-25 19:30:00, id=1, caption=Test Caption, title=Test Title, bookmark=true}
You can retrieve this data separately? For example, only the value of "bookmark" field
Please include an example. I am new to developing for Android.
remember that you know the data type that is used with the adapter.
instead of using toString, just get the item and use it as you usually do.
for example:
HashMap<String, String> i = (HashMap<String, String>) adapter.getItem(position);
i.get(ITEM_TITLE);