Reputation: 9
I am trying to build a Task app that will update everyone who has the app, I want to have some buttons, that each button is another task, for now I have 2, I want whenever someone presses the task, the task has "points" to give to the certain user who clicked it, the button will be updated to everyone as DONE, and the name of the button will be changed to ButtonName+UserClicked+TimeClicked. I am having trouble getting the points value from the JSON file and updating the user with the points, Here is my JSON file:
{
"Tasks":{
"Room1":{
"DoTime":"",
"ID":2,
"MadeBy":"",
"Points":150,
"isActive":1
},
"Room2":{
"DoTime":"",
"ID":1,
"MadeBy":"",
"Points":100,
"isActive":1
}
},
"Users":{
"L18uheV3GVbmJ8GmVvoCOmU3e1E2":{
"Email":"[email protected]",
"Points":0,
"UserName":"Name1",
"isAdmin":0
},
"tcAOO7dmXEgADBqRwTTlUfWIH803":{
"Email":"[email protected]",
"Points":0,
"UserName":"Name2",
"isAdmin":0
}
},
"update":2
}
Here is my code:
String displayName = " ";
Object TaskingPoints = 0;
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference RefUser = database.getReference("Users").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
DatabaseReference RefTask = database.getReference("Tasks");
public void button_function(View Button){
final String ButtonTaskName = Button.getResources().getResourceEntryName(Button.getId());
final Button b = (Button) Button;
DatabaseReference givePoints = database.getReference(ButtonTaskName).child("Points");
DatabaseReference ClickUser = (DatabaseReference) RefUser.child("UserName");
final CharSequence ClickTime = DateFormat.format("dd-MM-yyy (HH:mm)" , new Date().getTime());
RefUser.child("UserName").addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
displayName = dataSnapshot.getValue().toString();
b.setText(b.getText() + " (" + displayName + ") " + ClickTime);
RefTask.child(ButtonTaskName).child("DoTime").setValue(ClickTime);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
RefTask.child(ButtonTaskName).child("Points").addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
TaskingPoints = dataSnapshot.getValue();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
RefTask.child(ButtonTaskName).child("isActive").setValue(0);
RefUser.child("Points").setValue(TaskingPoints);
RefTask.child(ButtonTaskName).child("MadeBy").setValue(displayName);
b.getBackground();
b.setEnabled(false);
b.setBackgroundResource(R.color.myGreen);
}
Alot is WORKING, But the problem is: whenever I run the app and the first button I click, the points for the user is not updated and the MadeBy key in the task is not updated, BUT when I click another button, everything is update, but by the first button I click, Please teach me, what I am doing wrong. P.S: The buttons call this function when clicked.
Upvotes: 0
Views: 110
Reputation: 4035
Took a while to understand your code, you should have explained it more. Anyway the problem in your code is that you attach the listener when the button is clicked, this is why on the next button click everything works.
To solve this problem you should add these outside the button_function:
RefUser.child("UserName").addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
displayName = dataSnapshot.getValue().toString();
b.setText(b.getText() + " (" + displayName + ") " + ClickTime);
RefTask.child(ButtonTaskName).child("DoTime").setValue(ClickTime);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
RefTask.child(ButtonTaskName).child("Points").addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
TaskingPoints = dataSnapshot.getValue();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
I suggest you add them in the onCreate method.
Upvotes: 1