firebase accessing child and grandchild nodes android - android

solved
Android - Firebase - Getting Child of Child Data
how to view leaf nodes both children and grandchildren
E/data: {6640={first={sub3=dbms, m5=3, m2=2, sub2=dl, sub1=cis, m3=2, sub4=socio, m4=2, sub5=english, m1=4}}}
firebase data structure
the values i want are
the children inside 6640 eg : first and second
the grandchildren inside first eg : sub1--sub5 and m1-m5
the data above is a snapshot data snapshot from a query.. how do i access each values from the following snapshot.. i created a data module and used a for each loop like this
for (DataSnapshot post : dataSnapshot.getChildren())
{
trial_module m2 = post.getValue(trial_module.class);
Log.e("data",dataSnapshot.getValue().toString());
data2.add(m2);
}
do i need to create mutiple classes to hold data?
public class Result_module {
String sub1,sub2,sub3,sub4,sub5;
long m1,m2,m3,m4,m5;
public String getSub1() {
return sub1;
}
public void setSub1(String sub1) {
this.sub1 = sub1;
}
public String getSub2() {
return sub2;
}
public void setSub2(String sub2) {
this.sub2 = sub2;
}
public String getSub3() {
return sub3;
}
public void setSub3(String sub3) {
this.sub3 = sub3;
}
public String getSub4() {
return sub4;
}
public void setSub4(String sub4) {
this.sub4 = sub4;
}
public String getSub5() {
return sub5;
}
public void setSub5(String sub5) {
this.sub5 = sub5;
}
public long getM1() {
return m1;
}
public void setM1(long m1) {
this.m1 = m1;
}
public long getM2() {
return m2;
}
public void setM2(long m2) {
this.m2 = m2;
}
public long getM3() {
return m3;
}
public void setM3(long m3) {
this.m3 = m3;
}
public long getM4() {
return m4;
}
public void setM4(long m4) {
this.m4 = m4;
}
public long getM5() {
return m5;
}
public void setM5(long m5) {
this.m5 = m5;
}
}

Related

Retrofit response return " NumberFormatexception : Empty string"

i know actually a lot of these kinds of questions, and i have searched on google but i can't find where the error is in my code.
I tried to send a request to fetch data, but it runs the onFailure method which reads "NumberFormatException: Empty string" and the data can't be displayed in the recyclerview, even though I'm also getting a json response as I wanted.
i got this response:
{
"kode":1,
"pesan":"Barang ditemukan",
"data":[
{
"Sat_1":"PT",
"Sat_2":"",
"Isi_2":"",
"KdBrg":"280349191",
"NmBrg":"SILICONE PACIFIER STEP 1A",
"Stock_Akhir":"0",
"Hrg":28000
}
]
}
my object model
public class ModelDataBarang {
private int Isi_2;
private int Isi_3;
private int Isi_4;
private String KdBrg;
private String NmBrg;
private String Sat_1;
private String Sat_2;
private String Sat_3;
private String Sat_4;
private String KdHrgList;
private double Stock_Akhir;
private double HrgJl11;
public double getHrgJl11() {
return HrgJl11;
}
public void setHrgJl11(double hrgJl11) {
HrgJl11 = hrgJl11;
}
private String Hrg;
public String getHrg() {
return Hrg;
}
public void setHrg(String hrg) {
Hrg = hrg;
}
public String getKdHrgList() {
return KdHrgList;
}
public void setKdHrgList(String kdHrgList) {
KdHrgList = kdHrgList;
}
public String getSat_3() {
return this.Sat_3;
}
public void setSat_3(String sat_3) {
this.Sat_3 = sat_3;
}
public String getSat_4() {
return this.Sat_4;
}
public void setSat_4(String sat_4) {
this.Sat_4 = sat_4;
}
public int getIsi_3() {
return this.Isi_3;
}
public void setIsi_3(int isi_3) {
this.Isi_3 = isi_3;
}
public int getIsi_4() {
return this.Isi_4;
}
public void setIsi_4(int isi_4) {
this.Isi_4 = isi_4;
}
public String getKdBrg() {
return this.KdBrg;
}
public void setKdBrg(String kdBrg) {
this.KdBrg = kdBrg;
}
public String getNmBrg() {
return this.NmBrg;
}
public void setNmBrg(String nmBrg) {
this.NmBrg = nmBrg;
}
public String getSat_1() {
return this.Sat_1;
}
public void setSat_1(String sat_1) {
this.Sat_1 = sat_1;
}
public String getSat_2() {
return this.Sat_2;
}
public void setSat_2(String sat_2) {
this.Sat_2 = sat_2;
}
public int getIsi_2() {
return this.Isi_2;
}
public void setIsi_2(int isi_2) {
this.Isi_2 = isi_2;
}
public double getStock_Akhir() {
return this.Stock_Akhir;
}
public void setStock_Akhir(double stock_Akhir) {
this.Stock_Akhir = stock_Akhir;
}
public String toString() {
return this.NmBrg;
}
}
my recyclerview data holder
ModelDataBarang modelBarangResto= listModel.get(position);
holder.tvKdBrg.setText(modelBarangResto.getKdBrg());
holder.tvNmBarang.setText(modelBarangResto.getNmBrg());
holder.tvHrgBrg.setText(String.valueOf(modelBarangResto.getHrg()));
all answers i will appreciate
Your model class values are not defined as per your response. Try to check and modify respective return type. You are getting this error because you have defined Isi_2 as int in your model class but in response you are getting empty string.
Replace this
private int Isi_2;
With this
private String Isi_2;

