Search in sources :

Example 86 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project iNGAGE by davis123123.

the class ChatFragment method onUpvoteClick.

@Override
public void onUpvoteClick(int p) {
    // Log.d("vote" , "up : ");
    // get correct chat msg with ith key from chatmessage helper
    ChatMessageHelper chatMessageHelper = (ChatMessageHelper) chatAdapter.getItem(p);
    String chat_key = chatMessageHelper.getMessageID();
    DatabaseReference message_root = currPageData.child(chat_key);
    // get upvote data
    DatabaseReference upvote_count = message_root.child("upvotes");
    upvote_count.runTransaction(new Transaction.Handler() {

        @Override
        public Transaction.Result doTransaction(MutableData currentData) {
            Log.d("Data", String.valueOf(currentData));
            if (currentData.getValue() == null) {
                currentData.setValue(1);
            } else {
                currentData.setValue((Long) currentData.getValue() + 1);
            }
            // we can also abort by calling Transaction.abort()
            return Transaction.success(currentData);
        }

        // TODO:Error handle here
        @Override
        public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
        }
    });
}
Also used : ChatMessageHelper(ingage.ingage20.helpers.ChatMessageHelper) DatabaseError(com.google.firebase.database.DatabaseError) Transaction(com.google.firebase.database.Transaction) DatabaseReference(com.google.firebase.database.DatabaseReference) MutableData(com.google.firebase.database.MutableData) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 87 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project iNGAGE by davis123123.

the class ChatFragment method removeUpvote.

@Override
public void removeUpvote(int p) {
    ChatMessageHelper chatMessageHelper = (ChatMessageHelper) chatAdapter.getItem(p);
    String chat_key = chatMessageHelper.getMessageID();
    DatabaseReference message_root = currPageData.child(chat_key);
    // get upvote data
    DatabaseReference upvote_count = message_root.child("upvotes");
    upvote_count.runTransaction(new Transaction.Handler() {

        @Override
        public Transaction.Result doTransaction(MutableData currentData) {
            Log.d("Data", String.valueOf(currentData));
            if (currentData.getValue() == null) {
                currentData.setValue(0);
            } else {
                currentData.setValue((Long) currentData.getValue() - 1);
            }
            // we can also abort by calling Transaction.abort()
            return Transaction.success(currentData);
        }

        // TODO:Error handle here
        @Override
        public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
        }
    });
}
Also used : ChatMessageHelper(ingage.ingage20.helpers.ChatMessageHelper) DatabaseError(com.google.firebase.database.DatabaseError) Transaction(com.google.firebase.database.Transaction) DatabaseReference(com.google.firebase.database.DatabaseReference) MutableData(com.google.firebase.database.MutableData) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 88 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project iNGAGE by davis123123.

the class ChatActivity method insertComment.

private void insertComment(final String messageBy, final String messageText) {
    Log.d("PAGELIMIT", " " + page_root);
    temp_key = page_root.push().getKey();
    DatabaseReference message_root = page_root.child(temp_key);
    String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
    Map<String, Object> map_message = new HashMap<String, Object>();
    map_message.put("Username", messageBy);
    map_message.put("Msg", messageText);
    map_message.put("Side", user_side);
    map_message.put("upvotes", 0);
    map_message.put("downvotes", 0);
    map_message.put("TimeStamp", currentDateTimeString);
    message_root.updateChildren(map_message);
}
Also used : DatabaseReference(com.google.firebase.database.DatabaseReference) HashMap(java.util.HashMap) Date(java.util.Date)

Example 89 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project priend by TakoJ.

