How to use Dictionaries in Android like NSMutableDictionary in ios? - java

I am very new to Android development. I am converting my iPhone app to Android. In my iphone application, I used NSMutableDictionary to store StudentName(Key) and StudentId(Value) from NSXmlParser. Because, each student have different id, each id have different details for the student. If the user search the StudentName using UISearchBar, I want to show the StudentName and also to retrieve the correct Id for that student. I want to do the same in Android app. I searched my level best in Google for this but, I can't find the exact solution for my problem. Can anyone please help to find out the solution?
Thanks in advance.

It sounds like you probably want a HashMap... that's certainly a mutable key/value dictionary. It's not clear what you're doing with NSMutableDictionary, but if it's simple key/value lookup, then some implementation of Map (whether it's HashMap or something else) is appropriate.
You might want to read the Java Collections tutorial, too.

Related

location name of a position for a weather app

i want to build a weather app and i have some problems.
my big problems is places names! you know, i want that user find his location with two ways. with GPS and by searching. but my problem is that place names. how can find a database from whole places in the world?!
is it good idea that i store them in a database in my server? or there is some services that provide this functionality? if i have to create my own database how i can create a database like that. is there a database with city names and latitude/ longitude and other information? if a new location created how i can add it to the system? by hand?
the second problem is that after catching the city name how i can find that locations latitude/ longitude? it seems google has a Geocoder service but i don't know how it works. please help me. i just want to select a city name and find related coordinates...
The Google Maps API provides you with Geocoding, which allows you to turn strings like 'Santiago, Chile' or 'New York', into proper latitude/longitude coordinates. So in your code, you should perform an HTTP GET request to a URL like this one:
http://maps.googleapis.com/maps/api/geocode/json?address=Santiago,+Chile&sensor=true
And it returns a JSON object with a properly formatted address along with latitude and longitude information of the place you were looking up.
It's explained very thoroughly in the Google Maps API Geocoder documentation, so you should probably take a look at that. I'm no expert in Android development, but there should be some library that allows you to easily access what the Maps API has to offer in a clean way.
Maps API also provides you with a solution for Place Searching, and even input autocompletion, but all the examples I see are on JavaScript/HTML, so I'm not completely sure if there's an alternative in plain Java/Android to what you're trying to do. Nevertheless, you should take a look at basic place searching and place search autocompletion so you get a general sense of how it works.
There's an entire section in the Google Developers website dedicated to the Maps API on Android, so make sure to take a look at that aswell and you might find more useful information - sadly I have no experience with Android whatsoever so I can't really point you in any direction.
Good luck!
You could use the Google Maps Geocoding API. Querying coordinates returns a JSON response containing the current location in multiple levels of detail that you could parse.
E.g. for the position lat:40.714224 long: -73.961452, the query URL would look like the following:
http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false
The result now contains the city and country, beside much more.
You can find more information on the API here: https://developers.google.com/maps/documentation/geocoding/
You could also try http://openweathermap.org/
I haven't used this extensively but I tried it enough that I know it works. It lets you query by location name or geographic coordinates, and the city name is included in the responses along with plenty of other data.
http://api.openweathermap.org/data/2.5/weather?lat=xx&lon=xx returns a weather object with city name & related data. API key is even optional so you can try it out very quickly.

sharing savegames between a demo and a full version in Android

I am currently developing an Android Game and my goal is to create a free/demo version of the game, so that the users can try it out. But I also want that the savegames from the demo are automatically imported in the full version.
The savegames I store in the applications private storage and they are basically JSON strings mapping several Java Objects. The user can create as many "new games" as he wants and there is an "auto save" and a "manual save" file for each game he started. To keep track of all the files, I have a list containing the filenames and some additional information (like the players name etc).
So basically there are quite a lot of small files handling the savegames. This may not be the most elegant approach, but it works quite well.
So here is my question: Lets say the user has started a game in the demo version (so there will be 3 files saved in the private storage of the demo version). How can I now access these files from within the full version?
The two versions won't be much different. They are actually the same, despite the limitations of the demo version. But I would be using the same code base.
I know there has been quite some questions about this issue in this forum and elsewhere, but I was not able to find a suitable solution. All I could find involved either:
storing the files in a world readable storage (like the SD-card) or
using the SharedPreferences
But I neither want the user to be able to read the savegames (or even alter it – because this could mess up the game) – so no sd-card, nor can I use the SharedPreferences, because each single savegame has approx. 200 lines of code (many many java objects translated into JSON) and mapping all those values and objects into some kind of key-value structure used for the SharedPreferences seems quite impossible to me.
Is this all messed up, or does anyone have an idea?
Thank you for taking the time, looking forward to hear your ideas!
Christoph
So I see just 2 Solutions:
The first is a WorldReadable SharedPreferences. You said, that you store JsonStrings, so there is no need to map them any further down, if you can make Objects out of your json-strings (I like to use Gson for this kind of work), you can simply store these
Strings inside SharedPreferences.
The second Way is to bother with ContentProviders and implement a ContentResolver interface. This is the safest way I can imagine for your use-case, but you have to implement a lot for it
What you can't avoid
There are two things that you can't avoid:
If the user decides to root the phone, you can't prevent a user from accessing it, doesn't matter what you do to make it harder.
If you want a second app to access the same data (the saved games) in a non rooted device, there would always be a away for user access it from outside your apps.
What can you do to make it harder
You can encrypt (i.e. using device IMEI) the data before store it in a file or shared preferences (together with a hash to prevent changes)
You can store the data in a SQLite database (would require more knowledge to change it), and encrypt before store it (even harder).
You can use SQLCipher to store it in a ciphered database (encryption will be transparent).
Regards.
You can use a shared Content Provider (here the general documentation about ContentProviders http://developer.android.com/guide/topics/providers/content-providers.html)
you then have to declare it as exportable using the flag: android:exported="true" in the manifest
example:
<provider android:name="[yourpackage_here].SavegameProvider"
android:authorities="[yourpackage_here].SavegameProvider"
android:exported="true" />
you will be able to open it within your new app.

How can create a dictionary in android?

Am developing A Malayalam-> English dictionary app in android .how can i do this
stuff? can anyone please help me.which is the best method to create a dictionary?..all
answers are highly appreciative..
A Map implementation seems the obvious choice for a dictionary, where the key is the Malayalm word and the value is the English word.
you can either use hashmap or dictionary class. which use key/value pair concept.
to read more about that click the link below-
Click here
Will your database fit in the limited memory of the device? If so, then go with some sort of Map as suggested above.
Otherwise, it gets complicated. I would put your dictionary into an sqlite3 database and access the database from your app. For performance improvement, I would cache each word locally in a HashMap once it's been looked up. You can get clever and use WeakHashMap instead, which will purge entries from the cache as memory gets tight. (Caveat: does Android implement WeakHashMap?)
Finally, consider implementing a content provider backed up by the above-mentioned sqlite3 database. Then your dictionary is easily accessible by multiple clients and the database only needs to be opened once, by the content provider.

How to save an array of simple objects in my android app?

I'm currently creating my first android app and I've a simple class 'Player' I want to save a few instances (a dozen or two) of that consist of 5 ints. All I wish to use them for is for the user to be able to save and load players.
I'm currently a bit bewildered as to how to do this as googling and searching this site returns Parcelable, Serializable, JSON, Externalizable, SQL, Simple XML, shared preferences and many more techniques all of which have pros and cons and look quite complex to work given they may be slow or might not work or not be suited to the simplicity of what I wish to do.
What approach of compression and storage should I be taking?
Thanks
If you want easier solution then go with JSON or SharedPreferences. Easier to parse and easier to store.
Here is an example code I've uploaded for you. ObjectSavingTest
I think you can create a SQLite database with 12 rows and 5 columns.. for 5 integers in your class.. and rows for your player names..

loading google maps data into searchable data structure

For my CS bachelors I am doing a Senior project using android and google maps.
My vision was to do a (relatively) simple Dijkstra shortest path using google maps road data. I was going to add elevation change along with 2D distance. I am doing well playing with maps in android but I am completely stumped trying to access google maps data in any sort of searchable form.
How would I go about accessing the data for say intersections of roads (lat/long) in a particular area?
There has to be a way to pull that data in some sort of tree form. It seems like it may be possible with kml?
Any pointers would be awesome.
I want my paths to follow roads but If i cant this is going to turn into an orienteering application fairly fast.
Jeff
You might want to look into http://www.openstreetmap.org/ instead, they have the lat/long points of streets available for view. Google and others buy theirs, which keeps you from being allowed to access the raw information.
I don't believe you can access raw road data from Google Maps (such information is very valuable - if Google gave it out for free I can see car navigation companies going bust!). The best you can do is get the end result.

Categories