I'm trying to update the identity value of the user with onClick method of the update button but it is not working and no value is being updated.
Here is my activity (I used the Query to get to the user unique ID):
public class Update extends AppCompatActivity {
EditText identity;
DatabaseReference ref;
Button update;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check);
update = (Button) findViewById(R.id.button_update);
identity = (EditText) findViewById(R.id.edit_text_identity);
mStorageRef = FirebaseStorage.getInstance().getReference("uploads");
String key = getIntent().getExtras().get("androidId").toString();
final Query userQuery = FirebaseDatabase.getInstance().getReference().child("Users").orderByChild("androidId");
userQuery.equalTo(key).addListenerForSingleValueEvent(
new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot foodSnapshot: dataSnapshot.getChildren()) {
final String userUID = foodSnapshot.getKey();
ref = FirebaseDatabase.getInstance().getReference().child(userUID);
identity.setText(getIntent().getStringExtra("identity"));
update.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ref.child("identity").setValue("noo");
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
}
);
}
}
My database structure is like:
{
"Users" : {
"uid1" : {
"name" : "Josh",
"identity": "yes"
},
"uid2" : {
"name" : "June",
"identity": "no"
}
}
}
And there is no error showing in the logcat. What might be the problem here?
The problem here is that the ref you've used is not pointing exactly to the node you want to be updated. You can use this to do so:
update.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("Users").child(uidYouWant);
rootRef.child("identity").setValue("noo");
}
});
Also to get the uidYouWant instead of using query you can go though all the nodes to get the one you want, using orderByChild() and equalTo() directly like this:
ref.orderByChild("androidID").equalTo(key).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot data: dataSnapshot.getChildren()){
// do what you want to
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Related
I want to replace kids/[id]/kidLimit/[new data]
So what I do is I make a spinner with kidName data, then with the selected item from that spinner I want to replace the data of kidLimit (with the same parent as the selected item from the spinner).
The first thing I do is to find the unique key of the selected item, then go to the kidLimit with that unique key to then use the setValue() method.
public class setLimit extends AppCompatActivity {
private DatabaseReference db;
private EditText etLimit;
private Button btnSetLimit;
private Spinner kidSpinner;
private String kidKey;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_limit);
etLimit = findViewById(R.id.et_limit);
btnSetLimit = findViewById(R.id.btn_confirm);
kidSpinner = findViewById(R.id.spinner_kid);
//PUTTING STRING LIST FROM FIREBASE TO SPINNER DROPDOWN STARTS HERE
db = FirebaseDatabase.getInstance().getReference().child("kids");
db.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
final List<String> kid = new ArrayList<>();
for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren()) {
String kidName = dataSnapshot1.child("kidName").getValue(String.class);
kid.add(kidName);
}
ArrayAdapter<String> kidNameAdapter = new ArrayAdapter<>(setLimit.this, android.R.layout.simple_spinner_item, kid);
kidNameAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
kidSpinner.setAdapter(kidNameAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
//ENDS HERE
//ONCLICKLISTENER
btnSetLimit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
findId();
String newLimit = etLimit.getText().toString().trim();
db.child(kidKey).child("kidLimit").setValue(newLimit);
}
});
}
//FINDING KEY FROM THE SELECTED SPINNER ITEM
public void findId(){
String kidName = kidSpinner.getSelectedItem().toString();
db = FirebaseDatabase.getInstance().getReference().child("kids");
db.orderByChild("kidName").equalTo(kidName).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){
kidKey = dataSnapshot1.getKey();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
So basically what I did was using the snapshot inside the onclick method to find the key. But when I check them on the log, it showed that the kidkey variable is null the first time I press the button, but after that it's not null anymore.
What can I to do so when I press the button, I can go to a specific child to replace it's data without getting any null pointer exception?
Here's the database that I use
When you do this
btnSetLimit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
findId();
String newLimit = etLimit.getText().toString().trim();
db.child(kidKey).child("kidLimit").setValue(newLimit);
}
});
findId(); is executed but you don't know when it will finish, so after that method is executed and waiting for data, this line
db.child(kidKey).child("kidLimit").setValue(newLimit);
will have kidKey with a null value because it has not been pulled yet from the database, so instead, you should move your code to the onDataChange() or make a new callback when all the asynchronous process finishes
btnSetLimit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
findId();
}
});
public void findId(){
String kidName = kidSpinner.getSelectedItem().toString();
String newLimit = etLimit.getText().toString().trim();
db = FirebaseDatabase.getInstance().getReference().child("kids");
db.orderByChild("kidName").equalTo(kidName).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){
kidKey = dataSnapshot1.getKey();
}
db.child(kidKey).child("kidLimit").setValue(newLimit);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
I am making a to-do app, indeed my first firebase app. I have successfully implemented everything with a single text view(todo) under the firebase UID, by creating an array of the push id that returns the push ID when an element is clicked in the List view.
Here was my previous data structure:
Old data Structure
I am stuck with the problem that how can I get the push IDs of elements so that I can reach them and display them in my ListView.
This is the structure that I am trying to fetch :
New data structure
Here is the code that I use for adding and deleting data in the old
Structure
public void Upload(View v) {
String userId = myRootRef.push().getKey();
firebaseUniqueID =mAuth.getCurrentUser().getUid();
todoToUpload=todoEditText.getText().toString();
myRootRef.child(firebaseUniqueID).child(userId).setValue(todoToUpload);
startActivity(new Intent(AddTodo.this,showTodo.class));
Here's the code I use for adding and deleting:
FirebaseAuth mAuth;
FirebaseDatabase mDataBase= FirebaseDatabase.getInstance();
DatabaseReference myRootRef=mDataBase.getReference();
DatabaseReference userRef;
ListView listView;int pos;
private ArrayAdapter<String> adapter;
ArrayList<String> mTodo=new ArrayList<>();
ArrayList<String> keysList = new ArrayList<>();
adapter=new ArrayAdapter<String>(this,R.layout.custom_list_layout,R.id.tood,mTodo);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
pos=position;
AlertDialog.Builder mBuilder=new AlertDialog.Builder(showTodo.this);
View mView=getLayoutInflater().inflate(R.layout.custom_dialog_show,null);
Todo=mView.findViewById(R.id.todo);
Delete=mView.findViewById(R.id.delete);
Done=mView.findViewById(R.id.done);
closer=mView.findViewById(R.id.dialog_close);
mBuilder.setView(mView);
final AlertDialog dailog=mBuilder.create();
myRootRef.child(firebaseUniqueID).child(keysList.get(position)).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
todoOpened=dataSnapshot.getValue(String.class);
Todo.setText(todoOpened);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(Done.isChecked())
{
mTodo.remove(pos);
adapter.notifyDataSetChanged();
//new code below
myRootRef.getRoot().child(firebaseUniqueID).child(keysList.get(pos)).removeValue();
keysList.remove(pos);
dailog.dismiss();
}
}
});
myRootRef.child(firebaseUniqueID)
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot firebaseUniqueID : dataSnapshot.getChildren()) {
keysList.add(firebaseUniqueID.getKey());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
/*handle errors*/
}
});
userRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value= dataSnapshot.getValue(String.class);
mTodo.add(value);
adapter.notifyDataSetChanged();
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Here's the JSON file for the old and new structures
{
"Z2H2ZkX56fYv3WKxE3a3nCaa8Q63" : {
"-LNPyZIkX8-uDDGeX-pN" : {
"Time" : "22:56",
"Todo" : "play"
},
"-LNPze7ppG1L0qFukZO-" : {
"Priority" : "High",
"Time" : "22:56",
"TimeSet" : "09:56",
"Todo" : "play till the sun goes down"
},
"-LNPzhBDDtHv_XSIERX_" : {
"Priority" : "High",
"Time" : "22:56",
"TimeSet" : "09:56",
"Todo" : "play till the sun goes ddawdawdawdwown"
}
}
}
{
"Z2H2ZkX56fYv3WKxE3a3nCaa8Q63" : {
"-LNQRCZSsYVPqn0tWghZ" : "play in the evening",
"-LNQRrLliVSxWBHcEIrv" : "I want to shine like a sun"
}
}
You get the key of a DataSnapshot by calling its getKey() method. So for example:
public void onDataChange(DataSnapshot dataSnapshot) {
todoOpened=dataSnapshot.getValue(String.class);
String key = dataSnapshot.getKey();
Todo.setText(key + ": "+ todoOpened);
}
you can display list as per your new structure dsplayed in :https://i.stack.imgur.com/E4DXI.png
FirebaseRecyclerOptions<Messages> options =
new FirebaseRecyclerOptions.Builder<"Your Class Name">()
.setQuery(mMessageThread.child("messages").limitToLast(500), "Your Class Name".class)
.setLifecycleOwner(this)
.build();
use this with firebase adapter.
Then get ToDo text and display it.
I was trying to retrieve the data from user and time. But I can't access the data, it's not really accessing the database.
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_balance);
mFirebaseDatabase=FirebaseDatabase.getInstance();
cashRef=mFirebaseDatabase.getReference().child("ASSETS").child("cash_at_bank");
TextView view=(TextView)findViewById(R.id.view);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
cashRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
int time=dataSnapshot.getValue(int.class);
int values =dataSnapshot.getValue(int.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
To get the time, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference cashAtBankRef = rootRef.child("ASSETS").child("Cash at bank");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Long time = ds.child("time").child("time").getValue(Long.class);
Log.d("TAG", time + "");
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
cashAtBankRef.addListenerForSingleValueEvent(eventListener);
The output will be:
150555043995
//and so on
I have data in database:
database in firebase
I want read all child (Belgia, Czechy, Polska...) and display it in Text Field, but AFTER click the button (I don't change data in database).
The button have assigned below function:
public void f_btn_zaczytaj_dane(View view) {
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String value = dataSnapshot.getValue(String.class);
TextView txt_opis_panstwa = (TextView) findViewById(R.id.txt_opis_panstwa);
txt_opis_panstwa.setText(value);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Befrore i write in code:
FirebaseDatabase database;
DatabaseReference myRef;
database = FirebaseDatabase.getInstance();
myRef = database.getReference("kraj");
Unfortunately, when I press the button nothing happens.
I will be grateful for the help
Regards, Marcin
You have to iterate through your children
public void f_btn_zaczytaj_dane(View view) {
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
TextView txt_opis_panstwa = (TextView) findViewById(R.id.txt_opis_panstwa);
for (DataSnapshot country : dataSnapshot.getChildren()) {
txt_opis_panstwa.append(country.getKey());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Please use this code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference krajRef = rootRef.child("flagiistolice").child("kraj");
public void f_btn_zaczytaj_dane(View view) {
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
TextView txt_opis_panstwa = (TextView) findViewById(R.id.txt_opis_panstwa);
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String country = ds.getkey();
txt_opis_panstwa.append(country);
Log.d("TAG", country);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
krajRef.addListenerForSingleValueEvent(eventListener);
}
I am trying to populate a TextView from a firebase database. Here is the sample json file.
{
"Player" : {
"Club" : "Valley Rovers",
"Name" : "John Murphy"
}
}
Here is my android code:
public class MainActivity extends AppCompatActivity {
private TextView mPlayer;
private DatabaseReference mDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPlayer = (TextView) findViewById(R.id.player);;
mDatabase = FirebaseDatabase.getInstance().getReference("Player").child("Name");
final String player = mDatabase.push().getKey();
mDatabase.child("Name").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Check for null
if (player == null) {
Log.e(TAG, "User data is null!");
return;
}
mPlayer.setText(player);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
I want to add the name John Murphy to the firebase database and doing so the TextView mPlayer will populate with John Murphy.
You are getting the child "Name" twice on your code (leading to Player/Name/Name). Remove it from the mDatabase initialization:
mDatabase = FirebaseDatabase.getInstance().getReference("Player");
And you're never really getting the value from the DataSnapshot received. To do so, use this:
mDatabase.child("Name").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String playerName = dataSnapshot.getValue(String.class);
mPlayer.setText(playerName);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
In case someone wants to use POJO approach to this problem in future, follow the example below:
Remove the child("Name") from mDatabase.child("Name").addValueEventListener(new ValueEventListener()
unless you want to fetch the "Name" child only. Also remember to effect the correction made on mDatabase initialization.
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// String playerName = dataSnapshot.getValue(String.class);
PlayerModel playerModel = dataSnapshot.getValue(PlayerModel.class);
((TextView)findViewById(R.id.textviewName)).setText(playerModel.getName());
((TextView)findViewById(R.id.textviewClub)).setText(playerModel.getClub());
// mPlayer.setText(playerName);
}