Firebase
Firebase is realtime database hosted on cloud. It is no SQL database, It means data is store in the form of json and synchronised in realtime with every connected client.Firebase also supports offline mode, writing database changes to a local database before sending them off to the firebase server for synchronization with other devices.
When to use :
When the situation comes to save data on a Realtime basis to a server. We have to create server side database(MySQL, Oracle) with programming language(PHP,Java, .Net etc.) and have to create APIs to store and retrieve data from database.
Why we use :
- No need to extra code and easy to implement
- Directly communicate with firebase without any APIs
- Automatically update the database
Creating Firebase Project
1) First got to https://firebase.google.com/ and click "GET STARTED FOR FREE" make an account and create your first project.
2) After creating account it redirects you to firebase console. click on "Add Project". A dialog will open and enter project name and country. click on "Create Project".
3) After creating project, it redirect to firebase console page and click there on "Add firebase to your android app"
1. create a new project in Android studio. File -> New Project use the same package name that you have put in firebase app.
2. Copy the google-services.json file that you have download from firebase console paste it in app folder.
3. Open the build.gradle of project level and add the google dependency
Rules for Firebase Database
Firebase console provide you to set a set of rules to secure the database.
You can set the rules if you want to read and write the data only authorised user.
You can set the rules if you want to read and write the data all the user.
Insert Data in Database
The are two method to insert data to firebase. push() and setValue().
push() - push() method is used to generate unique Id, it creates an empty node with unique key.
setValue() - setValue() method used to inset data as child node in top level JSON. To get the reference of top node you can get by child() method, then you use setValue() method to store the user data.
String id =databasePlayers.push().getKey();
databasePlayers.child(id).setValue(model);
Retrieve the data from Firebase
To retrieve the data from database, you have to add ValueEventListener to database reference. Tis event will triggered when you change the value in realtime database. OnDatachanged() method will call.
Update the data from firebase database
To update the value you have use setValue() method with database reference and child method to find the node and setValue() method add the value in that node.
databasePlayers.child(id).setValue(model);
Delete the data from Firebase database
To delete the value you have to use removeValue() method.
layout/activity_main.xml
layout/player_item_list.xml
layout/player_item_list_header.xml
model/PlayerModel.java
Adapter/PlayerRecyclerAdapter.java
MainActivity.java
2. Copy the google-services.json file that you have download from firebase console paste it in app folder.
3. Open the build.gradle of project level and add the google dependency
dependencies { classpath 'com.android.tools.build:gradle:2.2.0' classpath 'com.google.gms:google-services:3.0.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }
4. Open app->build.gradle and add firebase database dependency.At the very bottom of the file, apply plugin
dependencies { // Adding support library for this demo app compile 'com.android.support:design:24.2.1' compile 'com.google.firebase:firebase-database:9.6.1' } apply plugin: 'com.google.gms.google-services' |
Rules for Firebase Database
Firebase console provide you to set a set of rules to secure the database.
You can set the rules if you want to read and write the data only authorised user.
{
"rules"
: {
".read"
:
"auth != null"
,
".write"
:
"auth != null"
}
}
You can set the rules if you want to read and write the data all the user.
{
"rules": {
".read": true,
".write": true
}
}
Initialize the Firebase database
First you have to initilialize the DatabaseReference reference. This will gives you reference to database JSON top node. From here you can use your child node names.
DatabaseReference databasePlayers; |
databasePlayers = FirebaseDatabase.getInstance().getReference("players"); |
Insert Data in Database
The are two method to insert data to firebase. push() and setValue().
push() - push() method is used to generate unique Id, it creates an empty node with unique key.
setValue() - setValue() method used to inset data as child node in top level JSON. To get the reference of top node you can get by child() method, then you use setValue() method to store the user data.
String id =databasePlayers.push().getKey();
databasePlayers.child(id).setValue(model);
Retrieve the data from Firebase
To retrieve the data from database, you have to add ValueEventListener to database reference. Tis event will triggered when you change the value in realtime database. OnDatachanged() method will call.
databasePlayers.addValueEventListener(new ValueEventListener() { @Override | |
public void onDataChange(DataSnapshot snapshot) { | |
playerlist.clear(); | |
for (DataSnapshot postSnapshot : snapshot.getChildren()) { | |
if(postSnapshot != null) { | |
String parentid = postSnapshot.getKey(); | |
PlayerModel model = postSnapshot.getValue(PlayerModel.class); | |
/////parse the single parameters////////////// | |
int p_id = model.getP_id(); | |
String image = model.getPlayerImage(); | |
String name = model.getPlayer_name(); | |
String match = model.getPlayer_match(); | |
String inning = model.getPlayer_inning(); | |
String run = model.getPlayer_run(); | |
String wicket = model.getPlayer_wicket(); | |
String bestRun = model.getPlayer_bestRun(); | |
String bestBowling = model.getPlayer_bestBowling(); | |
String avg = model.getAverage(); | |
model.setParentid(parentid); | |
playerlist.add(model); | |
adapter = new PlayerRecyclerAdapter(MainActivity.this,playerlist); | |
recyclerView.setAdapter(adapter); | |
} |
Update the data from firebase database
To update the value you have use setValue() method with database reference and child method to find the node and setValue() method add the value in that node.
databasePlayers.child(id).setValue(model);
Delete the data from Firebase database
To delete the value you have to use removeValue() method.
Complete project code
layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/activity_main" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
tools:context="com.androdheight.firebase.MainActivity"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Enter the Player details" | |
/> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<android.support.design.widget.TextInputLayout | |
android:id="@+id/playername" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1"> | |
<EditText | |
android:id="@+id/etplayername" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Player Name" /> | |
</android.support.design.widget.TextInputLayout> | |
<android.support.design.widget.TextInputLayout | |
android:id="@+id/playermatch" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1"> | |
<EditText | |
android:id="@+id/etplayermatch" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Match" /> | |
</android.support.design.widget.TextInputLayout> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<android.support.design.widget.TextInputLayout | |
android:id="@+id/playerinning" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1"> | |
<EditText | |
android:id="@+id/etplayerinning" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Inning" /> | |
</android.support.design.widget.TextInputLayout> | |
<android.support.design.widget.TextInputLayout | |
android:id="@+id/playerrun" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1"> | |
<EditText | |
android:id="@+id/etplayerrun" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Run" /> | |
</android.support.design.widget.TextInputLayout> | |
<android.support.design.widget.TextInputLayout | |
android:id="@+id/playerwicket" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1"> | |
<EditText | |
android:id="@+id/etplayerwicket" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Wicket" /> | |
</android.support.design.widget.TextInputLayout> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<android.support.design.widget.TextInputLayout | |
android:id="@+id/playerBestRun" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1"> | |
<EditText | |
android:id="@+id/etplayerBestRun" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Best Run" /> | |
</android.support.design.widget.TextInputLayout> | |
<android.support.design.widget.TextInputLayout | |
android:id="@+id/playerbestbowling" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1"> | |
<EditText | |
android:id="@+id/etplayerbestbowling" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Best Bowling" /> | |
</android.support.design.widget.TextInputLayout> | |
<android.support.design.widget.TextInputLayout | |
android:id="@+id/playerAvg" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1"> | |
<EditText | |
android:id="@+id/etplayerAvg" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Average" /> | |
</android.support.design.widget.TextInputLayout> | |
</LinearLayout> | |
<Button | |
android:id="@+id/btnSubmit" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:text="Submit"/> | |
</LinearLayout> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/rvPlayerlist" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> | |
</LinearLayout> |
layout/player_item_list.xml
<TableLayout android:stretchColumns="*" android:layout_height="wrap_content" | |
android:layout_width="fill_parent" | |
xmlns:android="http://schemas.android.com/apk/res/android"> | |
<TableRow android:layout_height="wrap_content" | |
android:layout_width="wrap_content" | |
android:paddingBottom="5dp" | |
android:paddingTop="5dp" | |
android:background="#EAE9E9" | |
android:layout_margin="1px" | |
android:id="@+id/tableRow1"> | |
<TextView android:layout_height="wrap_content" | |
android:layout_width="0dp" | |
android:id="@+id/tv_Name" | |
android:text="N" | |
android:textColor="#333333" | |
android:textSize="11sp" | |
android:gravity="center" | |
android:textStyle="bold" | |
android:layout_weight="1"/> | |
<TextView android:layout_height="wrap_content" | |
android:layout_width="0dp" | |
android:id="@+id/tv_Match" | |
android:text="M" | |
android:textColor="#333333" | |
android:textSize="11sp" | |
android:gravity="center" | |
android:textStyle="bold" | |
android:layout_weight="1"/> | |
<TextView android:layout_height="wrap_content" | |
android:layout_width="0dp" | |
android:id="@+id/tv_Inning" | |
android:text="I" | |
android:textColor="#333333" | |
android:textSize="11sp" | |
android:gravity="center" | |
android:textStyle="bold" | |
android:layout_weight="1"/> | |
<TextView android:layout_height="wrap_content" | |
android:layout_width="0dp" | |
android:id="@+id/tv_Run" | |
android:text="R" | |
android:textColor="#333333" | |
android:textSize="11sp" | |
android:gravity="center" | |
android:textStyle="bold" | |
android:layout_weight="1"/> | |
<TextView android:layout_height="wrap_content" | |
android:layout_width="0dp" | |
android:id="@+id/tv_Wicket" | |
android:text="W" | |
android:textColor="#333333" | |
android:textSize="11sp" | |
android:gravity="center" | |
android:textStyle="bold" | |
android:layout_weight="1"/> | |
<TextView android:layout_height="wrap_content" | |
android:layout_width="0dp" | |
android:id="@+id/tv_BestRun" | |
android:text="BR" | |
android:textColor="#333333" | |
android:textSize="11sp" | |
android:gravity="center" | |
android:textStyle="bold" | |
android:layout_weight="1"/> | |
<TextView android:layout_height="wrap_content" | |
android:layout_width="0dp" | |
android:id="@+id/tv_BestBowling" | |
android:text="BB" | |
android:textColor="#333333" | |
android:textSize="11sp" | |
android:gravity="center" | |
android:textStyle="bold" | |
android:layout_weight="1"/> | |
<TextView android:layout_height="wrap_content" | |
android:layout_width="0dp" | |
android:id="@+id/tv_Average" | |
android:text="A" | |
android:textColor="#333333" | |
android:textSize="11sp" | |
android:gravity="center" | |
android:textStyle="bold" | |
android:layout_weight="1"/> | |
</TableRow> | |
</TableLayout> |
layout/player_item_list_header.xml
<TableLayout android:stretchColumns="*" android:layout_height="wrap_content" | |
android:layout_width="fill_parent" | |
xmlns:android="http://schemas.android.com/apk/res/android"> | |
<TableRow android:layout_height="wrap_content" | |
android:layout_width="wrap_content" | |
android:paddingBottom="5dp" | |
android:paddingTop="5dp" | |
android:background="#EAE9E9" | |
android:layout_margin="1px" | |
android:id="@+id/tableRow1"> | |
<TextView android:layout_height="wrap_content" | |
android:layout_width="0dp" | |
android:id="@+id/tv_RowHeader1" | |
android:text="N" | |
android:textColor="#333333" | |
android:textSize="11sp" | |
android:gravity="center" | |
android:textStyle="bold" | |
android:layout_weight="1"/> | |
<TextView android:layout_height="wrap_content" | |
android:layout_width="0dp" | |
android:id="@+id/tv_RowHeader2" | |
android:text="M" | |
android:textColor="#333333" | |
android:textSize="11sp" | |
android:gravity="center" | |
android:textStyle="bold" | |
android:layout_weight="1"/> | |
<TextView android:layout_height="wrap_content" | |
android:layout_width="0dp" | |
android:id="@+id/tv_RowHeader3" | |
android:text="I" | |
android:textColor="#333333" | |
android:textSize="11sp" | |
android:gravity="center" | |
android:textStyle="bold" | |
android:layout_weight="1"/> | |
<TextView android:layout_height="wrap_content" | |
android:layout_width="0dp" | |
android:id="@+id/tv_RowHeader4" | |
android:text="R" | |
android:textColor="#333333" | |
android:textSize="11sp" | |
android:gravity="center" | |
android:textStyle="bold" | |
android:layout_weight="1"/> | |
<TextView android:layout_height="wrap_content" | |
android:layout_width="0dp" | |
android:id="@+id/tv_RowHeader5" | |
android:text="W" | |
android:textColor="#333333" | |
android:textSize="11sp" | |
android:gravity="center" | |
android:textStyle="bold" | |
android:layout_weight="1"/> | |
<TextView android:layout_height="wrap_content" | |
android:layout_width="0dp" | |
android:id="@+id/tv_RowHeader6" | |
android:text="BR" | |
android:textColor="#333333" | |
android:textSize="11sp" | |
android:gravity="center" | |
android:textStyle="bold" | |
android:layout_weight="1"/> | |
<TextView android:layout_height="wrap_content" | |
android:layout_width="0dp" | |
android:id="@+id/tv_RowHeader7" | |
android:text="BB" | |
android:textColor="#333333" | |
android:textSize="11sp" | |
android:gravity="center" | |
android:textStyle="bold" | |
android:layout_weight="1"/> | |
<TextView android:layout_height="wrap_content" | |
android:layout_width="0dp" | |
android:id="@+id/tv_RowHeader8" | |
android:text="A" | |
android:textColor="#333333" | |
android:textSize="11sp" | |
android:gravity="center" | |
android:textStyle="bold" | |
android:layout_weight="1"/> | |
</TableRow> | |
</TableLayout> |
model/PlayerModel.java
package com.androdheight.firebase.model; import java.io.Serializable; | |
/** | |
* Created by prabhakar on 11/04/17. | |
*/ | |
public class PlayerModel implements Serializable{ | |
private String playerImage; | |
private String parentid; | |
private int p_id; | |
private String player_name; | |
private String player_match; | |
private String player_inning; | |
private String player_run; | |
private String player_wicket; | |
private String player_bestRun; | |
private String player_bestBowling; | |
private String average; | |
public int getP_id() { | |
return p_id; | |
} | |
public void setP_id(int p_id) { | |
this.p_id = p_id; | |
} | |
public String getPlayer_name() { | |
return player_name; | |
} | |
public void setPlayer_name(String player_name) { | |
this.player_name = player_name; | |
} | |
public String getPlayer_match() { | |
return player_match; | |
} | |
public void setPlayer_match(String player_match) { | |
this.player_match = player_match; | |
} | |
public String getPlayer_inning() { | |
return player_inning; | |
} | |
public void setPlayer_inning(String player_inning) { | |
this.player_inning = player_inning; | |
} | |
public String getPlayer_run() { | |
return player_run; | |
} | |
public void setPlayer_run(String player_run) { | |
this.player_run = player_run; | |
} | |
public String getPlayer_wicket() { | |
return player_wicket; | |
} | |
public void setPlayer_wicket(String player_wicket) { | |
this.player_wicket = player_wicket; | |
} | |
public String getPlayer_bestRun() { | |
return player_bestRun; | |
} | |
public void setPlayer_bestRun(String player_bestRun) { | |
this.player_bestRun = player_bestRun; | |
} | |
public String getPlayer_bestBowling() { | |
return player_bestBowling; | |
} | |
public void setPlayer_bestBowling(String player_bestBowling) { | |
this.player_bestBowling = player_bestBowling; | |
} | |
public String getAverage() { | |
return average; | |
} | |
public void setAverage(String average) { | |
this.average = average; | |
} | |
public String getPlayerImage() { | |
return playerImage; | |
} | |
public void setPlayerImage(String playerImage) { | |
this.playerImage = playerImage; | |
} | |
public String getParentid() { | |
return parentid; | |
} | |
public void setParentid(String parentid) { | |
this.parentid = parentid; | |
} | |
} |
Adapter/PlayerRecyclerAdapter.java
| ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.ImageView; | ||
import android.widget.TextView; | ||
import com.androdheight.firebase.MainActivity; | ||
import com.androdheight.firebase.R; | ||
import com.androdheight.firebase.model.PlayerModel; | ||
import java.util.ArrayList; | ||
/** | ||
* Created by prabhakar on 11/04/17. | ||
*/ | ||
public class PlayerRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { | ||
private static final int TYPE_HEADER = 0; | ||
private static final int TYPE_ITEM = 1; | ||
private ArrayList<PlayerModel> playerlist; | ||
Context context; | ||
static OnListItemClickListener listItemClickListener; | ||
public interface OnListItemClickListener{ | ||
public void onItemClickL(View view, int position); | ||
} | ||
public class MyViewHolder extends RecyclerView.ViewHolder { | ||
TextView tv_Name,tv_Match,tv_Inning,tv_Run,tv_Wicket,tv_BestRun,tv_BestBowling,tv_Average; | ||
public MyViewHolder(final View itemView) { | ||
super(itemView); | ||
tv_Name = (TextView) itemView.findViewById(R.id.tv_Name); | ||
tv_Match = (TextView) itemView.findViewById(R.id.tv_Match); | ||
tv_Inning = (TextView) itemView.findViewById(R.id.tv_Inning); | ||
tv_Run = (TextView) itemView.findViewById(R.id.tv_Run); | ||
tv_Wicket = (TextView) itemView.findViewById(R.id.tv_Wicket); | ||
tv_BestRun = (TextView) itemView.findViewById(R.id.tv_BestRun); | ||
tv_BestBowling = (TextView) itemView.findViewById(R.id.tv_BestBowling); | ||
tv_Average = (TextView) itemView.findViewById(R.id.tv_Average); | ||
itemView.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
listItemClickListener.onItemClickL(itemView,getAdapterPosition()); | ||
} | ||
}); | ||
} | ||
} | ||
class MyViewHolderHeader extends RecyclerView.ViewHolder{ | ||
public MyViewHolderHeader(View itemView) { | ||
super(itemView); | ||
} | ||
} | ||
private PlayerModel getItem (int position) { | ||
return playerlist.get (position); | ||
} | ||
public PlayerRecyclerAdapter(Context context,ArrayList<PlayerModel> playerlist) { | ||
listItemClickListener= (MainActivity) context; | ||
this.context = context; | ||
this.playerlist = playerlist; | ||
} | ||
@Override | ||
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, | ||
int viewType) { | ||
if (viewType == TYPE_HEADER) { | ||
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.player_item_list_header, parent, false); | ||
return new MyViewHolderHeader(v); | ||
} else if (viewType == TYPE_ITEM) { | ||
View view = LayoutInflater.from(parent.getContext()) | ||
.inflate(R.layout.player_item_list, parent, false); | ||
MyViewHolder myViewHolder = new MyViewHolder(view); | ||
return myViewHolder; | ||
} | ||
return null; | ||
} | ||
@Override | ||
public void onBindViewHolder( RecyclerView.ViewHolder viewHolder, int listPosition) { | ||
if(viewHolder instanceof MyViewHolder) { | ||
// PlayerModel currentItem = getItem (listPosition - 1); | ||
MyViewHolder holder = (MyViewHolder) viewHolder; | ||
holder.tv_Name.setText(playerlist.get(listPosition-1).getPlayer_name()); | ||
holder.tv_Match.setText(playerlist.get(listPosition-1).getPlayer_match()); | ||
holder.tv_Inning.setText(playerlist.get(listPosition-1).getPlayer_inning()); | ||
holder.tv_Run.setText(playerlist.get(listPosition-1).getPlayer_run()); | ||
holder.tv_Wicket.setText(playerlist.get(listPosition-1).getPlayer_wicket()); | ||
holder.tv_BestRun.setText(playerlist.get(listPosition-1).getPlayer_bestRun()); | ||
holder.tv_BestBowling.setText(playerlist.get(listPosition-1).getPlayer_bestBowling()); | ||
holder.tv_Average.setText(playerlist.get(listPosition-1).getAverage()); | ||
} | ||
} | ||
@Override | ||
public int getItemViewType (int position) { | ||
if(isPositionHeader (position)) { | ||
return TYPE_HEADER; | ||
} | ||
return TYPE_ITEM; | ||
} | ||
private boolean isPositionHeader (int position) { | ||
return position == 0; | ||
} | ||
@Override | ||
public int getItemCount() { | ||
return playerlist.size()+1; | ||
} | ||
} |
MainActivity.java
package com.androdheight.firebase; import android.content.Intent; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import com.androdheight.firebase.adapter.PlayerRecyclerAdapter; | |
import com.androdheight.firebase.model.PlayerModel; | |
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; | |
public class MainActivity extends AppCompatActivity implements PlayerRecyclerAdapter.OnListItemClickListener{ | |
private static RecyclerView.Adapter adapter; | |
private RecyclerView.LayoutManager layoutManager; | |
private static RecyclerView recyclerView; | |
private static ArrayList<PlayerModel> playerlist; | |
private EditText etplayername,etplayermatch,etplayerinning,etplayerrun, | |
etplayerwicket,playerBestRun,etplayerbestbowling,etplayerAvg; | |
private Button btnSubmit; | |
DatabaseReference databasePlayers; | |
String id; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
databasePlayers = FirebaseDatabase.getInstance().getReference("players"); | |
initilize(); | |
playerlist = new ArrayList<PlayerModel>(); | |
btnSubmit.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
getFieldsValue(); | |
} | |
}); | |
// specify an adapter (see also next example) | |
} | |
private void initilize(){ | |
etplayername = (EditText)findViewById(R.id.etplayername); | |
etplayermatch = (EditText)findViewById(R.id.etplayermatch); | |
etplayerinning = (EditText)findViewById(R.id.etplayerinning); | |
etplayerrun = (EditText)findViewById(R.id.etplayerrun); | |
etplayerwicket = (EditText)findViewById(R.id.etplayerwicket); | |
playerBestRun = (EditText)findViewById(R.id.etplayerBestRun); | |
etplayerbestbowling = (EditText)findViewById(R.id.etplayerbestbowling); | |
etplayerAvg = (EditText)findViewById(R.id.etplayerAvg); | |
btnSubmit =(Button)findViewById(R.id.btnSubmit); | |
recyclerView = (RecyclerView)findViewById(R.id.rvPlayerlist); | |
recyclerView.setHasFixedSize(true); | |
// use a linear layout manager | |
layoutManager = new LinearLayoutManager(this); | |
recyclerView.setLayoutManager(layoutManager); | |
//////////////////retrieve the value////////////// | |
databasePlayers.addValueEventListener(new ValueEventListener() { | |
@Override | |
public void onDataChange(DataSnapshot snapshot) { | |
playerlist.clear(); | |
for (DataSnapshot postSnapshot : snapshot.getChildren()) { | |
if(postSnapshot != null) { | |
String parentid = postSnapshot.getKey(); | |
PlayerModel model = postSnapshot.getValue(PlayerModel.class); | |
/////parse the single parameters////////////// | |
int p_id = model.getP_id(); | |
String image = model.getPlayerImage(); | |
String name = model.getPlayer_name(); | |
String match = model.getPlayer_match(); | |
String inning = model.getPlayer_inning(); | |
String run = model.getPlayer_run(); | |
String wicket = model.getPlayer_wicket(); | |
String bestRun = model.getPlayer_bestRun(); | |
String bestBowling = model.getPlayer_bestBowling(); | |
String avg = model.getAverage(); | |
model.setParentid(parentid); | |
playerlist.add(model); | |
adapter = new PlayerRecyclerAdapter(MainActivity.this,playerlist); | |
recyclerView.setAdapter(adapter); | |
} | |
} | |
} | |
@Override | |
public void onCancelled(DatabaseError databaseError) { | |
System.out.println("The read failed: " + databaseError.getMessage()); | |
} | |
}); | |
} | |
private void getFieldsValue(){ | |
int p_id = playerlist.size(); | |
String name = etplayername.getText().toString(); | |
String match = etplayermatch.getText().toString(); | |
String inning = etplayerinning.getText().toString(); | |
String run = etplayerrun.getText().toString(); | |
String wicket = etplayerwicket.getText().toString(); | |
String bestRun = playerBestRun.getText().toString(); | |
String bestBowling = etplayerbestbowling.getText().toString(); | |
String avg = etplayerAvg.getText().toString(); | |
//////////insert the value in firebase database//////////////////// | |
id =databasePlayers.push().getKey(); | |
PlayerModel model = new PlayerModel(); | |
model.setPlayerImage(""); | |
model.setPlayer_name(name); | |
model.setPlayer_match(match); | |
model.setPlayer_inning(inning); | |
model.setPlayer_run(run); | |
model.setPlayer_wicket(wicket); | |
model.setPlayer_bestRun(bestRun); | |
model.setPlayer_bestBowling(bestBowling); | |
model.setAverage(avg); | |
model.setParentid(id); | |
databasePlayers.child(id).setValue(model); | |
Toast.makeText(this,"Playes added",Toast.LENGTH_LONG).show(); | |
////retrieve data from firebase database////////////// | |
//Value event listener for realtime data update | |
/*adapter = new PlayerRecyclerAdapter(this,playerlist); | |
recyclerView.setAdapter(adapter);*/ | |
} | |
@Override | |
public void onItemClickL(View view, int position) { | |
Bundle bundle = new Bundle(); | |
bundle.putSerializable("player",playerlist.get(position-1)); | |
Intent i= new Intent(MainActivity.this,UpdatePlayerActivity.class); | |
i.putExtras(bundle); | |
startActivity(i); | |
} | |
} |
Screenshot
Get the full source code from Github
0 comments:
Post a Comment