How to array listing index from Firebase on Android - android

0 --> David
1 --> Alice
2 --> Jayden
3 --> Nicole
4 --> David
this sequence shows 0 and 4 David's records. I want to find David's records and find index values.
find --> David
Situated shows list --> data to recyclerview listing
i want to do
My Firebase ;
"Node" : {
"-L1pFRQg8anT4_-4V60B" : {
"name" :"David"
"note" : " firs note"
}, "-L1pFRQg8anT4_-4V2342" : {
"name" :"Alice"
"note" :"note values"
}, "-L1pFRQg8anT4_-65656" : {
"name" :"Jayden"
"note" :"note values"
}, "-L1pFRQg8anT4_-sajkfhsaj" : {
"name" :"Nicole"
"note" :"note values"
}, "-L1pFRQg8anT4_-jwquh24" : {
"name" :"David"
"note" :"second note"
},
I know how to read and print data with Firebase. I know how to use FirebaseRecyclerview. But I have to find out in what order David is in the order of number. So if I called "David", it would be easy to show Davids in Recyclerview. I can do that. It is difficult to print the rank value of "David" among other data. etc 0-4
Query query = FirebaseDatabase.getInstance ()
.getreferen by ()
.child ( "node")
.orderbychild ( "name");
When I sort by name, I want to get "David" in position number and print it as "note" data and "position number".
I want to do index values

Related

Executing a for loop after one is finished in flutter

Server response returning three JSON arrays and all the objects are stored in database one by one and optionsGroupList, attributes, assignedattributes.
In the Project I have three tables are named as a group, attribute, and assignedattribute. firstly I have to insert all the elements of optionsGroupList in group table and I'm able to do so, In the second array I'm getting groupid, attributeid, attributename but in the second table I have 4 columns which have
groupid, attributeid,attributename,groupname, but for the groupname I have to get the record of a particular group by group id from group table but my second loop executes first so I'm not able to get the group record because of the loop still inserting records that time my second loop has started before completing first, my second table depends on the first and my third table depends on the second so I need to insert record after one is finished. Sometimes it works correctly
{
"result": 1,
"data": "optionsList",
"merchantid": "MER-07156",
"optionsGroupList": [{
"grouprowid": "3012",
"groupname": "Color",
"isrequired": "0"
}],
"attributes": [{
"attributerowid": "20794",
"grouprowid": "3012",
"attributename": "Red",
"weight": "0"
}],
"assignedattributes": [{
"attributegrouprowid": "154577",
"productrowid": "342702",
"attributerowid": "20794",
"subsku": "",
"quantity": "0",
"pricechange": "4"
},
{
"attributegrouprowid": "154590",
"productrowid": "354723467",
"attributerowid": "20794",
"subsku": "0",
"quantity": "0",
"pricechange": "0"
}
]
}
My Second loop debug statement printing before first.
optionListService().then((onValue) async {
if (onValue.result == 1) {
//Loop 1
for(int i=0;i<onValue.optionsGroupList.length;i++){
_insertOption(onValue.optionsGroupList[i]);
}
//Loop 2
for(int j=0;j<onValue.attributes.length;j++){
debugPrint('Attribute Name:--${ onValue.attributes[j].attributename}');// this is printed first before first loop excuted
List groupIdList=new List<Map<String,dynamic>>();
groupIdList= await dbHelper.queryReadOption(onValue.attributes[j].grouprowid); // here i'm finding group name from group table but i think is executing first.
if(groupIdList.length>0){
_insertOptionAttribute(onValue.attributes[j],groupIdList[0][DatabaseHelper.columnGroupName],groupIdList[0][DatabaseHelper.columnIsRequired]);
}
}
//Loop 3
for(int i=0;i<onValue.assignedattributes.length;i++){
String attr=onValue.assignedattributes[i].attributerowid;
arrtIdList=new List<Map<String,dynamic>>();
arrtIdList= await dbHelper.queryReadAttribute(attr);
if(arrtIdList.length>0){
_insertProductWithAttribute(
onValue.assignedattributes[i],
arrtIdList[0][DatabaseHelper.columnGroupId],
arrtIdList[0][DatabaseHelper.columnGroupName],
arrtIdList[0][DatabaseHelper.columnAttributeName],
arrtIdList[0][DatabaseHelper.columnIsRequired],
arrtIdList[0][DatabaseHelper.columnWeight]
);
}
}}});}
just add second loop in chained "then" block and in first block return "onValue" varibale. like so:
optionListService().then((onValue) async {
if (onValue.result == 1) {
//Loop 1
for(int i=0;i<onValue.optionsGroupList.length;i++){
_insertOption(onValue.optionsGroupList[i]);
}
}
return onValue;
}).then((onValue) {
if (onValue.result == 1) {
//Loop 2
for(int j=0;j<onValue.attributes.length;j++){
// some code
}
}
});

