Search in sources :

Example 56 with DatabaseError

use of com.google.firebase.database.DatabaseError in project MadMax by deviz92.

the class EventsViewAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(final EventViewHolder eventViewHolder, int position) {
    final Event event = getItem(position).getValue();
    User currentUser = MainActivity.getCurrentUser();
    DatabaseReference databaseReference = FirebaseUtils.getDatabaseReference();
    switch(event.getEventType()) {
        case GROUP_ADD:
            eventViewHolder.imageView.setImageResource(R.drawable.group_add);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.GROUP_ADD) + " \"" + event.getObject() + "\"");
            break;
        /*case GROUP_REMOVE:
                eventViewHolder.imageView.setImageResource(R.drawable.group_remove);
                event.setDescription("\"" + event.getSubject() + "\" " + context.getString(R.string.GROUP_REMOVE) + " \"" + event.getObject() + "\"");
                break;*/
        case GROUP_EDIT:
            eventViewHolder.imageView.setImageResource(R.drawable.group_edit);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.GROUP_EDIT) + " \"" + event.getObject() + "\"");
            break;
        case GROUP_MEMBER_ADD:
            eventViewHolder.imageView.setImageResource(R.drawable.user_add);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.GROUP_MEMBER_ADD) + " " + event.getObject());
            break;
        case GROUP_MEMBER_REMOVE:
            eventViewHolder.imageView.setImageResource(R.drawable.user_remove);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.GROUP_MEMBER_REMOVE) + " " + event.getObject());
            break;
        case EXPENSE_ADD:
            eventViewHolder.imageView.setImageResource(R.drawable.expense_add);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.EXPENSE_ADD) + " \"" + event.getObject() + "\"");
            break;
        case EXPENSE_REMOVE:
            eventViewHolder.imageView.setImageResource(R.drawable.expense_remove);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.EXPENSE_REMOVE) + " \"" + event.getObject() + "\"");
            break;
        case EXPENSE_EDIT:
            eventViewHolder.imageView.setImageResource(R.drawable.expense_edit);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.EXPENSE_EDIT) + " \"" + event.getObject() + "\"");
            break;
        case PENDING_EXPENSE_ADD:
            eventViewHolder.imageView.setImageResource(R.drawable.expense_add);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.PENDING_EXPENSE_ADD) + " \"" + event.getObject() + "\"");
            break;
        case PENDING_EXPENSE_REMOVE:
            eventViewHolder.imageView.setImageResource(R.drawable.expense_remove);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.PENDING_EXPENSE_REMOVE) + " \"" + event.getObject() + "\"");
            break;
        case PENDING_EXPENSE_EDIT:
            eventViewHolder.imageView.setImageResource(R.drawable.expense_edit);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.PENDING_EXPENSE_EDIT) + " \"" + event.getObject() + "\"");
            break;
        case PENDING_EXPENSE_VOTE_UP:
            eventViewHolder.imageView.setImageResource(R.drawable.thumb_up_black);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.PENDING_EXPENSE_VOTE) + " \"" + event.getObject() + "\"");
            break;
        case PENDING_EXPENSE_VOTE_DOWN:
            eventViewHolder.imageView.setImageResource(R.drawable.thumb_down_black);
            event.setDescription(event.getSubject() + " " + context.getString(R.string.PENDING_EXPENSE_VOTE) + " \"" + event.getObject() + "\"");
            break;
        case PENDING_EXPENSE_APPROVED:
            eventViewHolder.imageView.setImageResource(R.drawable.expense_add);
            event.setDescription(context.getString(R.string.PENDING_EXPENSE) + " \"" + event.getObject() + "\" " + context.getString(R.string.PENDING_EXPENSE_APPROVED));
            break;
        case PENDING_EXPENSE_NEGLECTED:
            eventViewHolder.imageView.setImageResource(R.drawable.pending_expense_neglected);
            event.setDescription(context.getString(R.string.PENDING_EXPENSE) + " \"" + event.getObject() + "\" " + context.getString(R.string.PENDING_EXPENSE_NEGLECTED));
            break;
        /*case FRIEND_INVITE:
                eventViewHolder.imageView.setImageResource(R.drawable.user_add);
                event.setDescription(context.getString(R.string.FRIEND_INVITE) + " \"" + event.getObject() + "\"");
                break;*/
        case FRIEND_GROUP_INVITE:
            eventViewHolder.imageView.setImageResource(R.drawable.user_add);
            event.setDescription(context.getString(R.string.FRIEND_GROUP_INVITE) + " " + event.getObject());
            break;
        case // instead of the username the subject must be the ID of the user
        USER_PAY:
            eventViewHolder.imageView.setImageResource(R.drawable.user_pay);
            if (currentUser.getID().equals(event.getSubject())) {
                event.setDescription(context.getString(R.string.ME_PAY) + " " + event.getObject());
            } else {
                databaseReference.child("users").child(event.getSubject()).addListenerForSingleValueEvent(new ValueEventListener() {

                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        event.setDescription(dataSnapshot.child("name").getValue(String.class) + " " + dataSnapshot.child("surname").getValue(String.class) + " " + context.getString(R.string.FRIEND_PAY) + " " + event.getObject());
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        Log.w(TAG, databaseError.toException());
                    }
                });
            }
            break;
        case // instead of the username the subject must be the ID of the user
        USER_COMMENT_ADD:
            eventViewHolder.imageView.setImageResource(R.drawable.user_comment);
            if (currentUser.getID().equals(event.getSubject())) {
                event.setDescription(context.getString(R.string.ME_COMMENT_ADD) + " \"" + event.getObject() + "\"");
            } else {
                databaseReference.child("users").child(event.getSubject()).addListenerForSingleValueEvent(new ValueEventListener() {

                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        event.setDescription(dataSnapshot.child("name").getValue(String.class) + " " + dataSnapshot.child("surname").getValue(String.class) + " " + context.getString(R.string.FRIEND_COMMENT_ADD) + " \"" + event.getObject() + "\"");
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        Log.w(TAG, databaseError.toException());
                    }
                });
            }
            break;
        default:
            eventViewHolder.imageView.setImageResource(R.drawable.event_default);
            event.setDescription("\"" + event.getSubject() + "\" " + context.getString(R.string.did) + " \"" + event.getObject() + "\"");
    }
    eventViewHolder.descriptionTextView.setText(event.getDescription());
    eventViewHolder.timestampTextView.setText(context.getString(R.string.day) + " " + event.getDate() + " " + context.getString(R.string.at) + " " + event.getTime());
}
Also used : User(com.polito.mad17.madmax.entities.User) DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) Event(com.polito.mad17.madmax.entities.Event) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 57 with DatabaseError