the class disease_detail method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_disease_detail);
    TextView name = (TextView) findViewById(R.id.disease_name);
    final TextView define = (TextView) findViewById(R.id.define);
    final TextView condition = (TextView) findViewById(R.id.condition);
    final TextView cause = (TextView) findViewById(R.id.cause);
    Intent intent = getIntent();
    name.setText(intent.getStringExtra("name"));
    final String names = name.getText().toString();
    for (int i = 1; i < 40; i++) {
        final String num = Integer.toString(i);
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        final DatabaseReference myRef = database.getReference().child("diseases").child(num);
        myRef.child("diease_name").addListenerForSingleValueEvent(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    String disease_name = dataSnapshot.getValue().toString();
                    if (disease_name.equals(names)) {
                        myRef.child("define").addListenerForSingleValueEvent(new ValueEventListener() {

                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                String disease_define = dataSnapshot.getValue().toString();
                                define.setText(disease_define);
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {
                            }
                        });
                        myRef.child("condition").addListenerForSingleValueEvent(new ValueEventListener() {

                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                String disease_condition1 = dataSnapshot.getValue().toString();
                                String disease_condition2 = disease_condition1.replace("[", "");
                                String disease_condition3 = disease_condition2.replace("],", "\n");
                                String disease_condition = disease_condition3.replace("]]", "");
                                condition.setText(disease_condition);
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {
                            }
                        });
                        myRef.child("cause").addListenerForSingleValueEvent(new ValueEventListener() {

                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                String disease_cause = dataSnapshot.getValue().toString();
                                cause.setText(disease_cause);
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {
                            }
                        });
                        return;
                    }
                    ;
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) TextView(android.widget.TextView) Intent(android.content.Intent) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 90 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project priend by TakoJ.

the class disease_listActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_disease_list);
    ArrayList<String> getserver = getIntent().getExtras().getStringArrayList("selects");
    final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1);
    listview = (ListView) findViewById(R.id.diseaseView);
    listview.setAdapter(adapter);
    DatabaseReference database = FirebaseDatabase.getInstance().getReference();
    for (final String str : getserver) {
        for (int i = 0; i < 40; i++) {
            final String num = Integer.toString(i);
            final DatabaseReference mDatabase = database.child("diseases").child(num);
            mDatabase.child("condition").addListenerForSingleValueEvent(new ValueEventListener() {

                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists()) {
                        String disease_condition = dataSnapshot.getValue().toString();
                        if (disease_condition.contains(str)) {
                            mDatabase.child("diease_name").addListenerForSingleValueEvent(new ValueEventListener() {

                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {
                                    String name = dataSnapshot.getValue().toString();
                                    adapter.add(name);
                                }

                                @Override
                                public void onCancelled(DatabaseError databaseError) {
                                }
                            });
                        }
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            });
        }
    }
    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // listview의 item클릭시 detail로 넘어가기
            Intent intent = new Intent(getApplicationContext(), disease_detail.class);
            // intent객체에 데이터를 실어서 보내기
            String disease_name = ((TextView) view).getText().toString();
            intent.putExtra("name", disease_name);
            startActivity(intent);
        }
    });
}
Also used : DatabaseReference(com.google.firebase.database.DatabaseReference) Intent(android.content.Intent) DataSnapshot(com.google.firebase.database.DataSnapshot) TextView(android.widget.TextView) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) DatabaseError(com.google.firebase.database.DatabaseError) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ValueEventListener(com.google.firebase.database.ValueEventListener) ArrayAdapter(android.widget.ArrayAdapter)

Aggregations

DatabaseReference (com.google.firebase.database.DatabaseReference)157 DatabaseError (com.google.firebase.database.DatabaseError)95 DataSnapshot (com.google.firebase.database.DataSnapshot)87 ValueEventListener (com.google.firebase.database.ValueEventListener)61 FirebaseDatabase (com.google.firebase.database.FirebaseDatabase)50 HashMap (java.util.HashMap)24 View (android.view.View)18 MutableData (com.google.firebase.database.MutableData)17 Transaction (com.google.firebase.database.Transaction)17 Intent (android.content.Intent)16 FirebaseUser (com.google.firebase.auth.FirebaseUser)13 ArrayList (java.util.ArrayList)12 TextView (android.widget.TextView)11 Query (com.google.firebase.database.Query)11 Map (java.util.Map)11 ProgressDialog (android.app.ProgressDialog)10 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)10 ChildEventListener (com.google.firebase.database.ChildEventListener)9 SharedPreferences (android.content.SharedPreferences)8 ChatMessageHelper (ingage.ingage20.helpers.ChatMessageHelper)8