Firestore fetch array from collection
I have collection in firestore which return the response as follows:
{packs=[{subcode=s1, weight=1, price=12}], name=abc, desc=Good, code=001}
How I can create model for this response and parse this data in Android.
In the current model, It returns the packs null, I am getting the data desc, name and code but packs is null, packs is a array.
Java code for fetching data:
mFirestore.collection("catend").addSnapshotListener(new EventListener() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (e != null) {
Log.d(TAG, "onEvent: error" + e.getMessage());
}
for (DocumentChange document : documentSnapshots.getDocumentChanges()) {
switch (document.getType()) {
case ADDED:
ProductTest productModel=document.getDocument().toObject(ProductTest.class);
Log.d(TAG, "onEvent: response"+document.getDocument().getData());
Log.d(TAG, "onEvent: code="+productModel.getCode()); //work
Log.d(TAG, "onEvent: description="+productModel.getDesc()); //work
Log.d(TAG, "onEvent: name="+productModel.getName()); //work
Log.d(TAG, "onEvent: packs"+productModel.getPacksList()); //not work
break;
case REMOVED:
break;
case MODIFIED:
break;
}
}
}
});
Model class:
public class ProductTest {
String code,desc,name;
List<Packs> packs;
public ProductTest() {
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Packs> getPacksList() {
return packs;
}
public void setPacksList(List<Packs> packsList) {
this.packs = packsList;
}
public class Packs
{
String subcode;
int price;
public String getSubcode() {
return subcode;
}
public void setSubcode(String subcode) {
this.subcode = subcode;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Packs() {
}
}
}
Debug result:
You can see in the images
Firebase structure
Andoird studio log
java android firebase google-cloud-firestore
add a comment |
I have collection in firestore which return the response as follows:
{packs=[{subcode=s1, weight=1, price=12}], name=abc, desc=Good, code=001}
How I can create model for this response and parse this data in Android.
In the current model, It returns the packs null, I am getting the data desc, name and code but packs is null, packs is a array.
Java code for fetching data:
mFirestore.collection("catend").addSnapshotListener(new EventListener() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (e != null) {
Log.d(TAG, "onEvent: error" + e.getMessage());
}
for (DocumentChange document : documentSnapshots.getDocumentChanges()) {
switch (document.getType()) {
case ADDED:
ProductTest productModel=document.getDocument().toObject(ProductTest.class);
Log.d(TAG, "onEvent: response"+document.getDocument().getData());
Log.d(TAG, "onEvent: code="+productModel.getCode()); //work
Log.d(TAG, "onEvent: description="+productModel.getDesc()); //work
Log.d(TAG, "onEvent: name="+productModel.getName()); //work
Log.d(TAG, "onEvent: packs"+productModel.getPacksList()); //not work
break;
case REMOVED:
break;
case MODIFIED:
break;
}
}
}
});
Model class:
public class ProductTest {
String code,desc,name;
List<Packs> packs;
public ProductTest() {
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Packs> getPacksList() {
return packs;
}
public void setPacksList(List<Packs> packsList) {
this.packs = packsList;
}
public class Packs
{
String subcode;
int price;
public String getSubcode() {
return subcode;
}
public void setSubcode(String subcode) {
this.subcode = subcode;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Packs() {
}
}
}
Debug result:
You can see in the images
Firebase structure
Andoird studio log
java android firebase google-cloud-firestore
what problem are you facing?
– Mohammed Farhan
Feb 3 at 5:02
packs is a array which returns null, I have also share my debug results screenshot . please guides me..thank you
– vikas
Feb 3 at 5:07
changeList<Packs> packs
toMap<String,Packs> mapPacks
and to obtain field value from pack usemapPacks.getObject("subcode");
like this
– Mohammed Farhan
Feb 3 at 5:25
getmapPacks return null
– vikas
Feb 3 at 8:05
add a comment |
I have collection in firestore which return the response as follows:
{packs=[{subcode=s1, weight=1, price=12}], name=abc, desc=Good, code=001}
How I can create model for this response and parse this data in Android.
In the current model, It returns the packs null, I am getting the data desc, name and code but packs is null, packs is a array.
Java code for fetching data:
mFirestore.collection("catend").addSnapshotListener(new EventListener() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (e != null) {
Log.d(TAG, "onEvent: error" + e.getMessage());
}
for (DocumentChange document : documentSnapshots.getDocumentChanges()) {
switch (document.getType()) {
case ADDED:
ProductTest productModel=document.getDocument().toObject(ProductTest.class);
Log.d(TAG, "onEvent: response"+document.getDocument().getData());
Log.d(TAG, "onEvent: code="+productModel.getCode()); //work
Log.d(TAG, "onEvent: description="+productModel.getDesc()); //work
Log.d(TAG, "onEvent: name="+productModel.getName()); //work
Log.d(TAG, "onEvent: packs"+productModel.getPacksList()); //not work
break;
case REMOVED:
break;
case MODIFIED:
break;
}
}
}
});
Model class:
public class ProductTest {
String code,desc,name;
List<Packs> packs;
public ProductTest() {
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Packs> getPacksList() {
return packs;
}
public void setPacksList(List<Packs> packsList) {
this.packs = packsList;
}
public class Packs
{
String subcode;
int price;
public String getSubcode() {
return subcode;
}
public void setSubcode(String subcode) {
this.subcode = subcode;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Packs() {
}
}
}
Debug result:
You can see in the images
Firebase structure
Andoird studio log
java android firebase google-cloud-firestore
I have collection in firestore which return the response as follows:
{packs=[{subcode=s1, weight=1, price=12}], name=abc, desc=Good, code=001}
How I can create model for this response and parse this data in Android.
In the current model, It returns the packs null, I am getting the data desc, name and code but packs is null, packs is a array.
Java code for fetching data:
mFirestore.collection("catend").addSnapshotListener(new EventListener() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (e != null) {
Log.d(TAG, "onEvent: error" + e.getMessage());
}
for (DocumentChange document : documentSnapshots.getDocumentChanges()) {
switch (document.getType()) {
case ADDED:
ProductTest productModel=document.getDocument().toObject(ProductTest.class);
Log.d(TAG, "onEvent: response"+document.getDocument().getData());
Log.d(TAG, "onEvent: code="+productModel.getCode()); //work
Log.d(TAG, "onEvent: description="+productModel.getDesc()); //work
Log.d(TAG, "onEvent: name="+productModel.getName()); //work
Log.d(TAG, "onEvent: packs"+productModel.getPacksList()); //not work
break;
case REMOVED:
break;
case MODIFIED:
break;
}
}
}
});
Model class:
public class ProductTest {
String code,desc,name;
List<Packs> packs;
public ProductTest() {
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Packs> getPacksList() {
return packs;
}
public void setPacksList(List<Packs> packsList) {
this.packs = packsList;
}
public class Packs
{
String subcode;
int price;
public String getSubcode() {
return subcode;
}
public void setSubcode(String subcode) {
this.subcode = subcode;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Packs() {
}
}
}
Debug result:
You can see in the images
Firebase structure
Andoird studio log
java android firebase google-cloud-firestore
java android firebase google-cloud-firestore
edited Feb 3 at 9:45
asked Feb 3 at 4:52
vikas
187
187
what problem are you facing?
– Mohammed Farhan
Feb 3 at 5:02
packs is a array which returns null, I have also share my debug results screenshot . please guides me..thank you
– vikas
Feb 3 at 5:07
changeList<Packs> packs
toMap<String,Packs> mapPacks
and to obtain field value from pack usemapPacks.getObject("subcode");
like this
– Mohammed Farhan
Feb 3 at 5:25
getmapPacks return null
– vikas
Feb 3 at 8:05
add a comment |
what problem are you facing?
– Mohammed Farhan
Feb 3 at 5:02
packs is a array which returns null, I have also share my debug results screenshot . please guides me..thank you
– vikas
Feb 3 at 5:07
changeList<Packs> packs
toMap<String,Packs> mapPacks
and to obtain field value from pack usemapPacks.getObject("subcode");
like this
– Mohammed Farhan
Feb 3 at 5:25
getmapPacks return null
– vikas
Feb 3 at 8:05
what problem are you facing?
– Mohammed Farhan
Feb 3 at 5:02
what problem are you facing?
– Mohammed Farhan
Feb 3 at 5:02
packs is a array which returns null, I have also share my debug results screenshot . please guides me..thank you
– vikas
Feb 3 at 5:07
packs is a array which returns null, I have also share my debug results screenshot . please guides me..thank you
– vikas
Feb 3 at 5:07
change
List<Packs> packs
to Map<String,Packs> mapPacks
and to obtain field value from pack use mapPacks.getObject("subcode");
like this– Mohammed Farhan
Feb 3 at 5:25
change
List<Packs> packs
to Map<String,Packs> mapPacks
and to obtain field value from pack use mapPacks.getObject("subcode");
like this– Mohammed Farhan
Feb 3 at 5:25
getmapPacks return null
– vikas
Feb 3 at 8:05
getmapPacks return null
– vikas
Feb 3 at 8:05
add a comment |
2 Answers
2
active
oldest
votes
Alex Mamo answer help me alot, I changed my model code:
public class ProductTest {
String code,desc,name;
ArrayList<Object> packs;
public ArrayList<Object> getPacks() {
return packs;
}
public void setPacks(ArrayList<Object> packs) {
this.packs = packs;
}
public ProductTest() {
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
I also updated my MainActivity.java class where I am fetching data:
mFirestore.collection("catend").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (e != null) {
Log.d(TAG, "onEvent: error" + e.getMessage());
}
for (DocumentChange document : documentSnapshots.getDocumentChanges()) {
switch (document.getType()) {
case ADDED:
ProductTest productModel=document.getDocument().toObject(ProductTest.class);
Log.d(TAG, "onEvent: response"+document.getDocument().getData());
Log.d(TAG, "onEvent: code="+productModel.getCode()); //work
Log.d(TAG, "onEvent: description="+productModel.getDesc()); //work
Log.d(TAG, "onEvent: name="+productModel.getName()); //work
Log.d(TAG, "onEvent: packs"+productModel.getPacks()); //Work
for (int i = 0; i <productModel.getPacks().size() ; i++) {
try {
JSONObject jsonObject=new JSONObject(productModel.getPacks().get(i).toString());
Log.d(TAG, "onEvent: subcode= "+jsonObject.getString("subcode"));
Log.d(TAG, "onEvent: subprice="+jsonObject.getString("price"));
Log.d(TAG, "onEvent: weight="+jsonObject.getString("weight"));
} catch (JSONException e1) {
e1.printStackTrace();
}
}
break;
case REMOVED:
break;
case MODIFIED:
break;
}
}
}
});
Output:
Android studio logcat
ChangingList<Packs> packs;
toArrayList<Object> packs;
makes no difference since in Java, anyArrayList
is-aList
and everyPacks
is-a Object. The solution is what I have written in my updated answer. So hope you reconsider accepting my answer.
– Alex Mamo
yesterday
add a comment |
I see in your Firestore response that the packs
object is a List
and not an array. I also see the same thing in your model class, List<Packs> packs
.
The main idea is not to create a model class according with the data that you already have in your database. So a question such "How I can create model for this response?" is not a correct, because this is not how the things works. You create the model class in the first place and then you add data to the database in the way you have structured your helper class.
So the packs
property from your document si actually a Map
. To print those value out please change this line of code:
Log.d(TAG, "onEvent: packs"+productModel.getPacksList()); //not work
with
Map<String, Object> packs = (Map<String, Object>) documentSnapshot.get("packs");
Map<String, Object> zero = (Map<String, Object>) packs.get("0");
String price = (String) zero.get("price");
String subcode = (String) zero.get("subcode");
String weight = (String) zero.get("price");
Log.d(TAG, price + " / " + subcode + " / " + weight);
Your output will be: 12 / s1 / 1
Edit:
Looking again at your model class, the problem in your code is the getter. So if you want to use:
productModel.getPacksList();
Please change the following getter:
public List<Packs> getPacksList() {
return packs;
}
to
public List<Packs> getPacks() {
return packs;
}
And then simply call:
productModel.getPacks();
That's it!
How i can parse this response: {packs=[{subcode=s1, weight=1, price=12}], name=abc, desc=good, code=001} , please provide some code so that I can understand what you want to say
– vikas
Feb 3 at 9:01
As I see in your image, isnull
. You cannot parse somethig that has the value ofnull
.
– Alex Mamo
Feb 3 at 9:03
That is my question, why its comming null, because packs have some data in it, you can check here, packs have data such as subcode, weight and price, Does my model is wrong? ...{packs=[{subcode=s1, weight=1, price=12}], name=abc, desc=good, code=001}
– vikas
Feb 3 at 9:23
Are getting the other values correct? Please add a more details database structure.
– Alex Mamo
Feb 3 at 9:24
I have added my firestore structure and android studio log image
– vikas
Feb 3 at 9:46
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48594059%2ffirestore-fetch-array-from-collection%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Alex Mamo answer help me alot, I changed my model code:
public class ProductTest {
String code,desc,name;
ArrayList<Object> packs;
public ArrayList<Object> getPacks() {
return packs;
}
public void setPacks(ArrayList<Object> packs) {
this.packs = packs;
}
public ProductTest() {
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
I also updated my MainActivity.java class where I am fetching data:
mFirestore.collection("catend").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (e != null) {
Log.d(TAG, "onEvent: error" + e.getMessage());
}
for (DocumentChange document : documentSnapshots.getDocumentChanges()) {
switch (document.getType()) {
case ADDED:
ProductTest productModel=document.getDocument().toObject(ProductTest.class);
Log.d(TAG, "onEvent: response"+document.getDocument().getData());
Log.d(TAG, "onEvent: code="+productModel.getCode()); //work
Log.d(TAG, "onEvent: description="+productModel.getDesc()); //work
Log.d(TAG, "onEvent: name="+productModel.getName()); //work
Log.d(TAG, "onEvent: packs"+productModel.getPacks()); //Work
for (int i = 0; i <productModel.getPacks().size() ; i++) {
try {
JSONObject jsonObject=new JSONObject(productModel.getPacks().get(i).toString());
Log.d(TAG, "onEvent: subcode= "+jsonObject.getString("subcode"));
Log.d(TAG, "onEvent: subprice="+jsonObject.getString("price"));
Log.d(TAG, "onEvent: weight="+jsonObject.getString("weight"));
} catch (JSONException e1) {
e1.printStackTrace();
}
}
break;
case REMOVED:
break;
case MODIFIED:
break;
}
}
}
});
Output:
Android studio logcat
ChangingList<Packs> packs;
toArrayList<Object> packs;
makes no difference since in Java, anyArrayList
is-aList
and everyPacks
is-a Object. The solution is what I have written in my updated answer. So hope you reconsider accepting my answer.
– Alex Mamo
yesterday
add a comment |
Alex Mamo answer help me alot, I changed my model code:
public class ProductTest {
String code,desc,name;
ArrayList<Object> packs;
public ArrayList<Object> getPacks() {
return packs;
}
public void setPacks(ArrayList<Object> packs) {
this.packs = packs;
}
public ProductTest() {
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
I also updated my MainActivity.java class where I am fetching data:
mFirestore.collection("catend").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (e != null) {
Log.d(TAG, "onEvent: error" + e.getMessage());
}
for (DocumentChange document : documentSnapshots.getDocumentChanges()) {
switch (document.getType()) {
case ADDED:
ProductTest productModel=document.getDocument().toObject(ProductTest.class);
Log.d(TAG, "onEvent: response"+document.getDocument().getData());
Log.d(TAG, "onEvent: code="+productModel.getCode()); //work
Log.d(TAG, "onEvent: description="+productModel.getDesc()); //work
Log.d(TAG, "onEvent: name="+productModel.getName()); //work
Log.d(TAG, "onEvent: packs"+productModel.getPacks()); //Work
for (int i = 0; i <productModel.getPacks().size() ; i++) {
try {
JSONObject jsonObject=new JSONObject(productModel.getPacks().get(i).toString());
Log.d(TAG, "onEvent: subcode= "+jsonObject.getString("subcode"));
Log.d(TAG, "onEvent: subprice="+jsonObject.getString("price"));
Log.d(TAG, "onEvent: weight="+jsonObject.getString("weight"));
} catch (JSONException e1) {
e1.printStackTrace();
}
}
break;
case REMOVED:
break;
case MODIFIED:
break;
}
}
}
});
Output:
Android studio logcat
ChangingList<Packs> packs;
toArrayList<Object> packs;
makes no difference since in Java, anyArrayList
is-aList
and everyPacks
is-a Object. The solution is what I have written in my updated answer. So hope you reconsider accepting my answer.
– Alex Mamo
yesterday
add a comment |
Alex Mamo answer help me alot, I changed my model code:
public class ProductTest {
String code,desc,name;
ArrayList<Object> packs;
public ArrayList<Object> getPacks() {
return packs;
}
public void setPacks(ArrayList<Object> packs) {
this.packs = packs;
}
public ProductTest() {
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
I also updated my MainActivity.java class where I am fetching data:
mFirestore.collection("catend").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (e != null) {
Log.d(TAG, "onEvent: error" + e.getMessage());
}
for (DocumentChange document : documentSnapshots.getDocumentChanges()) {
switch (document.getType()) {
case ADDED:
ProductTest productModel=document.getDocument().toObject(ProductTest.class);
Log.d(TAG, "onEvent: response"+document.getDocument().getData());
Log.d(TAG, "onEvent: code="+productModel.getCode()); //work
Log.d(TAG, "onEvent: description="+productModel.getDesc()); //work
Log.d(TAG, "onEvent: name="+productModel.getName()); //work
Log.d(TAG, "onEvent: packs"+productModel.getPacks()); //Work
for (int i = 0; i <productModel.getPacks().size() ; i++) {
try {
JSONObject jsonObject=new JSONObject(productModel.getPacks().get(i).toString());
Log.d(TAG, "onEvent: subcode= "+jsonObject.getString("subcode"));
Log.d(TAG, "onEvent: subprice="+jsonObject.getString("price"));
Log.d(TAG, "onEvent: weight="+jsonObject.getString("weight"));
} catch (JSONException e1) {
e1.printStackTrace();
}
}
break;
case REMOVED:
break;
case MODIFIED:
break;
}
}
}
});
Output:
Android studio logcat
Alex Mamo answer help me alot, I changed my model code:
public class ProductTest {
String code,desc,name;
ArrayList<Object> packs;
public ArrayList<Object> getPacks() {
return packs;
}
public void setPacks(ArrayList<Object> packs) {
this.packs = packs;
}
public ProductTest() {
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
I also updated my MainActivity.java class where I am fetching data:
mFirestore.collection("catend").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (e != null) {
Log.d(TAG, "onEvent: error" + e.getMessage());
}
for (DocumentChange document : documentSnapshots.getDocumentChanges()) {
switch (document.getType()) {
case ADDED:
ProductTest productModel=document.getDocument().toObject(ProductTest.class);
Log.d(TAG, "onEvent: response"+document.getDocument().getData());
Log.d(TAG, "onEvent: code="+productModel.getCode()); //work
Log.d(TAG, "onEvent: description="+productModel.getDesc()); //work
Log.d(TAG, "onEvent: name="+productModel.getName()); //work
Log.d(TAG, "onEvent: packs"+productModel.getPacks()); //Work
for (int i = 0; i <productModel.getPacks().size() ; i++) {
try {
JSONObject jsonObject=new JSONObject(productModel.getPacks().get(i).toString());
Log.d(TAG, "onEvent: subcode= "+jsonObject.getString("subcode"));
Log.d(TAG, "onEvent: subprice="+jsonObject.getString("price"));
Log.d(TAG, "onEvent: weight="+jsonObject.getString("weight"));
} catch (JSONException e1) {
e1.printStackTrace();
}
}
break;
case REMOVED:
break;
case MODIFIED:
break;
}
}
}
});
Output:
Android studio logcat
answered Feb 3 at 14:33
vikas
187
187
ChangingList<Packs> packs;
toArrayList<Object> packs;
makes no difference since in Java, anyArrayList
is-aList
and everyPacks
is-a Object. The solution is what I have written in my updated answer. So hope you reconsider accepting my answer.
– Alex Mamo
yesterday
add a comment |
ChangingList<Packs> packs;
toArrayList<Object> packs;
makes no difference since in Java, anyArrayList
is-aList
and everyPacks
is-a Object. The solution is what I have written in my updated answer. So hope you reconsider accepting my answer.
– Alex Mamo
yesterday
Changing
List<Packs> packs;
to ArrayList<Object> packs;
makes no difference since in Java, any ArrayList
is-a List
and every Packs
is-a Object. The solution is what I have written in my updated answer. So hope you reconsider accepting my answer.– Alex Mamo
yesterday
Changing
List<Packs> packs;
to ArrayList<Object> packs;
makes no difference since in Java, any ArrayList
is-a List
and every Packs
is-a Object. The solution is what I have written in my updated answer. So hope you reconsider accepting my answer.– Alex Mamo
yesterday
add a comment |
I see in your Firestore response that the packs
object is a List
and not an array. I also see the same thing in your model class, List<Packs> packs
.
The main idea is not to create a model class according with the data that you already have in your database. So a question such "How I can create model for this response?" is not a correct, because this is not how the things works. You create the model class in the first place and then you add data to the database in the way you have structured your helper class.
So the packs
property from your document si actually a Map
. To print those value out please change this line of code:
Log.d(TAG, "onEvent: packs"+productModel.getPacksList()); //not work
with
Map<String, Object> packs = (Map<String, Object>) documentSnapshot.get("packs");
Map<String, Object> zero = (Map<String, Object>) packs.get("0");
String price = (String) zero.get("price");
String subcode = (String) zero.get("subcode");
String weight = (String) zero.get("price");
Log.d(TAG, price + " / " + subcode + " / " + weight);
Your output will be: 12 / s1 / 1
Edit:
Looking again at your model class, the problem in your code is the getter. So if you want to use:
productModel.getPacksList();
Please change the following getter:
public List<Packs> getPacksList() {
return packs;
}
to
public List<Packs> getPacks() {
return packs;
}
And then simply call:
productModel.getPacks();
That's it!
How i can parse this response: {packs=[{subcode=s1, weight=1, price=12}], name=abc, desc=good, code=001} , please provide some code so that I can understand what you want to say
– vikas
Feb 3 at 9:01
As I see in your image, isnull
. You cannot parse somethig that has the value ofnull
.
– Alex Mamo
Feb 3 at 9:03
That is my question, why its comming null, because packs have some data in it, you can check here, packs have data such as subcode, weight and price, Does my model is wrong? ...{packs=[{subcode=s1, weight=1, price=12}], name=abc, desc=good, code=001}
– vikas
Feb 3 at 9:23
Are getting the other values correct? Please add a more details database structure.
– Alex Mamo
Feb 3 at 9:24
I have added my firestore structure and android studio log image
– vikas
Feb 3 at 9:46
|
show 3 more comments
I see in your Firestore response that the packs
object is a List
and not an array. I also see the same thing in your model class, List<Packs> packs
.
The main idea is not to create a model class according with the data that you already have in your database. So a question such "How I can create model for this response?" is not a correct, because this is not how the things works. You create the model class in the first place and then you add data to the database in the way you have structured your helper class.
So the packs
property from your document si actually a Map
. To print those value out please change this line of code:
Log.d(TAG, "onEvent: packs"+productModel.getPacksList()); //not work
with
Map<String, Object> packs = (Map<String, Object>) documentSnapshot.get("packs");
Map<String, Object> zero = (Map<String, Object>) packs.get("0");
String price = (String) zero.get("price");
String subcode = (String) zero.get("subcode");
String weight = (String) zero.get("price");
Log.d(TAG, price + " / " + subcode + " / " + weight);
Your output will be: 12 / s1 / 1
Edit:
Looking again at your model class, the problem in your code is the getter. So if you want to use:
productModel.getPacksList();
Please change the following getter:
public List<Packs> getPacksList() {
return packs;
}
to
public List<Packs> getPacks() {
return packs;
}
And then simply call:
productModel.getPacks();
That's it!
How i can parse this response: {packs=[{subcode=s1, weight=1, price=12}], name=abc, desc=good, code=001} , please provide some code so that I can understand what you want to say
– vikas
Feb 3 at 9:01
As I see in your image, isnull
. You cannot parse somethig that has the value ofnull
.
– Alex Mamo
Feb 3 at 9:03
That is my question, why its comming null, because packs have some data in it, you can check here, packs have data such as subcode, weight and price, Does my model is wrong? ...{packs=[{subcode=s1, weight=1, price=12}], name=abc, desc=good, code=001}
– vikas
Feb 3 at 9:23
Are getting the other values correct? Please add a more details database structure.
– Alex Mamo
Feb 3 at 9:24
I have added my firestore structure and android studio log image
– vikas
Feb 3 at 9:46
|
show 3 more comments
I see in your Firestore response that the packs
object is a List
and not an array. I also see the same thing in your model class, List<Packs> packs
.
The main idea is not to create a model class according with the data that you already have in your database. So a question such "How I can create model for this response?" is not a correct, because this is not how the things works. You create the model class in the first place and then you add data to the database in the way you have structured your helper class.
So the packs
property from your document si actually a Map
. To print those value out please change this line of code:
Log.d(TAG, "onEvent: packs"+productModel.getPacksList()); //not work
with
Map<String, Object> packs = (Map<String, Object>) documentSnapshot.get("packs");
Map<String, Object> zero = (Map<String, Object>) packs.get("0");
String price = (String) zero.get("price");
String subcode = (String) zero.get("subcode");
String weight = (String) zero.get("price");
Log.d(TAG, price + " / " + subcode + " / " + weight);
Your output will be: 12 / s1 / 1
Edit:
Looking again at your model class, the problem in your code is the getter. So if you want to use:
productModel.getPacksList();
Please change the following getter:
public List<Packs> getPacksList() {
return packs;
}
to
public List<Packs> getPacks() {
return packs;
}
And then simply call:
productModel.getPacks();
That's it!
I see in your Firestore response that the packs
object is a List
and not an array. I also see the same thing in your model class, List<Packs> packs
.
The main idea is not to create a model class according with the data that you already have in your database. So a question such "How I can create model for this response?" is not a correct, because this is not how the things works. You create the model class in the first place and then you add data to the database in the way you have structured your helper class.
So the packs
property from your document si actually a Map
. To print those value out please change this line of code:
Log.d(TAG, "onEvent: packs"+productModel.getPacksList()); //not work
with
Map<String, Object> packs = (Map<String, Object>) documentSnapshot.get("packs");
Map<String, Object> zero = (Map<String, Object>) packs.get("0");
String price = (String) zero.get("price");
String subcode = (String) zero.get("subcode");
String weight = (String) zero.get("price");
Log.d(TAG, price + " / " + subcode + " / " + weight);
Your output will be: 12 / s1 / 1
Edit:
Looking again at your model class, the problem in your code is the getter. So if you want to use:
productModel.getPacksList();
Please change the following getter:
public List<Packs> getPacksList() {
return packs;
}
to
public List<Packs> getPacks() {
return packs;
}
And then simply call:
productModel.getPacks();
That's it!
edited yesterday
answered Feb 3 at 8:15
Alex Mamo
39.2k72758
39.2k72758
How i can parse this response: {packs=[{subcode=s1, weight=1, price=12}], name=abc, desc=good, code=001} , please provide some code so that I can understand what you want to say
– vikas
Feb 3 at 9:01
As I see in your image, isnull
. You cannot parse somethig that has the value ofnull
.
– Alex Mamo
Feb 3 at 9:03
That is my question, why its comming null, because packs have some data in it, you can check here, packs have data such as subcode, weight and price, Does my model is wrong? ...{packs=[{subcode=s1, weight=1, price=12}], name=abc, desc=good, code=001}
– vikas
Feb 3 at 9:23
Are getting the other values correct? Please add a more details database structure.
– Alex Mamo
Feb 3 at 9:24
I have added my firestore structure and android studio log image
– vikas
Feb 3 at 9:46
|
show 3 more comments
How i can parse this response: {packs=[{subcode=s1, weight=1, price=12}], name=abc, desc=good, code=001} , please provide some code so that I can understand what you want to say
– vikas
Feb 3 at 9:01
As I see in your image, isnull
. You cannot parse somethig that has the value ofnull
.
– Alex Mamo
Feb 3 at 9:03
That is my question, why its comming null, because packs have some data in it, you can check here, packs have data such as subcode, weight and price, Does my model is wrong? ...{packs=[{subcode=s1, weight=1, price=12}], name=abc, desc=good, code=001}
– vikas
Feb 3 at 9:23
Are getting the other values correct? Please add a more details database structure.
– Alex Mamo
Feb 3 at 9:24
I have added my firestore structure and android studio log image
– vikas
Feb 3 at 9:46
How i can parse this response: {packs=[{subcode=s1, weight=1, price=12}], name=abc, desc=good, code=001} , please provide some code so that I can understand what you want to say
– vikas
Feb 3 at 9:01
How i can parse this response: {packs=[{subcode=s1, weight=1, price=12}], name=abc, desc=good, code=001} , please provide some code so that I can understand what you want to say
– vikas
Feb 3 at 9:01
As I see in your image, is
null
. You cannot parse somethig that has the value of null
.– Alex Mamo
Feb 3 at 9:03
As I see in your image, is
null
. You cannot parse somethig that has the value of null
.– Alex Mamo
Feb 3 at 9:03
That is my question, why its comming null, because packs have some data in it, you can check here, packs have data such as subcode, weight and price, Does my model is wrong? ...{packs=[{subcode=s1, weight=1, price=12}], name=abc, desc=good, code=001}
– vikas
Feb 3 at 9:23
That is my question, why its comming null, because packs have some data in it, you can check here, packs have data such as subcode, weight and price, Does my model is wrong? ...{packs=[{subcode=s1, weight=1, price=12}], name=abc, desc=good, code=001}
– vikas
Feb 3 at 9:23
Are getting the other values correct? Please add a more details database structure.
– Alex Mamo
Feb 3 at 9:24
Are getting the other values correct? Please add a more details database structure.
– Alex Mamo
Feb 3 at 9:24
I have added my firestore structure and android studio log image
– vikas
Feb 3 at 9:46
I have added my firestore structure and android studio log image
– vikas
Feb 3 at 9:46
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48594059%2ffirestore-fetch-array-from-collection%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what problem are you facing?
– Mohammed Farhan
Feb 3 at 5:02
packs is a array which returns null, I have also share my debug results screenshot . please guides me..thank you
– vikas
Feb 3 at 5:07
change
List<Packs> packs
toMap<String,Packs> mapPacks
and to obtain field value from pack usemapPacks.getObject("subcode");
like this– Mohammed Farhan
Feb 3 at 5:25
getmapPacks return null
– vikas
Feb 3 at 8:05