Skip to content

Instantly share code, notes, and snippets.

@crazy-diya
Last active August 7, 2020 06:50
Show Gist options
  • Save crazy-diya/e26077fd67f9fb912972a74fce6ab75f to your computer and use it in GitHub Desktop.
Save crazy-diya/e26077fd67f9fb912972a74fce6ab75f to your computer and use it in GitHub Desktop.
/**Getting Firebase Database data Using HashMap Used addValueEventListener for Auto data display for*/ in Android Studio Java
package com.example.learningfirebase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
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;
import java.util.Iterator;
public class Third_Activity extends AppCompatActivity {
EditText editText,editText2,editText3,editText4;
Button save,retrive;
ProgressBar progressBar;
ListView listView;
ArrayList arrayList;
ArrayAdapter arrayAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
editText = findViewById(R.id.editTextTextPersonName);
editText2 = findViewById(R.id.editTextTextPersonName2);
editText3 = findViewById(R.id.editTextTextPersonName3);
editText4 = findViewById(R.id.editTextTextPersonName4);
save = findViewById(R.id.button);
retrive = findViewById(R.id.button2);
progressBar = findViewById(R.id.progressBar2);
progressBar.setVisibility(View.INVISIBLE);
listView = findViewById(R.id.listview1);
arrayList = new ArrayList();
arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(Third_Activity.this, "Im "+(position+1), Toast.LENGTH_SHORT).show();
}
});
final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Worker");
/**Create Save method data using in to Firebase Database with another class*/
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
try { // wenas data type eken asign unoth program eka off nowenna try ekak daala exception handle karanawa
String name = editText.getText().toString().trim();
String collage = editText3.getText().toString().trim();
int age = Integer.parseInt(editText2.getText().toString().trim());
double salary = Double.parseDouble(editText4.getText().toString().trim());
Worker worker = new Worker(name,collage,age,salary); /**class ekak hadanawa data key walata*/
String key = databaseReference.push().getKey().trim(); /**awashya thna key eka hadawala eeka string ekak widihat gannawa*/
databaseReference.child(key).child(name + "'s Details! ").setValue(worker).addOnSuccessListener(new OnSuccessListener<Void>() {/**data assign worker OBJECT eka database ekata
set karanawa */
@Override
public void onSuccess(Void aVoid) {
progressBar.setVisibility(View.INVISIBLE);
Toast.makeText(Third_Activity.this, "Successfully Save Data!", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressBar.setVisibility(View.INVISIBLE);
Toast.makeText(Third_Activity.this, "Something error!", Toast.LENGTH_SHORT).show();
}
});
editText.setText(null);
editText3.setText(null);
editText2.setText(null);
editText4.setText(null);
}catch (Exception e){
progressBar.setVisibility(View.INVISIBLE);
AlertDialog.Builder builder = new AlertDialog.Builder(Third_Activity.this);
builder.setIcon(R.drawable.common_google_signin_btn_icon_light_normal);
builder.setTitle("Invalid input!");
builder.setMessage("Name : Text \n Age : Number only \n Collage = Text \n Salary = Number only");
builder.show();
}
}
});
/**End of Save method data using in to Firebase Database with another class*/
/**Getting Firebase Database data Using HashMap Used addValueEventListener for Auto data display for*/
progressBar.setVisibility(View.VISIBLE);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
arrayAdapter.clear();
/**********MOST IMPORTANT!***********/
HashMap<String,HashMap<String,HashMap<String,String>>> hashMap = (HashMap<String, HashMap<String, HashMap<String, String>>>) dataSnapshot.getValue();
for (HashMap h : hashMap.values()){
HashMap<String,HashMap<String,String>> hashMap1 = new HashMap<>(h);
for (HashMap hh : hashMap1.values()){
arrayList.add("Name is "+hh.get("name")+". Age is "+hh.get("age")+". Collage is "+hh.get("collage")+". Salary is "+hh.get("salary")+".");
}
}
System.out.println("* "+dataSnapshot.getKey());
System.out.println("* "+dataSnapshot.getValue());
System.out.println("* "+dataSnapshot.getChildren());
System.out.println("\n");
for (DataSnapshot ds : dataSnapshot.getChildren()){
System.out.println("# "+ds.getKey());
System.out.println("# "+ds.getValue());
System.out.println("# "+ds.getChildren());
System.out.println("\n");
for (DataSnapshot dss : ds.getChildren()){
System.out.println("$ "+dss.getKey());
System.out.println("$ "+dss.getValue());
System.out.println("$ "+dss.getChildren());
System.out.println("\n");
for (DataSnapshot dsss : dss.getChildren()){
System.out.println("& "+dsss.getKey());
System.out.println("& "+dsss.getValue());
System.out.println("& "+dsss.getChildren());
System.out.println("\n");
//arrayList.add(dsss.getKey() + " " + dsss.getValue());
}
}
// System.out.println(ds);
}
System.out.println(arrayList);
System.out.println(dataSnapshot.getValue());
arrayAdapter.notifyDataSetChanged();
System.out.println(arrayList);
//System.out.println(datas);
progressBar.setVisibility(View.INVISIBLE);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
/**Closing Firebase Database data Using HashMap Used addValueEventListener for Auto data display for*/
/**Getting Firebase Database data Using HashMap Used addListenerForSingleValueEvent in to click on the button and retrive method*/
/*retrive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
arrayAdapter.clear();
progressBar.setVisibility(View.VISIBLE);
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
String datas;
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
datas = dataSnapshot.getValue().toString();
HashMap<String,HashMap<String,HashMap<String,String>>> hashMap = (HashMap<String, HashMap<String, HashMap<String, String>>>) dataSnapshot.getValue();
for (HashMap h : hashMap.values()){
HashMap<String,HashMap<String,String>> hashMap1 = new HashMap<>(h);
for (HashMap hh : hashMap1.values()){
arrayList.add("Name is "+hh.get("name")+". Age is "+hh.get("age")+". Collage is "+hh.get("collage")+". Salary is "+hh.get("salary")+".");
}
}
arrayAdapter.notifyDataSetChanged();
System.out.println(arrayList);
//System.out.println(datas);
progressBar.setVisibility(View.INVISIBLE);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});*/
/**Ending Firebase Database data Using HashMap Used addListenerForSingleValueEvent in to click on the button and retrive method*/
}
}
class Worker{
String name;
String collage;
int age;
double salary;
public Worker(String name, String collage, int age, double salary) {
this.name = name;
this.collage = collage;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCollage() {
return collage;
}
public void setCollage(String collage) {
this.collage = collage;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
@Kaushalya-Udani
Copy link

Thanks for lot.very important to me this code.

@crazy-diya
Copy link
Author

Thanks for lot.very important to me this code.

dont think

@lasithadilshan
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment