I apologize for my English, I use Google translator.
The question is: how can I get the value of a particular column of a particular row. As an example - there are two columns "cost" and the "margin" in the database options scored 10 lines, how can I get a "cost" in line 5?
When I do so -
SaveData item = SaveData.findById(SaveData.class, 5);
Log.d(LOG_TAG, String.valueOf(item.cost));
then I crash
It is the correct way to retrieve objects providing their identifier. However the "5" represents the object of type SaveData which the identifier is equal to 5, and not the #5 row.
So, check which is the identifier of the object you want to retrieve, or alternatively, get all the objects stored in the database and the select the #5 row as follows (the code is to be considered as a sample, check it before to try)
List<SaveData> list = SaveData.listAll(SaveData.class);
if (list.size() > 4) { // it means that there are at lease 5 elements
return list.get(4); //it retrieves the #5 element of the list/database
}
Related
E.g. I have next data in my Firebase Realtime Database (my json file contains json array of some objects):
I would like to get all objects starting from a specific index (position) to the end of this array (in Firebase DB json array is converted to simple object which contains children with keys 001, 002 and so on)
So I need something like this but for Firebase query:
list.subList(10, list.size)
I know there are limitToFirst, limitToLast methods but it's different
fireDatabaseReference.child("episodes").limitToLast(10)
It won't do what I need. Because I need to know the size of this array and I need to be sure that this array won't become bigger at the moment someone makes such request (at some point this array can get become bigger due to adding new objects)
Would be great to have a method like from to get all children from 10 to the end (so first 9 children are excluded):
fireDatabaseReference.child("episodes").from(10)
But there is no such method
Is there any solution for this?
Solved :)
fireDatabaseReference.child("episodes").orderByKey().startAt("10")
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
}
Advertissement: I'm using CouchbaseLite Android 1.4.0 and I'm not ready to switch to 2.0.
To get the context of my issue:
I have 2 doc types: PATIENT & VISIT
A patient is linked to a visit by a field visitId
A visit has a startDateTime and a endDateTime
A visit is considered as 'open' if the endDateTime is null or missing
A visit can be of different types through the field visitType
A visit can be inactive through the field inactive
I want to:
Get all patients (id + name) of all open and active visits of a certain type
The result is paginated using skip and limit
Get the result sorted by name
I managed to get all the correct patients and to paginate the result but the issue is with the sorting.
I can't make a 'post sorting' (getting the result in a random order and then use self-made method to sort them) because of the pagination.
So here is how it works actually without the sorting:
THE MAP/REDUCE
if (isVisit()) {
Object inactive = document.get(FIELDS.INACTIVE);
if(inactive == null)
inactive = false;
Document patientDoc = database.getExistingDocument(FIELDS.PATIENT_ID);
if(patientDoc != null && patientDoc.getProperties() != null) {
Object[] keys = {
document.get(CouchbaseVisitManager.FIELDS.STOP_DATE_TIME),
inactive,
document.get(CouchbaseVisitManager.FIELDS.VISIT_TYPE)
};
Object[] values = {
document.get(FIELDS.PATIENT_ID),
patientDoc.getProperties().get(FIELDS.FAMILY_NAME),
patientDoc.getProperties().get(FIELDS.FIRST_NAME)
}
emitter.emit(keys, values);
}
}
THE QUERY
Query query = getQuery(Views.PATIENTS_CURRENTLY_IN_VISIT); //Helper method to create a query
query.setKeys(new ArrayList<Object>(){{
add(new ArrayList<Object>(){{
add(null);
add(false);
add(visitType);
}});
}});
query.setSkip(skip);
query.setLimit(limit);
And this is working well to get my patients but not for sorting them.
I tried to add patient names inside the view like this:
Object[] keys = {
patientDoc.getProperties().get(FIELDS.FAMILY_NAME),
patientDoc.getProperties().get(FIELDS.FIRST_NAME),
document.get(FIELDS.STOP_DATE_TIME),
inactive,
document.get(FIELDS.VISIT_TYPE)
};
And update my query.keys like this:
query.setStartKey(new ArrayList<Object>(){{
add(null);
add(null);
add(null);
add(false);
add(visitType);
}});
query.setEndKey(new ArrayList<Object>(){{
add(new HashMap<>());
add(new HashMap<>());
add(null);
add(false);
add(visitType);
}});
But this returns no patients (result.size = 0)
So... I don't know how to achieve my goal.
I tought of a way to sort by values but it doesn't seem to exist yet (should wait for 2.0 I think). But is their any workaround to achieve this kind of behavior?
Thank's for reading.
View results are always sorted by key. So you should add the patient name to the key and then you'll get your sorting.
Be careful to put the name into the correct place of the keys array since the sorting is lexicographical, that is if the name is going to be the last component then you'll get all open inactive sorted by name, then all open active sorted by name e.t.c.
In case you need to put your name key in front of the other keys but still be able to filter your results, then you will need to properly construct the start key and the end key so that the full range of every possible patient names would be accepted while the later keys would be filtered.
You will have to provide some values which will definitely be less than or greater than any possible patient name.
Following the key collation defined for Couchbase views, a null will be always less than any string, and an empty map {} can be used to match past the last string. You are basically doing it right in your last sample.
Don't forget to switch the start and end keys logic if you would like to query the view in reverse mode
Another thing to take care of is always incrementing the version of the view after changing its logic or key set, otherwise you will continue getting old results. Incrementing the version will force a refresh of the view, executing the updated logic in the view function over all your data.
I have an extremely long list of items (over 200) and a database that displays the list. I have an on click event that will compare that if the first item is an "apple" then when you click it, facts come up about an "apple". The problem is that this list isn't a SET list, meaning the the word "apple" could be in the 1st spot or it could be in the 18th spot.
I started doing an if statement that compares like this:
case 0:
if (text.equals("apple")) {
[show facts about apple]
} else if (text.equals("orange")) {
[show facts about orange]
//this would continue on to compare the ENTIRE LIST (about 500 lines per case)
break;
The problem is that i got an error that states:
The code of method onListItemClick(ListView, View, int, long) is exceeding the 65535 bytes limit
There must be an easier way to do this, right?
You can put the facts in a database and use the items to index the table. Then, your if will be evaluated by the database. It will look like select fact from facts where item='apple'. Might that help? It would also easily be possible to add, remove or alter information. Also, the look-up (the if-evaluation) is very fast with the aid of database indexes.
First of all, to just solve your "method too long" problem, there are several ways.
#1 move all your description into strings.xml.
if (text.equals("apple")) {
result = getResources().getString(R.string.apple_description);
}
#2 move your if-else into a separated method.
case 0:
mydescription = getDescription(text); // getDescription() is the big if-else you have
BUT..... it is still very bad to code in such a way.
Please consider following:
#1 Create a HashMap for name and description.
Apple - Description For Apple
Orange - Description For Orange
#2 In your list adapter, set a tag as indicator.
view.setTag("apple");
#3 In your onListItemClick, read this tag and get description.
String text = view.getTag();
String description = myhashmap.get(text).toString();
// If your hashmap is mapping name and string resource id then:
String description = getResources().getString(Integer.parseInt(myhashmap.get(text)));
String[] array =
getContext().getResources().getStringArray(R.array.DevCategories);
to get an array. I the call this:
Arrays.binarySearch(array,"Plan"));
it returns -5, I am sure the first value of the array is Plan. What happend?
By the way, the current class is extended from ContentProvider. I am writing a Provider.
I use this code get -5
String.format("%d",Arrays.binarySearch(array,"Plan")));
String.valueOf(Arrays.binarySearch(array,"Plan")));
both are -5
R.array.DevCategories from string.xml file.
<string-array name="DevCategories">
<item>Plan</item>
<item>Design</item>
<item>Coding</item>
<item>Debug</item>
<item>Test</item>
<item>Release</item>
</string-array>
Are you sure that the Array is sorted according to the natural sort order (for Strings in this case)?
Results are undefined if the Array is not sorted.
A negative result normally indicates that the key was not found and the result can be used to calculate the index at which the key should be inserted to add it to the Array (to maintain the proper sorting). This will be bogus if the Array was not sorted to begin with.