use of com.google.firebase.database.DatabaseError in project MadMax by deviz92.

the class GroupsFragment method getGroupAndBalance.

/*    void getGroupAndBalance (final String userID, final String groupID)
    {

        //final HashMap <String, Double> totalBalance = new HashMap<>();
        final HashMap <String, Expense> groupExpenses = new HashMap<>();
        //totalBalance.put(userID,0d);
        //totBalance = 0d;
        final HashMap <String, Double> totBalances = new HashMap<>();
        totBalances.clear();

        groupListener = databaseReference.child("groups").child(groupID).addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(final DataSnapshot groupDataSnapshot) {

                //totalBalance.put(userID,0d);
                totBalances.clear();
                if (!listenedGroups.contains(groupID))
                    listenedGroups.add(groupID);

                final Boolean deleted = groupDataSnapshot.child("deleted").getValue(Boolean.class);

                if (deleted != null)
                {
                    for (DataSnapshot groupExpenseSnapshot: groupDataSnapshot.child("expenses").getChildren())
                    {
                        //Contribuiscono al bilancio solo le spese non eliminate dal gruppo
                        if (groupExpenseSnapshot.getValue(Boolean.class) == true)
                        {
                            //Adesso sono sicuro che la spesa non è stata eliminata
                            //Ascolto la singola spesa del gruppo
                            String expenseID = groupExpenseSnapshot.getKey();
                            databaseReference.child("expenses").child(expenseID).addListenerForSingleValueEvent(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {

                                    Boolean involved = false; //dice se user contribuisce o no a quella spesa

                                    for (DataSnapshot participantSnapshot: dataSnapshot.child("participants").getChildren())
                                    {
                                        //todo poi gestire caso in cui utente viene tolto dai participant alla spesa
                                        if (participantSnapshot.getKey().equals(userID))
                                            involved = true;
                                    }

                                    //se user ha partecipato alla spesa
                                    if (involved)
                                    {
                                        //alreadyPaid = soldi già messi dallo user per quella spesa
                                        //dueImport = quota che user deve mettere per quella spesa
                                        //balance = credito/debito dello user per quella spesa
                                        for (DataSnapshot d : dataSnapshot.child("participants").getChildren())
                                        {
                                            Log.d (TAG, "PartCampo " + d.getKey() + ": " + d.getValue() );
                                        }
                                        Double alreadyPaid = dataSnapshot.child("participants").child(userID).child("alreadyPaid").getValue(Double.class);

                                        Log.d (TAG, "Fraction: " + Double.parseDouble(String.valueOf(dataSnapshot.child("participants").child(userID).child("fraction").getValue())));

                                        Double dueImport = Double.parseDouble(String.valueOf(dataSnapshot.child("participants").child(userID).child("fraction").getValue())) * dataSnapshot.child("amount").getValue(Double.class);
                                        if (alreadyPaid != null && dueImport != null)
                                        {
                                            Double balance = alreadyPaid - dueImport;
                                            String currency = dataSnapshot.child("currency").getValue(String.class);
                                            //se user per quella spesa ha già pagato più soldi della sua quota, il balance è positivo

                                            //current balance for that currency
                                            Double temp = totBalances.get(currency);
                                            //update balance for that currency
                                            if (temp != null)
                                            {
                                                totBalances.put(currency, temp + balance);
                                             //   Log.d (TAG, "Actual debt for " + groupName + ": " + totBalances.get(currency) + " " + currency);
                                            }
                                            else
                                            {
                                                totBalances.put(currency, balance);
                                             //   Log.d (TAG, "Actual debt for " + groupName + ": " + totBalances.get(currency) + " " + currency);

                                            }
                                            //Double currentBalance = totalBalance.get(userID);
                                            //totalBalance.put(userID, currentBalance+balance);
                                            //totBalance += balance;

                                            Expense expense = new Expense();
                                            expense.setID(dataSnapshot.getKey());
                                            expense.setCurrency(dataSnapshot.child("currency").getValue(String.class));
                                            expense.setAmount(dataSnapshot.child("amount").getValue(Double.class));

                                            groupExpenses.put(dataSnapshot.getKey(), expense);

                                            Log.d(TAG, "added expense " + dataSnapshot.getKey());
                                        }
                                        else
                                        {
                                            Log.d (TAG, "Qui sarebbe crashato");
                                        }


                                    }
                                }

                                @Override
                                public void onCancelled(DatabaseError databaseError) {

                                }
                            });
                        }
                    }

                    Group group = new Group();
                    group.setName(groupDataSnapshot.child("name").getValue(String.class));
                    group.setImage(groupDataSnapshot.child("image").getValue(String.class));
                    group.setDeleted(deleted);
                    group.setBalance(0d);
                    group.setCurrencyBalances(totBalances);
                    group.setExpenses(groupExpenses);

                    if (!deleted &&
                            groupDataSnapshot.child("members").hasChild(MainActivity.getCurrentUID()) &&
                            !groupDataSnapshot.child("members").child(MainActivity.getCurrentUID()).child("deleted").getValue(Boolean.class)) {
                        groups.put(groupID, group);
                    }
                    else
                    {
                        groups.remove(groupID);
                    }

//                    Group nullGroup = new Group();
//                    nullGroup.setID(groupID + 1);
//                    groups.put(nullGroup.getID(), nullGroup);

                    groupsViewAdapter.update(groups);
                    groupsViewAdapter.notifyDataSetChanged();
                    //totBalance = 0d;
                }
            }

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


        return ;
    }*/
