Search in sources :

Example 66 with DataSnapshot

use of com.google.firebase.database.DataSnapshot 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 67 with DataSnapshot

use of com.google.firebase.database.DataSnapshot 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 68 with DataSnapshot

use of com.google.firebase.database.DataSnapshot 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)

Example 69 with DataSnapshot

use of com.google.firebase.database.DataSnapshot in project SocialRec by Jkuras.

the class MainActivity method getPosts.

// sets listener at db/posts/deals/:all
// loads posts into mPosts, listener calls setUI
public void getPosts() {
    mref = mDataBase.getReference("posts/deals");
    ValueEventListener postListener = new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // initialize array of all posts and add them to mPosts
            mPosts = new ArrayList<DealPost>();
            for (DataSnapshot child : dataSnapshot.getChildren()) {
                mPosts.add((DealPost) child.getValue(DealPost.class));
            }
            setUI();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // Getting Post failed, log a message
            Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
        // ...
        }
    };
    mref.addListenerForSingleValueEvent(postListener);
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Aggregations

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