check if child exist in firebase for android (no given method on stackoverflow working )

583
May 25, 2017, at 5:10 PM
  1. i am making a chatting app for group chat and individual chat.
  2. i want to check if this chatroom(using this word for chat activity ) exist or not.
  3. i tried this in this way as given in other questions. but this checking code part run only onDataChange
  4. if no datachange then this part code is not running and adding duplicate values
  5. i read somewhere that for solving same problem need to use javascript
  6. but how to use or attch javascript in android studio.
  7. this given code is running but only on datachange please help me i am trying it from very long time.

    //android code for individual chat
     private DatabaseReference root = FirebaseDatabase.getInstance().getReference().getRoot();
     private DatabaseReference chat_Room_Ref;
     //name id is of logged user room id is user tio whome he wants to chat
     String tempkay=room_ID+"_"+name_ID;
                chat_Room_Ref=root.child(tempkay)
    chat_Room_Ref.addListenerForSingleValueEvent(new ValueEventListener() {
    
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        ifChildExist=dataSnapshot.exists();//if dataSnapshot is null means no child of this name
                        if(ifChildExist){
                            open_f=0;//2 1
                          //  Toast.makeText(getApplicationContext(),"in f0:"+ifChildExist+"=="+open_f,Toast.LENGTH_LONG).show();
                        }
                    }
                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                       //Toast.makeText(getApplicationContext(),"Database Connection Error",Toast.LENGTH_LONG).show();
                    }
                });
                if(!ifChildExist){
                    chat_Room_Ref=root.child(name_ID+"_"+room_ID);// if "1 2"
                    chat_Room_Ref.addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            ifChildExist=dataSnapshot.exists();//if dataSnapshot is null means no child of this name
                            if(ifChildExist){
                                open_f=1;//for 1 2
                                Toast.makeText(getApplicationContext(),"in f1:"+ifChildExist+"=="+open_f,Toast.LENGTH_LONG).show();
                            }
                        }
                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                            Toast.makeText(getApplicationContext(),"Database Connection Errot",Toast.LENGTH_LONG).show();
                        }
                    });
                }
    
                if(ifChildExist&&open_f==1){
                    int index=ID_Array.indexOf(room_ID);
                    room_Name=Name_Array.get(index);
                    Open_New_intent(room_ID);
                    Toast.makeText(getApplicationContext(),"Add a new User or Group already exist in chat",Toast.LENGTH_LONG).show();
                }
                Toast.makeText(getApplicationContext(),"ChildExist:"+ifChildExist,Toast.LENGTH_LONG).show();
                if(!ifChildExist){
                    //make new chat room named 2 1
                    Map<String,Object> map = new HashMap<String, Object>();
                    map.put(room_ID+"_"+name_ID,f);
    
                    root.updateChildren(map);
                    DatabaseReference message_root = root.child(room_ID+"_"+name_ID);
                    Map<String,Object> map2 = new HashMap<String, Object>();
                    map2.put("type",f);
                    map2.put("msg_Box","");
                    map2.put("Time_Stamp", ServerValue.TIMESTAMP);
                    Calendar c = Calendar.getInstance();
                    map2.put("msg_Date",c.getTime().toString());
                    message_root.updateChildren(map2);
                }
    
            }
        });
    
Rent Charter Buses Company
READ ALSO
Loading Data from Firebase into Recycleview using Fragments

Loading Data from Firebase into Recycleview using Fragments

anyone can help me with this firebase recycler view using fragments issue? I am having a problem with retrieving the data from firebase to my recycle view I am using fragments app crash everytime I run

454
RecyclerView smoothScrollToPosition does not work intermittently

RecyclerView smoothScrollToPosition does not work intermittently

smoothScrollToPosition is not work intermittently

447
prevent showing chooser dialog each time get user account

prevent showing chooser dialog each time get user account

I need to check user account in my app and I need to check it more than once in app , I used this answer and works fine , but the problem is each time I want to get account the chooser dialog appears for " choose an account " and its not good at all , I appreciate...

248
Android BluetoothGatt sample code use multi uuid

Android BluetoothGatt sample code use multi uuid

I revise android-BluetoothLeGatt sample code , add my uuid in SampleGattAttributesjava :

309