Trying to add relations in Backendless. No error but total count of relations added is always 0

Hi guys I am trying to save objects with relations in Backendless via API. I have two classes namely Task and Reminder. A task can be associated with many reminders hence I want a 1:N relationship between the Task table and Reminder table in Backendless. My Task class is as follows:
public class Task {
public Date created;
public Date updated;
private List<Reminder> reminders = null;
private String ownerId;
#PrimaryKey
#NonNull
private String objectId;
#NonNull
private String taskTitle;
#NonNull
private Date deadline;
#NonNull
private int isCompleted = 0;
#NonNull
private int isExpired = 0;
public String getOwnerId() {
return ownerId;
}
public void setOwnerId(String ownerId) {
this.ownerId = ownerId;
}
#NonNull
public String getObjectId() {
return objectId;
}
public void setObjectId(#NonNull String objectId) {
this.objectId = objectId;
}
public List<Reminder> getReminders() {
return reminders;
}
public void setReminders(List<Reminder> reminders) {
this.reminders = reminders;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getUpdated() {
return updated;
}
public void setUpdated(Date updated) {
this.updated = updated;
}
#NonNull
public int getIsCompleted() {
return isCompleted;
}
public void setIsCompleted(#NonNull int isCompleted) {
this.isCompleted = isCompleted;
}
#NonNull
public int getIsExpired() {
return isExpired;
}
public void setIsExpired(#NonNull int isExpired) {
this.isExpired = isExpired;
}
public String getTaskTitle() {
return taskTitle;
}
public void setTaskTitle(String taskTitle) {
this.taskTitle = taskTitle;
}
public Date getDeadline() {
return deadline;
}
public void setDeadline(Date deadline) {
this.deadline = deadline;
}
}
Reminder Class:
public class Reminder {
private String title;
private Date time;
private String objectId;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public String getObjectId() {
return objectId;
}
public void setObjectId(String objectId) {
this.objectId = objectId;
}
}
I am saving the objects and setting up the relation as below:
public void saveTaskToServer(final Task task) {
List<Reminder> remindersList = new ArrayList<>();
remindersList = task.getReminders();
final List<Reminder> savedReminders = new ArrayList<>();
if(remindersList!=null && remindersList.size()!=0) {
for
(Reminder reminder : remindersList) {
reminder.setTitle(task.getTaskTitle());
Backendless.Persistence.save(reminder, new AsyncCallback<Reminder>() {
#Override
public void handleResponse(Reminder response) {
savedReminders.add(response);
}
#Override
public void handleFault(BackendlessFault fault) {
Log.i("error saving remidners", fault.toString());
}
});
}
}
Backendless.Persistence.save(task, new AsyncCallback<Task>() {
#Override
public void handleResponse(Task response) {
newTask = response;
Log.i("id is ", newTask.getObjectId());
insertTask(response);
snackbarMessage.postValue("Task Created Successfully.");
}
#Override
public void handleFault(BackendlessFault fault) {
Log.i("error", fault.getMessage());
}
});
Backendless.Persistence.of(Task.class).addRelation(task, "reminders", savedReminders, new AsyncCallback<Integer>() {
#Override
public void handleResponse(Integer response) {
Log.i("response", "added" + response);
newTask.setReminders(savedReminders);
}
#Override
public void handleFault(BackendlessFault fault) {
Log.i("response", "error" + fault.toString());
}
});
}
I have tried saving the relation using the tablename:Class:n instead of the parentColumnName. Also tried saving the objectids of the reminders instead of the reminder objects themselves.The task and reminder objects get saved properly in the backendless console in their respective tables but the reminder column in the Task table still remains empty and no relations get added. Relations count in the backendless call in Android Studio also returns 0. Any advice is really appreciated. I have been following this example.
My relations were not getting saved because I was using the async callbacks in backendless!! I dont know why I didnt see that before. Since the save calls were being made before the async callbacks could finish I was ending up with null values. Fixed it by making the calls synchronous and wrapping them in an async task.

How to update a particular row in realm table android

I have the following table with a PrimaryKey in it. I have inserted some values in the table. Now I need to update a particular value in a particular row. I have a row with gameType as Puzzle and I need to update the currentLevel in the row. But I am not able to achieve that.
GamesDetails table:
public class GamesDetail extends RealmObject {
#PrimaryKey
private String gameType;
private int currentLevel;
private int totalLevel;
private int totalCoins;
private int currentBadge;
public String getGameType() {
return gameType;
}
public void setGameType(String gameType) {
this.gameType = gameType;
}
public int getCurrentLevel() {
return currentLevel;
}
public void setCurrentLevel(int currentLevel) {
this.currentLevel = currentLevel;
}
public int getTotalLevel() {
return totalLevel;
}
public void setTotalLevel(int totalLevel) {
this.totalLevel = totalLevel;
}
public int getTotalCoins() {
return totalCoins;
}
public void setTotalCoins(int totalCoins) {
this.totalCoins = totalCoins;
}
public int getCurrentBadge() {
return currentBadge;
}
public void setCurrentBadge(int currentBadge) {
this.currentBadge = currentBadge;
}
}
Here is what I have tried to update a particular row in the table:
final GamesDetail puzzleGameDetail = realm.where(GamesDetail.class).equalTo("gameType","Puzzle").findFirst();
final int[] nextLevel = {puzzleGameDetail.getCurrentLevel()};
realm.executeTransactionAsync(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
puzzleGameDetail.setCurrentLevel(++nextLevel[0]);
realm.copyToRealmOrUpdate(puzzleGameDetail);
}
}, new Realm.Transaction.OnSuccess() {
#Override
public void onSuccess() {
Log.e(TAG, "Done");
}
}, new Realm.Transaction.OnError() {
#Override
public void onError(Throwable error) {
Log.e(TAG,error.getMessage());
}
});
But the value is not getting updated and I am getting this following error:
Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
How can I update a particular value in a particular row in the table ?
When calling executeTransactionAsync, the execute block will run in a background thread, any Realm objects access from that thread need to be created/queried on that thread from the Realm instance which is the param of execute.
Move your finding GamesDetail query inside execute block and rest will work fine.

