How to make a generalized use from multiple array - android

I'm creating an android app which has selection of company and based on that I show cars and based on selection of particular car, I want to show some details of car e.g. capacity, power, engine oil etc. Now I'm using array to store all this car related information. e.g.
private static final String[] car1={"624CC","33bhp","10W40"}
private static final String[] car2={"1600CC","120bhp","10W40"}
To check the section, I've used multiple if-else statements for each car. And depending on the selection, I show value form array.
if(bn.equals("car 1"))
{
cap.setText(""+car1[0]);
powr.setText(""+car1[1]);
oil.setText(" "+car1[2]);
}
else if(bn.equals("car 2"))
{
cap.setText(""+car2[0]);
powr.setText(""+car2[1]);
oil.setText(" "+car2[2]);
}
else if(bn.equals("car 3"))
{
cap.setText(""+car3[0]);
powr.setText(""+car3[1]);
oil.setText(" "+car3[2]);
}
Now the problem is as the number of if-else have increased,I'm getting error of "Code too large" in android studio.
I was wondering if is there any way to replace these multiple if else statements with single, generalised statement. As you can see, in all if else, the code is the same, its just that array name is different.
I'm aware that I can use SQLite db, but I'll have to add all the values to it for all the cars again. So was wondering if I can use the same array that I've created. Any solution/suggestion will be really helpful

Probably in place of string comparison of names you can store the index of the value selected and use switch case to find out which car is selected. This should reduce the code also make it much more readable.

Related

What is the best way to save a string-array in SQLite on Android?

One string size is about 200 bytes,
and it stores 10~20 size in a daily array.
(Store 10~20 strings of 200bytes, as array type)
I have found a way to convert an array to a string
and store it in SQLite.
However, I do not know it's a good idea
because the size of the string is large.
1.
If large arrays of strings,
is it a good idea to store arrays as a string?
2.
or is there a better way?
I would like advice. Thank you.
You're actually placing your concern onto the wrong part of your database design.
For SQLite, the maximum length of a String is 1 billion bytes, so your worries about your 10-20 strings of 200 bytes each actually isn't considered that large.
There's really no harm in storing your array as a single long String in your database. Especially when it's nowhere close to the maximum limit of a String.
Your database query time won't become longer due to your String being long. The real concern here is the processing you'll be doing on that String to turn it back into an Array. Typically, if the String is extremely long, the real performance hit is when you're flattening the array into a String and when you're transforming that String back into an Array.
However, typically, this is something you'll show a loading indicator for to your users.
For storing an Array into a database, there's really only two ways to do so:
Flatten array into a single String and store the String as TEXT
Create a table meant to store the individual elements of the string, and include a column for a Foreign Key that allows you to associate those rows with the same array. Then you'll store each element of your String arrays as a row in this table.
Depending on what you need, one design is better than the other.
For example, you would normally prefer the second implementation if your app requires you to constantly edit individual elements of an array.
For such an example, it wouldn't make much sense to use the first solution, because this means every time you want to edit the contents of an array, you'll be fetching back the complete array in it's entirety. This is impractical when you only want to fetch or edit a particular portion of that String.
Therefore, in such an example, it is much more practical to store the individual elements of the arrays into individual rows of a Table meant for this type of data. You'll be querying only the row you want and updating only the row you want.
So to answer your questions, there's really only two ways to store your String array as a TEXT type in your SQLite database. Both ways work and the real concern is to consider which solution fits your needs best.
If your app only requires you to store and fetch the array in it's entirety each time, then the single String method might be more preferable.
But if your app requires you to work with individual elements of your array, then using the table method would be more convenient.

Get current place type in Google Places API

I'm working with Google Places API.
I'm getting right the place in my log, but I just want the name of the type of place. Let's say this is my output
Place 'Parque Las Tejas' has likelihood: 0.950000 Type: [69, 1013, 34]
So, at first I get the position where I am, the likelihood of where I am and then I just used:
List<Integer> types = placeLikelihood.getPlace().getPlaceTypes();
thinking it would return like "park" or "square" but instead of that I get those array of numbers [69, 1013, 34].
According to what I read here, there is lots of types that defines a certain place.
What I want is to get that kind of types only, so if I'm at a restaurant I don't want the name of the restaurant but instead just the type, so "Restaurant" will be my output.
I need this because I want to give the user options depending on what type of place they are.
Any idea what I'm doing wrong?
The List<Integer> that you get is actually the id of type of places, according to the docs:
The elements of this list are drawn from Place.TYPE_*
The list is here. So basically your goal is to convert int code to a string using this list. You can find your solution here, basically you obtain all the fields from the Place class, find all the fields that start with "TYPE", get the int value and compare it to the value that you get from the getPlaceTypes().
even though this is an old post, it will help you if you an encountering this issue,
i was encountering this issue
and here is my approach
List<Place.Type> types = placeLikelihood.getPlace().getTypes();
to get all the type you can use a foreach loop
for(Object type:types){
// get all individual type
}

Compare phone number from database in different condition

