Showing posts with label Featch data from database in android. Show all posts
Showing posts with label Featch data from database in android. Show all posts

Tuesday, October 5, 2021

Retrive Data From Firebase in Android

 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.dashboard.DashboardFragment">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycleviewgg"/>

</LinearLayout>



Xml:- 

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="10dp"
app:cardCornerRadius="10dp"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<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:textSize="30sp"
android:text="Form"
android:layout_gravity="center"
android:textStyle="bold"/>

<TextView
android:textSize="20sp"
android:textStyle="bold"
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:textSize="20sp"
android:textStyle="bold"
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:textSize="20sp"
android:textStyle="bold"
android:id="@+id/pincode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:textSize="20sp"
android:textStyle="bold"
android:id="@+id/mobilenumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>



Java Code:-


package com.niranjan.cloudfirebase.ui.dashboard;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QuerySnapshot;
import com.niranjan.cloudfirebase.Adapter;
import com.niranjan.cloudfirebase.Model;
import com.niranjan.cloudfirebase.R;

import java.util.ArrayList;
import java.util.List;

public class DashboardFragment extends Fragment {
FirebaseFirestore firestore;
FirebaseAuth auth;

RecyclerView recyclerView;
Adapter adapter;
List<Model> modelList;
public DashboardFragment(){

}
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {

View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
firestore=FirebaseFirestore.getInstance();
auth=FirebaseAuth.getInstance();

recyclerView=root.findViewById(R.id.recycleviewgg);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

modelList= new ArrayList<>();
adapter = new Adapter(getActivity(),modelList);
recyclerView.setAdapter(adapter);

firestore.collection("Address").document(auth.getCurrentUser().getUid())
.collection("CurrentUser").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()){

for (DocumentSnapshot documentSnapshot : task.getResult().getDocuments()){
Model model = documentSnapshot.toObject(Model.class);
modelList.add(model);
adapter.notifyDataSetChanged();

}
}
}
});
return root;
}
}



Model class :-


package com.niranjan.cloudfirebase;

public class Model {
String name,address,pincode,mobilenumbr;

public Model() {
}

public Model(String name, String address, String pincode, String mobilenumbr) {
this.name = name;
this.address = address;
this.pincode = pincode;
this.mobilenumbr = mobilenumbr;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getPincode() {
return pincode;
}

public void setPincode(String pincode) {
this.pincode = pincode;
}

public String getMobilenumbr() {
return mobilenumbr;
}

public void setMobilenumbr(String mobilenumbr) {
this.mobilenumbr = mobilenumbr;
}
}






Adapter class :- 



package com.niranjan.cloudfirebase;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private Context context;
private List<Model> modelList;

public Adapter(Context context, List<Model> modelList) {
this.context = context;
this.modelList = modelList;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new Adapter.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.showitem,parent,false));
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.name.setText(modelList.get(position).getName());
holder.address.setText(modelList.get(position).getAddress());
holder.pincode.setText(modelList.get(position).getPincode());
holder.mobilenumbr.setText(modelList.get(position).getMobilenumbr());
}

@Override
public int getItemCount() {
return modelList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
TextView name,address,pincode,mobilenumbr;
public ViewHolder(@NonNull View itemView) {
super(itemView);
name=itemView.findViewById(R.id.name);
address=itemView.findViewById(R.id.address);
pincode=itemView.findViewById(R.id.pincode);
mobilenumbr=itemView.findViewById(R.id.mobilenumber);
}
}
}

Activity Navigation One Activity to another Activity

Main Activity :-   <? xml version ="1.0" encoding ="utf-8" ?> < RelativeLayout xmlns: android ="http://sch...