Created
July 30, 2017 06:12
-
-
Save Bahaaib/d71fe7372629ef7b1326c57409695e8f to your computer and use it in GitHub Desktop.
Retrieve multipe data from Firebase Database
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
| package com.example.bahaaibrahim.firebasetest; | |
| import android.content.Context; | |
| import android.os.Bundle; | |
| import android.support.annotation.NonNull; | |
| import android.support.design.widget.FloatingActionButton; | |
| import android.support.design.widget.Snackbar; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.support.v7.widget.Toolbar; | |
| import android.view.View; | |
| import android.view.Menu; | |
| import android.view.MenuItem; | |
| import android.widget.ArrayAdapter; | |
| import android.widget.Button; | |
| import android.widget.ListView; | |
| import android.widget.TextView; | |
| import android.widget.Toast; | |
| import com.google.android.gms.tasks.OnCompleteListener; | |
| import com.google.android.gms.tasks.Task; | |
| import com.google.firebase.database.ChildEventListener; | |
| import com.google.firebase.database.DataSnapshot; | |
| import com.google.firebase.database.DatabaseError; | |
| import com.google.firebase.database.DatabaseReference; | |
| import com.google.firebase.database.FirebaseDatabase; | |
| import com.google.firebase.database.ValueEventListener; | |
| import java.util.ArrayList; | |
| import java.util.HashMap; | |
| public class MainActivity extends AppCompatActivity { | |
| public DatabaseReference mDatabase; | |
| public ListView dataList; | |
| public ArrayList<String> dataArray; | |
| public ArrayList<String> mKeys; | |
| public ArrayAdapter<String> mAdapter; | |
| public String result; | |
| public String key; | |
| @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(); | |
| dataList = (ListView) findViewById(R.id.dataList); | |
| dataArray = new ArrayList<>(); | |
| mKeys = new ArrayList<>(); | |
| mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dataArray); | |
| dataList.setAdapter(mAdapter); | |
| mDatabase.addChildEventListener(new ChildEventListener() { | |
| @Override | |
| public void onChildAdded(DataSnapshot dataSnapshot, String s) { | |
| result = dataSnapshot.getValue().toString(); | |
| key = dataSnapshot.getKey(); | |
| dataArray.add(result); | |
| mKeys.add(key); | |
| mAdapter.notifyDataSetChanged(); | |
| Toast.makeText(getApplicationContext(), "Accessed!", Toast.LENGTH_LONG).show(); | |
| } | |
| @Override | |
| public void onChildChanged(DataSnapshot dataSnapshot, String s) { | |
| result = dataSnapshot.getValue().toString(); | |
| key = dataSnapshot.getKey(); | |
| int index = mKeys.indexOf(key); | |
| dataArray.set(index, result); | |
| mAdapter.notifyDataSetChanged(); | |
| } | |
| @Override | |
| public void onChildRemoved(DataSnapshot dataSnapshot) { | |
| result = dataSnapshot.getValue().toString(); | |
| dataArray.remove(result); | |
| mAdapter.notifyDataSetChanged(); | |
| } | |
| @Override | |
| public void onChildMoved(DataSnapshot dataSnapshot, String s) { | |
| } | |
| @Override | |
| public void onCancelled(DatabaseError databaseError) { | |
| Toast.makeText(getApplicationContext(), "Failed !", Toast.LENGTH_LONG).show(); | |
| } | |
| }); | |
| FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); | |
| fab.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View view) { | |
| Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) | |
| .setAction("Action", null).show(); | |
| } | |
| }); | |
| } | |
| @Override | |
| public boolean onCreateOptionsMenu(Menu menu) { | |
| // Inflate the menu; this adds items to the action bar if it is present. | |
| getMenuInflater().inflate(R.menu.menu_main, menu); | |
| return true; | |
| } | |
| @Override | |
| public boolean onOptionsItemSelected(MenuItem item) { | |
| // Handle action bar item clicks here. The action bar will | |
| // automatically handle clicks on the Home/Up button, so long | |
| // as you specify a parent activity in AndroidManifest.xml. | |
| int id = item.getItemId(); | |
| //noinspection SimplifiableIfStatement | |
| if (id == R.id.action_settings) { | |
| return true; | |
| } | |
| return super.onOptionsItemSelected(item); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment