Click here to Skip to main content
15,348,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am achieving this to Simple Recyclerview but i am issue arising when user add or remove …auto populate not . So FirestoreRecycleAdapter can used? Help me ?

i add all snapshots below


Database structure is

collection>Document> array
>map
pickup= value
drop=value
via=value

What I have tried:

myjouurneyorder Fragment code

public class JourneyOrderFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    FirebaseFirestore db;
    RecyclerView recview2;

    RecycleAdapter adapter;

    FloatingActionButton fab;
   
     ArrayList<model> datalist;
  
    public JourneyOrderFragment() {
        // Required empty public constructor
    }

    
    // TODO: Rename and change types and number of parameters
    public static JourneyOrderFragment newInstance(String param1, String param2) {
        JourneyOrderFragment fragment = new JourneyOrderFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment 
        View view=inflater.inflate(R.layout.fragment_journey_order, container, false);
       
        recview2 =  view.findViewById(R.id.recview);
        recview2.setLayoutManager(new LinearLayoutManager(getContext()));
        recview2.setHasFixedSize(true);
        
        datalist=new ArrayList<model> ();
       adapter=new RecycleAdapter(datalist);
       
        fab=view.findViewById(R.id.fabbtn);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"you clicked me i am plus button",Toast.LENGTH_LONG).show();
                Intent intent=new Intent(getActivity(),AddJourneyOrder.class);
                startActivity(intent);
            }
        });

         db = FirebaseFirestore.getInstance();
        db.collection("orders").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {

                                ArrayList<model> journeyorders = document.toObject(modelDocument.class).journeyorders;

                    Log.d(TAG, "the document id is : "+document.getId()+"the document is :"+document.getData());
                    datalist.addAll(journeyorders);

                                recview2.setAdapter(adapter);
                            }
                        } else {
                            Log.d(TAG, "Error getting documents: ", task.getException());
                        }
                    }
                });
        
    
    
        return view;
    }

}




2) model class

package com.example.android.sohan.mohan.cabbie.modelclasses;


import java.util.ArrayList;

public class model {

    String pickup;
    String via;
    String drop;

   

    public model() {
    }

    public model(String pickup,String via,String drop) {
        this.pickup = pickup;
        this.via = via;
        this.drop = drop;
    }

    public String getPickup() {
        return pickup;
    }

    public void setPickup(String pickup) {
        this.pickup = pickup;
    }

    public String getVia() {
        return via;
    }

    public void setVia(String via) {
        this.via = via;
    }

    public String getDrop() {
        return drop;
    }

    public void setDrop(String drop) {
        this.drop = drop;
    }
}


3) modelDocument class

<pre>package com.example.android.sohan.mohan.cabbie.modelclasses;

import java.util.ArrayList;

public class modelDocument {

    
    public ArrayList<model> journeyorders;

    public void setJourneyorders(ArrayList<model> journeyorders) {
        this.journeyorders = journeyorders;
    }

   

    public modelDocument() {

    }
    
    
    
}


4) RecycleAdapter class

<pre>package com.example.android.sohan.mohan.cabbie.adapters;

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 com.example.android.sohan.mohan.cabbie.R;
import com.example.android.sohan.mohan.cabbie.modelclasses.model;

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

public class RecycleAdapter extends RecyclerView.Adapter<recycleadapter.viewholder>{
   
    ArrayList<model> datalist;
    public RecycleAdapter(ArrayList<model> datalist) {
        super();
        this.datalist=datalist;

    }


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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.t1.setText(datalist.get(position).getPickup());
        holder.t2.setText(datalist.get(position).getVia());
        holder.t3.setText(datalist.get(position).getDrop());
    }

    

   

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

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView t1;
        TextView t2;
        TextView t3;

        public ViewHolder(View itemView) {
            super(itemView);
            t1 = itemView.findViewById(R.id.t6);
            t2 = itemView.findViewById(R.id.t7);
            t3 = itemView.findViewById(R.id.t8);
        }
    }

}
Posted
Updated 25-May-22 6:53am
v2
Comments
k5054 25-May-22 9:23am
   
You've tagged this as C++, but it's clearly Java. Can you fix the tags? That might help getting help with your issue.
Bhumesh Thakur 25-May-22 12:53pm
   
its java....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900