React-native-contact updateContact is updating all phone numbers for 1 contact instead of a specific number

We have a mobile application written in React-Native and we used the module React-native-contact from this guide: https://github.com/rt2zz/react-native-contacts
The purpose of the app is to change the prefix of numbers from XXX to YYY.
The contacts is well populated and all is working fine. However, we want to update 1 specific number for a Contact but unfortunately it is updating all the numbers for this Contact. We are able to get the list of contacts found in the contact book via this code:
handleUpdate = () => {
//phoneContacts => all the contacts found in the address book
//contactDetails => all the contacts that have the prefix number that needs to be replaced.
//prefixNumber => is the prefix number that user wants to replace.
const { phoneContacts, contactDetails, prefixNumber } = this.state;
var updated_select = 0;
var record = null;
Contacts.getAll( (err, contacts) => {
_.forEach(contactDetails, (contactD) => {
if (contactD[1] == true) {
//where contactD[5] is the specific phone number
var num = contactD[5];
num = num.replace(prefixNumber, '010');
_.forEach(contacts, (cont) => {
if (cont.recordID === contactD[0])
//finding the record to be updated in the Contact List
record = cont;
});
//Replacing the record phone numbers with the updated one.
record.phoneNumbers[contactD[3]] = { label: contactD[4], number: num };
//Updating the contacts as per the guide linked above
Contacts.updateContact(record, (err) => {
if (err) {
_alert('error', 'Application error: Update unsuccessful.');
return;
}
});
++updated_select;
}
});
ToastAndroid.show(updated_select + ' rows have been updated', ToastAndroid.SHORT);
this.handleSearchContacts();
return;
});
}
So as a concrete example. We are changing the prefix number from 555 to 010
If User X has
phoneNumbers: [
{label: "work",number: "(555) 555-5555"},
{label: "mobile",number: "(444) 444-4444"},
],
On update, the app is updating both numbers for User X to:
phoneNumbers: [
{label: "work",number: "(010) 555-5555"},
{label: "work",number: "(010) 555-5555"},
],
We also tried pushing all the values including the updated record but all numbers were being updated.
During debugging, we verified the value of variable record and it is showing the contact with the all its details long with the specific phone number updated correctly.
Any ideas?

Android Cloudant sync query