// versione di ale
// /versione più complessa dell getGroup. Quest'ultima non verrà più usata.
// oltre al nome gruppo, prende anche il bilancio dello user col gruppo
void getGroupAndBalance(final String userID, final String groupID) {
    // final HashMap <String, Double> totalBalance = new HashMap<>();
    final HashMap<String, Expense> groupExpenses = new HashMap<>();
    // totalBalance.put(userID,0d);
    // totBalance = 0d;
    final HashMap<String, Double> totBalances = new HashMap<>();
    totBalances.clear();
    groupListener = databaseReference.child("groups").child(groupID).addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(final DataSnapshot groupDataSnapshot) {
            // totalBalance.put(userID,0d);
            totBalances.clear();
            if (!listenedGroups.contains(groupID))
                listenedGroups.add(groupID);
            final String groupName = groupDataSnapshot.child("name").getValue(String.class);
            final Boolean deleted = groupDataSnapshot.child("deleted").getValue(Boolean.class);
            if (deleted != null) {
                for (DataSnapshot groupExpenseSnapshot : groupDataSnapshot.child("expenses").getChildren()) {
                    // Contribuiscono al bilancio solo le spese non eliminate dal gruppo
                    if (groupExpenseSnapshot.getValue(Boolean.class) == true) {
                        // Adesso sono sicuro che la spesa non è stata eliminata
                        // Ascolto la singola spesa del gruppo
                        String expenseID = groupExpenseSnapshot.getKey();
                        databaseReference.child("expenses").child(expenseID).addListenerForSingleValueEvent(new ValueEventListener() {

                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                // dice se user contribuisce o no a quella spesa
                                Boolean involved = false;
                                for (DataSnapshot participantSnapshot : dataSnapshot.child("participants").getChildren()) {
                                    // todo poi gestire caso in cui utente viene tolto dai participant alla spesa
                                    if (participantSnapshot.getKey().equals(userID))
                                        involved = true;
                                }
                                // se user ha partecipato alla spesa
                                if (involved) {
                                    // balance = credito/debito dello user per quella spesa
                                    for (DataSnapshot d : dataSnapshot.child("participants").getChildren()) {
                                        Log.d(TAG, "PartCampo " + d.getKey() + ": " + d.getValue());
                                    }
                                    Double alreadyPaid = dataSnapshot.child("participants").child(userID).child("alreadyPaid").getValue(Double.class);
                                    // If has been made to avoid crash
                                    if (dataSnapshot.child("participants").child(userID).hasChild("fraction") && dataSnapshot.hasChild("amount")) {
                                        Log.d(TAG, "Fraction: " + Double.parseDouble(String.valueOf(dataSnapshot.child("participants").child(userID).child("fraction").getValue())));
                                        Double dueImport = Double.parseDouble(String.valueOf(dataSnapshot.child("participants").child(userID).child("fraction").getValue())) * dataSnapshot.child("amount").getValue(Double.class);
                                        if (alreadyPaid != null && dueImport != null) {
                                            Double balance = alreadyPaid - dueImport;
                                            String currency = dataSnapshot.child("currency").getValue(String.class);
                                            // se user per quella spesa ha già pagato più soldi della sua quota, il balance è positivo
                                            // current balance for that currency
                                            Double temp = totBalances.get(currency);
                                            // update balance for that currency
                                            if (temp != null) {
                                                totBalances.put(currency, temp + balance);
                                                Log.d(TAG, "Actual debt for " + groupName + ": " + totBalances.get(currency) + " " + currency);
                                            } else {
                                                totBalances.put(currency, balance);
                                                Log.d(TAG, "Actual debt for " + groupName + ": " + totBalances.get(currency) + " " + currency);
                                            }
                                            // Double currentBalance = totalBalance.get(userID);
                                            // totalBalance.put(userID, currentBalance+balance);
                                            // totBalance += balance;
                                            Expense expense = new Expense();
                                            expense.setID(dataSnapshot.getKey());
                                            expense.setCurrency(dataSnapshot.child("currency").getValue(String.class));
                                            expense.setAmount(dataSnapshot.child("amount").getValue(Double.class));
                                            groupExpenses.put(dataSnapshot.getKey(), expense);
                                            Log.d(TAG, "added expense " + dataSnapshot.getKey());
                                        } else {
                                            Log.d(TAG, "Qui sarebbe crashato");
                                        }
                                    }
                                }
                                Group g = new Group();
                                g.setName(groupName);
                                // g.setBalance(totalBalance.get(userID));
                                g.setCurrencyBalances(totBalances);
                                g.setExpenses(groupExpenses);
                                g.setDeleted(deleted);
                                g.setImage(groupDataSnapshot.child("image").getValue(String.class));
                                // se il gruppo non è deleted e io faccio ancora parte del gruppo
                                if (!deleted && groupDataSnapshot.child("members").hasChild(MainActivity.getCurrentUID()) && !groupDataSnapshot.child("members").child(MainActivity.getCurrentUID()).child("deleted").getValue(Boolean.class)) {
                                    groups.put(groupID, g);
                                } else {
                                    groups.remove(groupID);
                                }
                                groupsViewAdapter.update(groups);
                                groupsViewAdapter.notifyDataSetChanged();
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {
                            }
                        });
                    }
                }
                // Per gestire il caso di quando ho appena abbandonato o eliminato il gruppo
                // if (dataSnapshot.child("members").hasChild(userID))
                // {
                Group g = new Group();
                g.setName(groupName);
                g.setImage(groupDataSnapshot.child("image").getValue(String.class));
                g.setBalance(0d);
                g.setDeleted(deleted);
                if (!deleted && groupDataSnapshot.child("members").hasChild(MainActivity.getCurrentUID()) && !groupDataSnapshot.child("members").child(MainActivity.getCurrentUID()).child("deleted").getValue(Boolean.class)) {
                    groups.put(groupID, g);
                } else {
                    groups.remove(groupID);
                }
                // Group nullGroup = new Group();
                // nullGroup.setID(groupID + 1);
                // groups.put(nullGroup.getID(), nullGroup);
                groupsViewAdapter.update(groups);
                groupsViewAdapter.notifyDataSetChanged();
            // totBalance = 0d;
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    return;
}
Also used : ViewGroup(android.view.ViewGroup) Group(com.polito.mad17.madmax.entities.Group) DatabaseError(com.google.firebase.database.DatabaseError) Expense(com.polito.mad17.madmax.entities.Expense) HashMap(java.util.HashMap) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 58 with DatabaseError

use of com.google.firebase.database.DatabaseError in project MadMax by deviz92.

the class GroupsFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.i(TAG, "onCreateView");
    setInterface((OnItemClickInterface) getActivity(), (OnItemLongClickInterface) getActivity());
    final View view = inflater.inflate(R.layout.skeleton_list, container, false);
    databaseReference = FirebaseDatabase.getInstance().getReference();
    RecyclerView.ItemDecoration divider = new InsetDivider.Builder(getContext()).orientation(InsetDivider.VERTICAL_LIST).dividerHeight(getResources().getDimensionPixelSize(R.dimen.divider_height)).color(ContextCompat.getColor(getContext(), R.color.colorDivider)).insets(getResources().getDimensionPixelSize(R.dimen.divider_inset), 0).overlay(true).build();
    recyclerView = (RecyclerView) view.findViewById(R.id.rv_skeleton);
    layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.addItemDecoration(divider);
    groups.clear();
    groupsViewAdapter = new GroupsViewAdapter(this.getContext(), this, this, groups, GroupsFragment.TAG);
    recyclerView.setAdapter(groupsViewAdapter);
    // Ascolto i gruppi dello user
    databaseReference.child("users").child(MainActivity.getCurrentUID()).child("groups").addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Per ogni gruppo dello user
            for (DataSnapshot groupSnapshot : dataSnapshot.getChildren()) {
                // Se il gruppo è true, ossia è ancora tra quelli dello user
                try {
                    Boolean trueGroup = (Boolean) groupSnapshot.getValue();
                    if (trueGroup)
                        getGroupAndBalance(MainActivity.getCurrentUID(), groupSnapshot.getKey());
                    else {
                        // tolgo il gruppo da quelli che verranno stampati, così lo vedo sparire realtime
                        groups.remove(groupSnapshot.getKey());
                        groupsViewAdapter.update(groups);
                        groupsViewAdapter.notifyDataSetChanged();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.d(TAG, groupSnapshot.getValue().toString());
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, databaseError.toException());
        }
    });
    return view;
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) RecyclerView(android.support.v7.widget.RecyclerView) ValueEventListener(com.google.firebase.database.ValueEventListener) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) DataSnapshot(com.google.firebase.database.DataSnapshot) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView)