Firebase database error : Found conflicting getters for name: isAccessibilityFocusable

I am getting this error:
Caused by: com.google.firebase.database.DatabaseException: Found conflicting getters for name: isAccessibilityFocusable
while trying to update my User.
The User class is like this
public class User {
#PrimaryKey
String userName;
String groupName;
int totalMoney;
boolean turn;
boolean hapyo;
boolean blind;
#Exclude
TextView textView;
#Exclude
Button bHapdiye,bChale,bShow,bHeram;
public Button getbHapdiye() {
return bHapdiye;
}
public void setbHapdiye(Button bHapdiye) {
this.bHapdiye = bHapdiye;
}
public Button getbChale() {
return bChale;
}
public void setbChale(Button bChale) {
this.bChale = bChale;
}
public Button getbShow() {
return bShow;
}
public void setbShow(Button bShow) {
this.bShow = bShow;
}
public Button getbHeram() {
return bHeram;
}
public void setbHeram(Button bHeram) {
this.bHeram = bHeram;
}
public TextView getTextView() {
return textView;
}
public void setTextView(TextView textView) {
this.textView = textView;
}
public boolean isBlind() {
return blind;
}
public void setBlind(boolean blind) {
this.blind = blind;
}
public boolean isHapyo() {
return hapyo;
}
public void setHapyo(boolean hapyo) {
this.hapyo = hapyo;
}
public boolean isTurn() {
return turn;
}
public void setTurn(boolean turn) {
this.turn = turn;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public int getTotalMoney() {
return totalMoney;
}
public void setTotalMoney(int totalMoney) {
this.totalMoney = totalMoney;
}
}
I am trying to update the User like this
final User user1 = userList.get(0);
user1.setTextView(tvUser1);
user1.setbChale(bChaleP1);
user1.setbHapdiye(bHapdiyeP1);
user1.setbHeram(bHeramP1);
user1.setbShow(bShowP1);
user1.setTurn(true);
setUsersTurn(user1.isTurn(),bHapdiyeP1,bChaleP1,bShowP1,bHeramP1);
setTurnInFirebase(user1);
The setTurnInFirebase method is like this
public void setTurnInFirebase(User user){
myRef.child("users").setValue(user);
}
So when I run the project, I get the above mentioned error. What could be the reason for this. Thankyou
Your user contains a TextView, which is a class that Firebase won't be able to serialize/deserialize.
To allow writing a class to the Firebase Database, make sure that it only contains properties of simple types or objects that you created: so called POJOs - Plain Old Java Objects.
Alternatively you can exclude the non-supported properties (such as the TextView) by annotating them:
#Exclude
public TextView getTextView() {
return textView;
}
#Exclude
public void setTextView(TextView textView) {
this.textView = textView;
}

How to retrieve json response using retrofit in android

I am accessing data from a 3rd party api. It's giving this response.
This is the 3rd party api
I am unable to get response in the textview.
Please suggest me some solution.
Is this the correct way of writing
e.setText(arg0.get(1).getProductBaseInfo()
.getProductAttributes().getTitle().toString());
{
"productInfoList": [{
"productBaseInfo": {
"productIdentifier": {
"productId": "MOBDP6W6MCUWCFGV",
"categoryPaths": {
"categoryPath": [[{
"title": "Mobiles & Accessories"
},
{
"title": "Mobiles"
}]]
}
},
"productAttributes": {
"title": "Sony Xperia C Black",
}
}]
}
public class productInfoList {
private productBaseInfo productBaseInfo;
private productShippingBaseInfo productShippingBaseInfo;
private String offset;
public productBaseInfo getProductBaseInfo() {
return productBaseInfo;
}
public void setProductBaseInfo(productBaseInfo productBaseInfo) {
this.productBaseInfo = productBaseInfo;
}
public productShippingBaseInfo getProductShippingBaseInfo() {
return productShippingBaseInfo;
}
public void setProductShippingBaseInfo(
productShippingBaseInfo productShippingBaseInfo) {
this.productShippingBaseInfo = productShippingBaseInfo;
}
public String getOffset() {
return offset;
}
public void setOffset(String offset) {
this.offset = offset;
}
}
public class productBaseInfo {
private productIdentifier productIdentifier;
private productAttributes productAttributes;
public productIdentifier getProductIdentifier() {
return productIdentifier;
}
public void setProductIdentifier(productIdentifier productIdentifier) {
this.productIdentifier = productIdentifier;
}
public productAttributes getProductAttributes() {
return productAttributes;
}
public void setProductAttributes(productAttributes productAttributes) {
this.productAttributes = productAttributes;
}
}
public class productAttributes {
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
public interface Flipkart_Interface {
#Headers({ "Fk-Affiliate-Token:asdf",
"Fk-Affiliate-Id:weforteafsd1g" })
#GET("/search/json?query=sony&resultCount=10")
void search(Callback<List<productInfoList>> callback);
}
public class MainActivity extends ActionBarActivity {
List<productInfoList> flip = new ArrayList<productInfoList>();
TextView e;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e = (TextView) findViewById(R.id.sear);
String ENDPOINT = "https://affiliate-api.flipkart.net/affiliate";
RestAdapter adapter = new RestAdapter.Builder().setEndpoint(ENDPOINT)
.build();
/*
* productBaseInfo api1 = adapter.create(productBaseInfo.class);
* productInfoList api2 = adapter.create(productInfoList.class);
* productAttributes api3 = adapter.create(productAttributes.class);
*/
Flipkart_Interface api = adapter.create(Flipkart_Interface.class);
api.search(new Callback<List<productInfoList>>() {
#Override
public void success(List<productInfoList> arg0, Response arg1) {
// TODO Auto-generated method stub
e.setText(arg0.get(1).getProductBaseInfo()
.getProductAttributes().getTitle().toString());
}
#Override
public void failure(RetrofitError arg0) {
// TODO Auto-generated method stub
}
});
}
}
I am unable to get response in the textview.
Please suggest me some solution.
Is this the correct way of writing
e.setText(arg0.get(1).getProductBaseInfo()
.getProductAttributes().getTitle().toString());
Looking at the JSON, you have a one element List of productInfoList elements, and you're accessing index 1.
Try accessing index zero, and make sure that your List has data:
#Override
public void success(List<productInfoList> arg0, Response arg1) {
// TODO Auto-generated method stub
Log.d("MyApp", "arg0 size: " + arg0.size());
//make sure the List has data
if (arg0.size() > 0){
//log the value
Log.d("MyApp", "Title Value: " + arg0.get(0).getProductBaseInfo().getProductAttributes().getTitle().toString());
e.setText(arg0.get(0).getProductBaseInfo()
.getProductAttributes().getTitle().toString());
}
}

Categories

Resources