Map<String ,String[]> Converts to Map<String, Object[]) - codenameone

I have a map
Map<String, String[]> newMetaData = new LinkedHashMap();
which I populate with data like this:
newMetaData.put(
((String) elm.get("companyName")).trim(),
new String[]{
this.storeFile( "logo", (String) elm.get("logoLink") ),
this.storeFile( "profile", (String) elm.get("companyProfile") ),
this.storeFile( "action", (String) elm.get("drash") ),
(String) elm.get("fwtografies")
}
);
StoreFile is a function that returns string. I save this map to the storage as
Storage.getInstance().writeObject("MetaData", newMetaData);
Later on the code I retrive the above map as:
Map<String, String[]> metaData = (Map)Storage.getInstance().readObject("MetaData");
But instead of getting a Map of <String, String[] & gt; I get Map of <String, Object[] >
any help is appreciated

Generics in Java are syntax sugar. They are erased by the compiler and ignored by Codename One for the most part. This:
Map<String, String[]> newMetaData = new LinkedHashMap<>();
Is equivalent to this as far as the generated code is concerned:
Map newMetaData = new LinkedHashMap();
There are some caveats because the first version will also add casts when you use methods like set/put which could trigger a class cast exception in some cases. The thing is that this works in reverse as well so this should work just fine:
Map<String, String[]> metaData = (Map<String, String[]>)Storage.getInstance().readObject("MetaData");

Related

How to pass multiple condition in dynamic parameter in Dapper Query like we pass for IN operator

I want to pass multiple entity code while querying my PostgreSQL db. I am getting error with below code:
I want query to work like below:
SELECT * FROM public.test WHERE entity_code IN('value1', 'value2');
Below is my code which works in case I am passing single value:
string query = "SELECT * FROM public.test WHERE entity_code = #Entity_Code";
Dictionary<string, object> dictionary = new Dictionary<string, object>();
dictionary.Add("#Entity_Code", "value1");
var connection = _postgreSqlContext.Database.GetDbConnection();
var results = connection.Query<Test>(query, new DynamicParameters(dictionary));
And below is the code which is not working in case multiple values added against same parameter:
string query = "SELECT * FROM public.test WHERE entity_code IN #Entity_Code";
Dictionary<string, object> dictionary = new Dictionary<string, object>();
dictionary.Add("#Entity_Code", new string[]{ "value1", "value2" });
var connection = _postgreSqlContext.Database.GetDbConnection();
var results = connection.Query<Test>(query, new DynamicParameters(dictionary));
Postgres has a slightly different IN-syntax. You should use ANY:
string query = "SELECT * FROM public.test WHERE entity_code = ANY(#Entity_Code)";
var dictionary = new Dictionary<string, object>();
dictionary.Add("#Entity_Code", new string[]{ "value1", "value2" });
var connection = _postgreSqlContext.Database.GetDbConnection();
var results = connection.Query<Test>(query, new DynamicParameters(dictionary));
See the answer to this question: IN vs ANY operator in PostgreSQL

How do I get contents from a Firestore Array in Unity?

I'm struggling to make sense of Firestore arrays in Unity/C#.
DocumentSnapshot snap = task.Result;
IDictionary<string, object> dict = snap.ToDictionary();
object questData = dict["questData"];
How do I say results = dict["questData"][0].results?
Debugger shows questData as object I can't figure out how to say:
object[] questData = dict["questData"]
It comes in from Firestore as an object but it's really an array.
i dont see if its array or list oe IEnumerable..
but you could use that to unbox object:
var x =dict["quesData"] as object[] or List<object>
and following the type you apply the same schema.
if collection (ict["questData"]) is dictionary you could apply that:
foreach (KeyValuePair<string, object> pair in dict["questData"])
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
to find the other keys, i dont see if key is string, so object should does the job in case...
so you could write too: var x =dict["quesData"] as Dictionary<string, object>

Rest API for codenameone java.util.ArrayList cannot be cast to java.util.HashMap

For starters the Rest API is a lot easier to use than ConnectionRequest. However on the simulator it works well but on deploying the apk i get java.util.ArrayList cannot be cast to java.util.HashMap. Why does it work on simulator and fail on APP and most important how do i fix this. Please see attached the code snippet
List<Shift> shifts = new ArrayList<>();
Map<String, Object> result = (Map<String, Object>) Rest.get(new UrlManager().getShiftsAvailable())
.acceptJson().getAsJsonMap().getResponseData();
Iterator<Map.Entry<String, Object>> itr = result.entrySet().iterator();
while (itr.hasNext()) {
Map.Entry<String, Object> entry = itr.next();
Log.p("Key: "+entry.getKey()+" Value: "+entry.getValue());
Iterator<Map.Entry<String, Object>> itr1 = (Iterator<Map.Entry<String, Object>>) entry;
LinkedHashMap<String,String> hm = (LinkedHashMap<String,String>) entry.getValue();
Shift shift = new Shift(hm.get("id"), hm.get("createddate"),
hm.get("start_time"), hm.get("end_time"),
hm.get("driver_locations"), hm.get("shift_times"), hm.get("required_drivers"), hm.get("standby_drivers"), hm.get("check_in_minutes"));
shifts.add(shift);
}

How to populate image in a dynamic list in codename one

How to populate image in a dynamic list in code name one?
protected boolean initListModelList(List cmp) {
cmp.setModel(new com.codename1.ui.list.DefaultListModel(new String[] {"Firstname", "LastName", "Email"}));
return true;
}
Thanks!.
Assuming you used hashtables and a renderer as I explained here: How to add dynamic data in the renderer created using UI builder?
The value in the hashtable or map you used for a specific row can be an image. E.g.:
ArrayList<Map<String, Object>> a = new ArrayList<Map<String, Object>>();
HashMap<String, Object> m = new HashMap<String, Object>();
m.put("firstName", name);
m.put("picture", image);
a.add(m);
... etc for every row
cmp.setModel(new com.codename1.ui.list.DefaultListModel(a));

Deserialize multiple ArrayLists in a class using gson

I have a class with multiple Lists. Is the class design correct for gson to work?
My class:
Class Data
{
List<String> actor;
List<Integer> text_relevance;
List<String> title;
}
The json string is as follows ...
Is the class design for this json correct?
{
"data":
{
"actor":["Abercrombie, Ian","Baker, Dee Bradley","Burton, Corey",
"Eckstein, Ashley","Futterman, Nika","Kane, Tom",
"Lanter, Matt","Taber, Catherine","Taylor, James Arnold",
"Wood, Matthew"],
"text_relevance":["308"],
"title":["Star Wars: The Clone Wars"]
}
}
Yes, your code is correct. I deserialized it with this code:
Gson gson = new Gson();
Type type = new TypeToken<Map<String, Data>>() {}.getType();
Map<String, Data> map = gson.fromJson(json, type);
Data data = map.get("data");
Note that Gson will convert strings like "308" to the corresponding integer. This is generally used to permit very large values from losing precision. But it works out just fine for your case as well.

Resources