Example 59 with DatabaseError

use of com.google.firebase.database.DatabaseError in project FindMyHome by DjangoBlockchained.

the class ShelterDetailActivity method onCreate.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shelter_details);
    list = findViewById(R.id.shelterdetails);
    claimbutton = (Button) findViewById(R.id.claimbutton);
    adapter = new ArrayAdapter(ShelterDetailActivity.this, android.R.layout.simple_list_item_1, details);
    username = getIntent().getStringExtra("username");
    String name = getIntent().getStringExtra("Shelter Name");
    shelterRef = FirebaseDatabase.getInstance().getReference().child("shelters").child(name);
    claimbutton.setOnClickListener((view) -> {
        Intent intent = new Intent(ShelterDetailActivity.this, ClaimScreenActivity.class);
        intent.putExtra("username", username);
        intent.putExtra("Shelter Name", name);
        startActivity(intent);
    });
    shelterRef.addChildEventListener(new ChildEventListener() {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Object data = dataSnapshot.getValue();
            details.add(getLabel(dataSnapshot.getKey(), data));
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            Object data = dataSnapshot.getValue();
            String key = dataSnapshot.getKey();
            // Find the current row for the key and replace it with the new value.
            int index = -1;
            boolean found = false;
            for (int i = 0; i < details.size() && !found; i++) {
                if (details.get(i).contains(key)) {
                    index = i;
                    found = true;
                }
            }
            if (index != -1) {
                details.set(index, getLabel(dataSnapshot.getKey(), data));
            } else {
                details.add(getLabel(dataSnapshot.getKey(), data));
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    list.setAdapter(adapter);
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Intent(android.content.Intent) DataSnapshot(com.google.firebase.database.DataSnapshot) ArrayAdapter(android.widget.ArrayAdapter) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 60 with DatabaseError

use of com.google.firebase.database.DatabaseError in project FindMyHome by DjangoBlockchained.

the class ShelterListActivity method showSearchedShelter.

private void showSearchedShelter(String name, String gender, String age) {
    shelterRef.addChildEventListener(new ChildEventListener() {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Map<String, Object> string = (Map<String, Object>) dataSnapshot.getValue();
            String restriction = string.get("restrictions").toString();
            String dbname = string.get("name").toString();
            if (gender.equals("Any")) {
                if (dbname.toLowerCase().contains(name.toLowerCase()) && (((!(restriction.contains("Men") || (restriction.contains("Women")))) || (restriction.contains("Anyone"))) && ((restriction.contains(age)) || (restriction.contains("Anyone"))))) {
                    names.add(string.get("name").toString());
                    adapter.notifyDataSetChanged();
                }
            } else if (!gender.equals("")) {
                if (dbname.toLowerCase().contains(name.toLowerCase()) && (((!restriction.contains(gender)) || (restriction.contains("Anyone"))) && ((restriction.contains(age)) || (restriction.contains("Anyone"))))) {
                    names.add(string.get("name").toString());
                    adapter.notifyDataSetChanged();
                }
            } else {
                if (dbname.toLowerCase().contains(name.toLowerCase()) && ((restriction.contains(age)) || (restriction.contains("Anyone")))) {
                    names.add(string.get("name").toString());
                    adapter.notifyDataSetChanged();
                }
            }
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    list.setAdapter(adapter);
    list.setOnItemClickListener((parent, view, position, id) -> {
        String selected = (String) list.getItemAtPosition(position);
        Intent intent = new Intent(ShelterListActivity.this, ShelterDetailActivity.class);
        intent.putExtra("username", username);
        intent.putExtra("Shelter Name", selected);
        startActivity(intent);
    });
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Intent(android.content.Intent) DataSnapshot(com.google.firebase.database.DataSnapshot) Map(java.util.Map) ChildEventListener(com.google.firebase.database.ChildEventListener)

Aggregations

DatabaseError (com.google.firebase.database.DatabaseError)61 DataSnapshot (com.google.firebase.database.DataSnapshot)59 ValueEventListener (com.google.firebase.database.ValueEventListener)44 DatabaseReference (com.google.firebase.database.DatabaseReference)22 View (android.view.View)21 Intent (android.content.Intent)15 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)13 RecyclerView (android.support.v7.widget.RecyclerView)13 User (com.polito.mad17.madmax.entities.User)12 MutableData (com.google.firebase.database.MutableData)11 Transaction (com.google.firebase.database.Transaction)11 Bundle (android.os.Bundle)10 TextView (android.widget.TextView)9 ChatMessageHelper (ingage.ingage20.helpers.ChatMessageHelper)8 ImageView (android.widget.ImageView)6 SimpleDateFormat (java.text.SimpleDateFormat)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ChildEventListener (com.google.firebase.database.ChildEventListener)5 Event (com.polito.mad17.madmax.entities.Event)5