as docs say( https://github.com/cloudant/sync-android/blob/master/doc/query.md) im trying to do a query inside a list
look:
query.put("codigo", 4);
result = q.find(query);
for (DocumentRevision revision : result) {
Log.d("QueryTest", "Test1 :" + revision.getBody().toString());
}
return:
{ "codigo":4, "companies":[
{
"id":"b9f19d88-13c0-40e3-89de-63dc787afb4c",
"name":"Filial 0 1488949817178"
},
{
"id":"f17fb098-316e-4d33-a0f7-f5719bf9d62e",
"name":"Filial 1 1488949817178"
} ], "employees":[
{
"codigo":2891,
"id":"cc54fa37-0b64-4108-869a-1303c6176ce5",
"name":"Employee 0 79ed4"
},
{
"codigo":4642,
"id":"19b76bbc-82c7-4295-a385-82ac2d892458",
"name":"Employee 1 e1102"
} ], "id":"ef2d0ebf-50b9-4cd0-9aaf-5389bccaaa56", "nome":"Random 4" }
so its ok
but this query doesnt work:
query.put("employees.codigo", 2891);
first return:
QuerySqlTranslator: No single index contains all of
[{employees.codigo={$eq=2891}}]; add index for these fields to query
efficiently
after created the index:
Index i = q.createJsonIndex(Arrays.<FieldSort>asList(new FieldSort("employees.codigo")), null);
return
QueryExecutor: Projection fields array is empty, disabling project for
this query
so i created the arraylist of fields to filter
List<String> fields = Arrays.asList("codigo");
result = q.find(query, 0 , 0 , fields, null);
nothing returned
adding more one field to filter:
query.put("codigo",4)
query.put("employees.codigo", 2891);
returned: Complain about index
Created another index:
i = q.createJsonIndex(Arrays.<FieldSort>asList(new FieldSort("employees.codigo"), new FieldSort("codigo")), null);
returned: Nothing
whats is wrong?
How can i get document by child and how can i get ONLY the children?
thanks
I think this is unsupported, see the doc on unsupported features, specifically the highlighted entry:
Arrays
Dotted notation to index or query sub-documents in arrays.
Querying for exact array match, { field: [ 1, 3, 7 ] }.
Querying to match a specific array element using dotted notation, { field.0: 1 }.
Querying using $all.
Querying using $elemMatch.

Can I use Cursor in RecyclerView.Adapter for binding Cartesian product (two joined tables)?

Maybe I didn't quite understand the purpose and possibilities of the Cursor. I am interested in the issue.
I have two tables, and one-to-many relationship
Content
------------------
id
article_id
index
type
text
images
Images
------------------
id
content_id
content_index
image_index
url
title
So, I want see all content for article with id = 1.
SELECT * FROM content
WHERE article_id = 1
Result rows:
id article_id index type text images
-------------------------------------------------------------
1 1 0 "text" "Text1" NULL
2 1 1 "text" "Text2" NULL
3 1 2 "text" "Text3" NULL
Ok, next in my CursorRecyclerViewAdapter I get cursor for each row in result and bind it to UI:
#Override
public void onBindViewHolder(ViewHolder viewHolder, Cursor cursor) {
String type = cursor.getString(TYPE);
String text = cursor.getString(TEXT);
//......
}
But if content has type "images", I can use JOIN for joined tables and get result Cartesian product, something like:
id article_id index type text url title
-------------------------------------------------------------------------
1 1 0 "text" "Text1" NULL NULL
2 1 1 "text" "Text2" NULL NULL
3 1 2 "images" NULL "image1.png" "img1"
3 1 2 "images" NULL "image2.png" "img2"
3 1 2 "images" NULL "image3.png" "img3"
4 1 3 "text" "Text3" NULL NULL
In this case how use cursor in
onBindViewHolder(ViewHolder viewHolder, Cursor cursor);
There are typical solutions for this issue? I was looking for a solution to the StackOverflow, but with no results, I begin to doubt that in such cases, you can use the Cursor?
UPDATE
If use GROUP BY index I don't see all images for content row:
id article_id index type text url title
-------------------------------------------------------------------------
1 1 0 "text" "Text1" NULL NULL
2 1 1 "text" "Text2" NULL NULL
3 1 2 "images" NULL "image1.png" "img1"
4 1 3 "text" "Text3" NULL NULL

Filter array field in couchbase

I m working on couchbase lite android.
I have series of documents, every document contains a field which it's value is array of string.
now I want to filter value of this field.
{
type : "customer",
name: "customerX",
states: [ "IL" , "IO" , "NY" , "CA" ]
},
{
type : "customer",
name: "customerY",
states: [ "WY" , "CA", "WA" ]
},
{
type : "customer",
name: "customerZ",
states: [ "NY" ]
}
I want to get customer documents which have "CA" in their states field.
I m using CBL Android.
emit(states , null);
then how could I make my Start and End Key option.
startkey("CA")
Or
startKey(["CA"])
customerX customerY
how can i get only customerX and customerY by "CA" in their states field ?!
If you are just wanting to filter on one state then your best solution would be to make a view like this:
function (doc, meta) {
if(doc.type && doc.type == "customer") {
if(doc.states) {
for (index = 0, len = doc.states.length; index < len; ++index) {
emit(doc.states[index],null);
}
}
}
}
This view will emit a view row for each state in the arrays of each of your documents of type 'customer'. This means that you can then select an individual state with a startkey of "CA" and an endkey of "CA", remember to set the inclusive_true flag to true so that endkeys that match are included:
startkey="CA"&endkey="CA"&inclusive_end=true
Hope that helps!

Categories

Resources