Created
July 25, 2017 21:58
-
-
Save Bahaaib/f613f1f312bf5c2fc08518a8435fd010 to your computer and use it in GitHub Desktop.
Firebase Read /Write simple data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MainActivity extends AppCompatActivity { | |
| public Button mSendButton; | |
| public DatabaseReference mDatabase; | |
| public HashMap<String, String> dataMap; | |
| public TextView retData; | |
| public String name; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | |
| setSupportActionBar(toolbar); | |
| mDatabase = FirebaseDatabase.getInstance().getReference().child("Bahaa"); | |
| mSendButton = (Button)findViewById(R.id.Send); | |
| dataMap= new HashMap<>(); | |
| dataMap.put("Username", "Bahaa Ibrahim"); | |
| dataMap.put("Email", "Bahaasco@gmail.com"); | |
| dataMap.put("Mobile", "01009540399"); | |
| mSendButton.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View view) { | |
| mDatabase.setValue(dataMap).addOnCompleteListener(new OnCompleteListener<Void>() { | |
| @Override | |
| public void onComplete(@NonNull Task<Void> task) { | |
| if(task.isSuccessful()){ | |
| Toast.makeText(MainActivity.this, "Sent !", Toast.LENGTH_LONG).show(); | |
| }else { | |
| Toast.makeText(MainActivity.this, "Failed !", Toast.LENGTH_LONG).show(); | |
| } | |
| } | |
| }); | |
| } | |
| }); | |
| mDatabase = FirebaseDatabase.getInstance().getReference().child("Bahaa").child("Username"); | |
| retData = (TextView)findViewById(R.id.retData); | |
| mDatabase.addValueEventListener(new ValueEventListener() { | |
| @Override | |
| public void onDataChange(DataSnapshot dataSnapshot) { | |
| name = dataSnapshot.getValue().toString(); | |
| retData.setText("Name is: " + name); | |
| } | |
| @Override | |
| public void onCancelled(DatabaseError databaseError) { | |
| Toast.makeText(MainActivity.this, "Failed !", Toast.LENGTH_LONG).show(); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment