ASP.NET, ASP.NET Core, JavaScript, SQL Server,WCF Services, Web Services, Angular, AngularJS, Ionic Framework
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="16dp" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="16dp" tools:context="com.database.firebase.firbasedatabase.MainActivity" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/txt_user" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingBottom="16dp" android:paddingTop="16dp" android:textSize="20dp" /> <EditText android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Name" android:inputType="textCapWords" android:maxLines="1" /> <EditText android:id="@+id/age" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Age" android:inputType="text|number" android:maxLines="1" /> <Button android:id="@+id/btn_save" style="?android:textAppearanceSmall" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:background="@color/colorPrimary" android:text="save" android:textColor="@android:color/white" android:textStyle="bold" /> </LinearLayout> </LinearLayout>
package com.database.firebase.firbasedatabase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; 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; public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName(); private TextView txtDetails; private EditText inputName, inputAge; private Button btnSave; private DatabaseReference mFirebaseDatabase; private FirebaseDatabase mFirebaseInstance; private String userId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportActionBar().setDisplayShowHomeEnabled(true); getSupportActionBar().setIcon(R.mipmap.ic_launcher); txtDetails = (TextView) findViewById(R.id.txt_user); inputName = (EditText) findViewById(R.id.name); inputAge = (EditText) findViewById(R.id.age); btnSave = (Button) findViewById(R.id.btn_save); mFirebaseInstance = FirebaseDatabase.getInstance(); mFirebaseDatabase = mFirebaseInstance.getReference("users"); mFirebaseInstance.getReference("app_title").setValue("Realtime Database"); mFirebaseInstance.getReference("app_title").addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { Log.e(TAG, "App title updated");
String appTitle = dataSnapshot.getValue(String.class);
getSupportActionBar().setTitle(appTitle);
} @Override public void onCancelled(DatabaseError error) {
Log.e(TAG, "Failed to read app title value.", error.toException()); } }); btnSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {
String name = inputName.getText().toString(); String age = inputAge.getText().toString(); if (TextUtils.isEmpty(userId)) {
createUser(name, age); } else { updateUser(name, age); } } }); toggleButton(); } private void toggleButton() { if (TextUtils.isEmpty(userId)) { btnSave.setText("Save"); } else { btnSave.setText("Update"); } } private void createUser(String name, String age) { if (TextUtils.isEmpty(userId)) { userId = mFirebaseDatabase.push().getKey(); } User user = new User(name,age); mFirebaseDatabase.child(userId).setValue(user); addUserChangeListener(); } private void addUserChangeListener() { mFirebaseDatabase.child(userId).addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class); if (user == null) { Log.e(TAG, "User data is null!"); return; } Log.e(TAG, "User data is changed!" + user.name + ", " + user.age); txtDetails.setText(user.name + ", " + user.age); inputAge.setText(""); inputName.setText(""); toggleButton(); } @Override public void onCancelled(DatabaseError error) { Log.e(TAG, "Failed to read user", error.toException()); } }); } private void updateUser(String name, String age) { if (!TextUtils.isEmpty(name)) mFirebaseDatabase.child(userId).child("name").setValue(name); if (!TextUtils.isEmpty(age)) mFirebaseDatabase.child(userId).child("age").setValue(age); } }
package com.database.firebase.firbasedatabase; /** * Created by vijay.parjapati on 4/18/2017. */ public class User { public String name; public String age; public User() { } public User(String name,String age) { this.name = name; this.age = age; } }
No comments:
Post a Comment