Created
          March 27, 2018 10:41 
        
      - 
      
 - 
        
Save sidward35/d234b4c1ab845434c8ca3c9eab4001a7 to your computer and use it in GitHub Desktop.  
    Android get data from Firebase realtime database and add it as CardView
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | private DatabaseReference mDatabase; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { //closes on line 74 | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_available_donations); | |
| final Context mContext = getApplicationContext(); | |
| mDatabase = FirebaseDatabase.getInstance().getReference(); | |
| mDatabase.addValueEventListener(new ValueEventListener() { //closes on line 73 | |
| @Override | |
| public void onDataChange(DataSnapshot dataSnapshot) { | |
| for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) { | |
| try{ | |
| String address = childDataSnapshot.child("address").getValue().toString(); | |
| String foodName = childDataSnapshot.child("foodName").getValue().toString(); | |
| String phoneNumber = childDataSnapshot.child("phoneNumber").getValue().toString(); | |
| String quantity = childDataSnapshot.child("quantity").getValue().toString(); | |
| CardView card = new CardView(mContext); | |
| // Set the CardView layoutParams | |
| LayoutParams params = new LayoutParams( | |
| LayoutParams.MATCH_PARENT, | |
| LayoutParams.WRAP_CONTENT); | |
| params.setMargins(40, 20, 40, 20); | |
| card.setLayoutParams(params); | |
| //card.setRadius(9); | |
| //card.setContentPadding(15, 15, 15, 15); | |
| card.setCardBackgroundColor(Color.parseColor("#e5e5e5")); | |
| //card.setMaxCardElevation(15); | |
| card.setCardElevation(5); | |
| TextView tv = new TextView(mContext); | |
| tv.setLayoutParams(params); | |
| tv.setText(foodName+" ("+quantity+")"); | |
| tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20); | |
| tv.setTextColor(Color.BLACK); | |
| tv.setGravity(Gravity.CENTER_HORIZONTAL); | |
| LayoutParams params2 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); | |
| params2.setMargins(0, 150, 0, 20); | |
| TextView tv3 = new TextView(mContext); | |
| tv3.setLayoutParams(params2); | |
| tv3.setText("\n"+address); | |
| tv3.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20); | |
| tv3.setTextColor(Color.DKGRAY); | |
| tv3.setGravity(Gravity.CENTER_HORIZONTAL); | |
| LayoutParams params3 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); | |
| params3.setMargins(0, 300, 0, 20); | |
| TextView tv4 = new TextView(mContext); | |
| tv4.setLayoutParams(params3); | |
| tv4.setText("\n\n\n"+phoneNumber.substring(0, 3)+"-"+phoneNumber.substring(3, 6)+"-"+phoneNumber.substring(6, 10)); | |
| tv4.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20); | |
| tv4.setTextColor(Color.DKGRAY); | |
| tv4.setGravity(Gravity.CENTER_HORIZONTAL); | |
| // Put the TextView in CardView | |
| card.addView(tv); | |
| card.addView(tv3); | |
| card.addView(tv4); | |
| // Finally, add the CardView in root layout | |
| ((LinearLayout)findViewById(R.id.linearlayoutboi)).addView(card); //replace "linearlayoutboi" with desired XML file in res/layouts/ | |
| }catch(Exception e){} | |
| } | |
| } | |
| @Override | |
| public void onCancelled(DatabaseError databaseError) {} | |
| }); //closes ValueEventListener | |
| } //closes onCreate() | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment