Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add nodes dynamically from firebase #34

Closed
deeppomal opened this issue Mar 31, 2019 · 8 comments
Closed

How to add nodes dynamically from firebase #34

deeppomal opened this issue Mar 31, 2019 · 8 comments
Labels
question Further information is requested

Comments

@deeppomal
Copy link

Tried this but app crashes. I also want to add mulitple child nodes to a parent and I cant guess how to do that dynamically.

databaseReference = FirebaseDatabase.getInstance().getReference("family_members/");
databaseReference.addValueEventListener(new ValueEventListener() {
@OverRide
public void onDataChange(DataSnapshot snapshot) {

            for (DataSnapshot postSnapshot : snapshot.getChildren()) {

                FamMembers memberUploadInfo = postSnapshot.getValue(FamMembers.class);

                if(memberUploadInfo.getFname().equals("pp")) {
                    node1 = new Node(memberUploadInfo.getFname());
                    node2 = new Node(memberUploadInfo.getChildren());

                }


            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    graph.addEdge(node1,node2);
@GregorBlock
Copy link
Contributor

@deeppomal, you have too loop throw memberUploadInfo.getChildren() and create every time a new edge graph.addEdge(node1,famMember). Next time please add a stacktrace!

@deeppomal deeppomal reopened this Apr 4, 2019
@deeppomal
Copy link
Author

Okay so I made an arraylist of all FamMember objects and looped through it to find the parent of first generation which will have getParent value = "null" and if its getChildren is not null then it will display the children ( as code given below). Now this will only display first generation. Now how to display children of further generations. Like to add an edge the first argument should be compile time declared variable right? eg : Node node1 = new Node(Parent);

for(int i =0;i<famMembers.size();i++) {

                if (famMembers.get(i).getParent().equals("null")) {
                    Parent = famMembers.get(i).getFname();
                    node1 = new Node(Parent);
                    if(!famMembers.get(i).getChildren().equals("null"))
                        children = famMembers.get(i).getChildren();
                    graph.addEdge(node1, new Node(children));
                }

@DennisBlock
Copy link
Contributor

You are still not looping through your children. All you do is finding your first generation of parents and adding their children as a node. children should be an array you can loop through and create a new edge between the parent and each child. The child can also be a parent and have children, so you have to loop through them as well until there are no more children.

@GregorBlock
Copy link
Contributor

@deeppomal this can be achieved by recursion.

@GregorBlock GregorBlock added the question Further information is requested label Apr 5, 2019
@deeppomal
Copy link
Author

@GregorBlock Yes, implemented.

@garrysawyers3007
Copy link

I am trying to dynamically create a graph with each node displaying the event name.
final Graph graph = new Graph();
GraphView graphView = findViewById(R.id.graph);

    FirebaseFirestore db = FirebaseFirestore.getInstance();      

   db.collection("Events").whereEqualTo("Type","coding and fintech")
            .addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                    if (e != null) {
                        Log.w("TAG", "Listen failed.", e);
                        return;
                    }

                  

                        Log.d("sjcbscj","Yeah!!");
                        ArrayList<Event_Details> list=new ArrayList<>();
                        for (QueryDocumentSnapshot document : queryDocumentSnapshots) {
                            Log.d("TAG", document.getId() + " => " + document.get("Name"));
                            if(document.get("Name")!=null && document.get("Type")!=null && document.getTimestamp("Time")!=null)
                                list.add(new Event_Details(document.get("Name").toString(),document.get("Type").toString(),document.getTimestamp("Time")));
               }
                        

                        Node node1=new Node("Start");

                        for(int i=0;i<list.size();i++){
                            Node node2=new Node(list.get(i).getName());
                            graph.addEdge(node1,node2);
                            node1=node2;
                        }


                        final BaseGraphAdapter<ViewHolder> adapter=new BaseGraphAdapter<ViewHolder>() {
                            @NonNull
                            @Override
                            public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                                final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.node, parent, false);
                                return new SimpleViewHolder(view);
                            }

                            @Override
                            public void onBindViewHolder(ViewHolder viewHolder, Object data, int position) {
                                ((SimpleViewHolder)viewHolder).textView.setText(data.toString());
                            }

                            @Override
                            public Graph getGraph() {
                                return super.getGraph();
                            }
                        };


                            graphView.setAdapter(adapter);

                            adapter.setAlgorithm(new FruchtermanReingoldAlgorithm(1));
                            graphView.setZoomEnabled(true);


                    } else {
                        Log.d("TAG", "Current data: null");
                    }
                }

            }); 

However the app crashes and the following error shows up in the logcat

java.lang.NullPointerException: Attempt to invoke interface method 'de.blox.graphview.Graph de.blox.graphview.GraphAdapter.getGraph()' on a null object reference
at de.blox.graphview.GraphNodeContainerView.dispatchDraw(GraphNodeContainerView.java:303)
at android.view.View.updateDisplayListIfDirty(View.java:20462)
at android.view.View.draw(View.java:21326)
at android.view.ViewGroup.drawChild(ViewGroup.java:4413)
at com.otaliastudios.zoom.ZoomLayout.drawChild(ZoomLayout.kt:184)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4174)
at android.view.View.draw(View.java:21597)
at android.view.View.updateDisplayListIfDirty(View.java:20471)
at android.view.View.draw(View.java:21326)
at android.view.ViewGroup.drawChild(ViewGroup.java:4413)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4174)
at android.view.View.updateDisplayListIfDirty(View.java:20462)
at android.view.View.draw(View.java:21326)
at android.view.ViewGroup.drawChild(ViewGroup.java:4413)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4174)
at android.view.View.updateDisplayListIfDirty(View.java:20462)
at android.view.View.draw(View.java:21326)
at android.view.ViewGroup.drawChild(ViewGroup.java:4413)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4174)
at android.view.View.updateDisplayListIfDirty(View.java:20462)
at android.view.View.draw(View.java:21326)
at android.view.ViewGroup.drawChild(ViewGroup.java:4413)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4174)
at android.view.View.updateDisplayListIfDirty(View.java:20462)
at android.view.View.draw(View.java:21326)
at android.view.ViewGroup.drawChild(ViewGroup.java:4413)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4174)
at android.view.View.updateDisplayListIfDirty(View.java:20462)
at android.view.View.draw(View.java:21326)
at android.view.ViewGroup.drawChild(ViewGroup.java:4413)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4174)
at android.view.View.updateDisplayListIfDirty(View.java:20462)
at android.view.View.draw(View.java:21326)
at android.view.ViewGroup.drawChild(ViewGroup.java:4413)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4174)
at android.view.View.draw(View.java:21597)
at com.android.internal.policy.DecorView.draw(DecorView.java:815)
at android.view.View.updateDisplayListIfDirty(View.java:20471)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:580)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:586)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:659)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:3871)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:3671)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2993)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1930)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7988)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1154)
at android.view.Choreographer.doCallbacks(Choreographer.java:977)
at android.view.Choreographer.doFrame(Choreographer.java:893)
at android.view.Choreographer$FrameHandler.handleMessage(Choreographer.java:1082)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

@GregorBlock
Copy link
Contributor

It's a bug. Put the adapter initialization out of the callback directly after the findViewById and set the graph with adapter.setGraph()

@garrysawyers3007
Copy link

I put the initialization outside the call back and set the adapter outside it. It worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants