Retrieving data from a HashMap in an Adapter (GetView method) - android

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);

Related

How can i create and update an array of number in firestore?

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.

How to retrieve List object stored in TreeMap?

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!

Android assign value to two dimension string array

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

Android Hashmap interaction

I am attempting to set up a simple Morse encoder using a hashmap in android. Putting values in the hashmap seems pretty straightforward like so:
HashMap<String, String> translate = new HashMap<String, String>();
//initializing translate
translate.put("A",".-");
//same for all letters of alphabet and numbers
However I am having difficulty finding an effective way to utilize the data of the key values for export to another method. I plan to use these values in a string method and simply display it on the phone screen for the user when they type a letter. For example, if they type in "A" the hash map will be queried for "A" and return a ".-". I have never worked with hashmaps before and can't find a suitable example.
Any help on how to access these keys within an android environment will be appreciated!
Use HashMap.get(), so:
translate.get("A"); // returns ".-"
The object returned is exactly the same as the object supplied in the 2nd argument to put(). So if you put a URL in (and the Map is suitably typed) you will get the same URL instance returned from get().
HashMap has keySet and entrySet(). You may start from here. Here is javadoc for complete list of methods in HashMap. Here is an example on how to use those methods.

Spinner and HashTable in android

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);

Categories

Resources