- Connect Your Project with Firebase Firestore . follow link https://olee.tech.blog/2019/05/22/android-connect-with-firebase/
- Add Internet Permission.
<uses-permission android:name="android.permission.INTERNET" />
- Update activity.main.xml file
<?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:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/singledatatextview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="add"
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/showdata"
android:text="Show Data"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/update"
android:text="Update Data"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/delete"
android:text="Delete"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/alldata"
android:text="alladata"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
- Update Mainactivity.java.
package olee.tech.ngo;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
public class MainActivity extends AppCompatActivity {
private Map <String,Object> user = new HashMap<String, Object>();
private EditText name,age;
private TextView singledatatextview;
private Button add,showdata,update,delete,alldata;
// Define Database Class Variable;
private FirebaseFirestore myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
singledatatextview=findViewById(R.id.singledatatextview);
name=findViewById(R.id.name);
add=findViewById(R.id.add);
update=findViewById(R.id.update);
delete=findViewById(R.id.delete);
alldata=findViewById(R.id.alldata);
alldata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Showlist.class);
startActivity(intent);
}
});
showdata=findViewById(R.id.showdata);
myDb=FirebaseFirestore.getInstance();
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String username=name.getText().toString().trim();
if (!TextUtils.isEmpty(username)){
myDb.collection("users").document("cMmiI2O580raRoX34OGc").update("name",username).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
name.setText("");
Toast.makeText(getApplicationContext(),"update successfull",Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
}
});
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username=name.getText().toString().trim();
if (!TextUtils.isEmpty(username)){
user.put("name",username);
myDb.collection("users").document().set(user).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
name.setText("");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
else {
name.setError("please Filled The Input");
}
}
});
showdata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DocumentReference documentReference = myDb.collection("users").document("cMmiI2O580raRoX34OGc");
documentReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()){
DocumentSnapshot documentSnapshot=task.getResult();
if (documentSnapshot.exists()){
StringBuilder stringBuilder = new StringBuilder("");
stringBuilder.append("name").append(documentSnapshot.get("name"));
singledatatextview.setText(stringBuilder.toString());
}
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DocumentReference documentReference= myDb.collection("users").document("cMmiI2O580raRoX34OGc");
documentReference.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(),"delete Successfull",Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
});
}
}