I'm developing sms APP and want to receive sms from the specific numbers. But number can be changed sometime with country code as +923201234567 or sometime without country code 03201234567 how I can compare number from database? because don't know in which format number is saved in database(with country code or without country code)
public boolean isMember(String phone, long id){
String query = "SELECT * from members where phone = ? AND active = 1 AND gid = ?";
Cursor c = dbActions.rawQuery(query, new String[]{String.valueOf(phone), String.valueOf(id)});
return c.moveToFirst();
}
Suppose if the number is saved in database without country code 03201234567 then my requirement is to get true if I compare it with country code. +923201234567. Country code could be changed.
PhoneNumberUtils.compare(); is not useful because it not compare with database.
If you can't acquire the correct information always; then you need to look into heuristics.
Meaning: you could write your own comparisons; and when you encounter two numbers like:
03201234567
+923201234567
you can figure: their "tail" is equal; the only difference is that the first one starts with 0 (so no country code) and the second one with +92. So it might be reasonable to declare those two numbers to be equal.
So a "solution" would do things like "normalize" your input (remove all non-digit content; except for leading + signs); and to then make such "tail-bound" comparisons.
If that is "too" fuzzy; I guess then you should step back and describe the requirement that you actually try to resolve here. Why are you comparing numbers; and what do you intend to do with the output of that comparison?!
Normalize all of the phone numbers into the same format before you put them into the database. That way you can just do a normal db search.
The other thing I've done for phone numbers is to convert all letters into the appropriate number, then remove all non digits, then just compare the last 7 digits.

Efficiency, Resources - store all data in one or multiple ArrayList

so this must have been asked before, but i could'nt find something useful, so sry.
Here is my scenario:
I want to store Information about Items, their price and other values in an ArrayList. Lets say we have about 10.000 items in the list when we're done. My question is, what way is the fastet / what takes less resources for the system so that the app runs most efficient.
Should i:
use one ArrayList for all the Data Like this
ArrayList<String> items = new ArrayList<String>();
items.add("name;price;from");
String[] seperated = items.get(i).split(";");
seperated[0]...
and then have to split it whenever i need to read from it.
(will be each time a customer is searching an item, i could then only split the entries that fit the search criteria)
or should i use 3 ArrayLists with 10.000 Items each.
Thanks in advance :)
Faster parsing - multiple arrays.
Less memory used - single array.
It depends on how large you expect the array(s) to become. A proper way would be to load only as many items as are needed. E.g. if on the screen you see 10 items, you load 30 and (un)load more as they are removed/requested.
I'm not 100% clear on your spec, but if it were me, I would implement an Item object:
class Item {
private String _id;
private String _name;
private String _price;
private String _from;
//getters,setters,etc.
}
Then have a HashMap to look up the Items by id.
HashMap<String,Item> items = new HashMap<>();
You can get a list of all your items via:
items.values();
Or, you can perform an O(1) lookup via id:
items.get(someId);

Using Speech Recognition to Select Radio Button on Android

I'm new to Android. Currently I'm working on a quiz using SpeechRecognizer to choose the answer of the question
answer has 5 radio button
each radio has 3 to 5 words
Answer example:
Jhonny likes an apple
Donny likes an apple
Boni likes an apple
Ronney likes an apple
Molly likes an apple
Currently my App can only select the answer if the recognized words are all matched.
what i want to do is..
how can my App select one of the answer, if recognized word is more than other.
just like this, if recognized word is Molly and apple, so the closest answer is Molly likes an apple, then my app can select it.
How can i accomplish this?
Thankyou in advance.
To make it work well, you have to do some workaround. My advices are:
to choose names, which are totally different from each other. Like: Johnny, Steven, Alfred, etc. The reason is to make sure the name will be picked up correctly and Bonny won't be mixed with Johnny.
when you have the properly chosen names, collect the recognition result for all of the names (johnny, jonny, johnnie, johny, jonnie) and create separate array or list from these
to handle these you use them as constant list or create map, according to your programming style
When you are processing the recognition result, i suggest to brake all of the results into words, so you can compare them with the expected answers.
public void onResults(Bundle results){
...
//this is your predefined name synonyms
List<String> johnnyNameList = (ArrayList<String>);
johnnyNameList.add("johnny");
johnnyNameList.add("jonny");
johnnyNameList.add("johnnie");
List<String> katieNameList = (ArrayList<String>);
katieNameList.add("katy");
katieNameList.add("katie");
katieNameList.add("kattie");
Map<String, List<String>> namesMap = new HashMap<String, List<String>>();
namesMap.put(johnny, johnnyNameList);
namesMap.put(katie, katieNameList);
...
List<String> recognitionResult = (ArrayList<String>) results.get(RESULTS_RECOGNITION);
for (String resultItem : recognitionResult) {
//brake into pieces the results
String[] messagePieces = resultItem.split(",");
//you process in case the length of the result fits
if(messagePieces.length == 4){
//compare predefined name collection with the result first element
//A) solution
for (String name : johnnyNameList) {
if(name.trim().toLowerCase().equals(messagePieces[0])){
//you have a name match
}
}
//B) solution process namesMap which contains all names
...
//you can compare the other parts of the result
//remove first element of the result
for (String piece : messagePieces) {
//count matches on the other words
}
}
}
I did something similar in my project, where i had commands and i had to recognize them and execute processes.
You can use https://github.com/Simmetrics/simmetrics
for example:
float allowD = 0.95f;
AbstractStringMetric metric = new JaroWinkler();
if (metric.getSimilarity(choice.toLowerCase(), fromTextRecognizer.trim().toLowerCase()) > allowD) {
// match found
}
This libs also provide other metrics that can be used

Categories

Resources