69. Firebase Database

First ! You need login into your Firebase and creat new Project 。Creat android project .

Second, copy Android app package name add it into your Firebase Project.

Third ,Add some library to working with Firebase. classpath "com.google.gms:google-services:3.0.0" in build gradle

Fourth , Add library in app build.grade, compile 'com.android.support.design:25.0.0"

    compile "com.google.firebase:firebase-database:9.6.1"

    compile  "com.android.support.test.espresso:espresso-core:2.2.2"

Fifth To get SHA1 string ,just use my way ,very simple, Select Grade ,and double click sgrinReport.Paste your SHA1 string and press "Add App"

Six , Download google-services.json and copy-paste it into your android project. Android project ->app->pasete。

Seven, add this line to apply plugin at the end of app buid.gradle, apply plugin:"com.google.gms.google-services",if you have error like me ,just add API key .

Eight ,add some drawable using for our project. 

Nine , creat folder named menu ,and creat menu.xml file in this folder , We will using Toolbar for replace ActionBar ,so set Theme NOACTIONBAR,

Ten , Add ToolBar. <android.support.v7.widget.Toolbar

    id, minHeight = "?attr/actionBarSize" background = "?attr/color"

    app:titleTextColor = "?android:color/white"

Eleven ,set Toolbar  Toolbar toolbar = (Toolbar )findviewbyid (R.id.toolbar);

  toolbar.setTitle("Firebase Demo");setSupportActionBar(toolbar);

   />

Twelve ,override onCreatOptionsMenu,get menuinflater().inflate(R.menu.menu_main,menu);

  return super.onCreatOptionsMenu(menu);   and after this session ,we have done setup Toolbar,

Thirteen, creat listview_item.xml layout file ,.

Fourteen, add andoid.support.design.widget.TextInputLayout

Fifteen ,Custom color for progerss Bar,<style name = "CircularProgerss" parent = "Theme.AppCompat.light"

Sixteen ,Creat Model User , uid = UUID => primary Key and this key is to get data from Firebase

Seventeen ,public class User{

  private String uid,name,email;

  public User(){}

  public User(String uid, String name, String email){

  this.uid = uid;this.name = name ;this.email = email;

  }

  getter & setter 

  }

Eighteen , Creat ListViewAdapter extends BaseAdapter

  @override

  public View getView(int i,View view,ViewGroup viewGroup){

  inflater = (LayoutInflater)activity.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERCRVICE);

  view itemView = inflater.inflate(R.layout.listview_item,null);

  TextView txtUser = (TextView

  }

Nineteen ,This function to add listener event to our Firebass DB, if have any changes from Firebase(Creat/Update/Delete)it will automaatic update our ListvIew

we will store all information about User into node "users"

mDatabaseReference.child("users").addValueEventListener(new ValueEventListener(){

  @override

  public void onDataChange(DataSnapshot dataSnapshot){

  //Delete all old item in ListView when we get new data from Firebase  

  if(list_users.size() > 0) list.users.clear();

  for(DataSnapshot postSnapshot:dataSnapshot.getChildren()){

    User user = postSnapShot.getValue(User.class);

    list_users.add(user);

  }

  }

  }

Twenty ,Process event for button "Add" ,"Update", "Delete" ;

User user = new User(UUID.randomUUID().toString().input_name.getText().toString(),input_email.getText().toString());

mDatabaseReference.child("users").child(user.getUid()).setValue(user);

Twenty-One , We need local variable to store Usaer data when we click item in ListView,this variable will let us known what item we selected

private User selectedUser; hold user when we selected. so that why we have variable "selected user", beacuse when you update or delete information ,you need known key of data.

Twenty-two ,We need change rules ,beacuse our app doesn't authentication so we just change to "true"

Twenty_three, add OnItemClickEventListener to set selected user when we click on item

posted on 2017-09-22 10:51  L1nus  阅读(173)  评论(0)    收藏  举报

导航