Skip to content

Commit c69233e

Browse files
committed
Loading Data from the Firebase Database
1 parent 660a4df commit c69233e

File tree

6 files changed

+203
-0
lines changed

6 files changed

+203
-0
lines changed
0 Bytes
Binary file not shown.

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ dependencies {
2929
implementation 'com.firebaseui:firebase-ui-auth:3.3.1'
3030
implementation 'com.google.firebase:firebase-storage:15.0.0'
3131
implementation 'com.google.firebase:firebase-database:15.0.0'
32+
implementation 'com.squareup.picasso:picasso:2.71828'
3233
}
3334

3435
apply plugin: 'com.google.gms.google-services'

app/src/main/java/com/dragosholban/myinstagramapp/FeedActivity.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,38 @@
99
import android.support.v4.content.ContextCompat;
1010
import android.support.v7.app.AppCompatActivity;
1111
import android.os.Bundle;
12+
import android.support.v7.widget.LinearLayoutManager;
13+
import android.support.v7.widget.RecyclerView;
1214
import android.view.View;
1315
import android.widget.Toast;
1416

1517
import com.google.android.gms.tasks.OnFailureListener;
1618
import com.google.android.gms.tasks.OnSuccessListener;
1719
import com.google.firebase.auth.FirebaseAuth;
1820
import com.google.firebase.auth.FirebaseUser;
21+
import com.google.firebase.database.ChildEventListener;
22+
import com.google.firebase.database.DataSnapshot;
23+
import com.google.firebase.database.DatabaseError;
1924
import com.google.firebase.database.DatabaseReference;
2025
import com.google.firebase.database.FirebaseDatabase;
26+
import com.google.firebase.database.Query;
27+
import com.google.firebase.database.ValueEventListener;
2128
import com.google.firebase.storage.FirebaseStorage;
2229
import com.google.firebase.storage.StorageReference;
2330
import com.google.firebase.storage.UploadTask;
2431

2532
import java.text.SimpleDateFormat;
33+
import java.util.ArrayList;
2634
import java.util.Date;
2735

2836
public class FeedActivity extends AppCompatActivity {
2937

3038
FirebaseUser fbUser;
3139
DatabaseReference database;
40+
RecyclerView recyclerView;
41+
RecyclerView.LayoutManager mLayoutManager;
42+
ImageAdapter mAdapter;
43+
ArrayList<Image> images = new ArrayList<>();
3244

3345
static final int RC_PERMISSION_READ_EXTERNAL_STORAGE = 1;
3446
static final int RC_IMAGE_GALLERY = 2;
@@ -44,6 +56,61 @@ protected void onCreate(Bundle savedInstanceState) {
4456
}
4557

4658
database = FirebaseDatabase.getInstance().getReference();
59+
60+
// Setup the RecyclerView
61+
recyclerView = findViewById(R.id.recyclerView);
62+
mLayoutManager = new LinearLayoutManager(this);
63+
recyclerView.setLayoutManager(mLayoutManager);
64+
mAdapter = new ImageAdapter(images, this);
65+
recyclerView.setAdapter(mAdapter);
66+
67+
// Get the latest 100 images
68+
Query imagesQuery = database.child("images").orderByKey().limitToFirst(100);
69+
imagesQuery.addChildEventListener(new ChildEventListener() {
70+
@Override
71+
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
72+
73+
// A new image has been added, add it to the displayed list
74+
final Image image = dataSnapshot.getValue(Image.class);
75+
76+
// get the image user
77+
database.child("users/" + image.userId).addListenerForSingleValueEvent(new ValueEventListener() {
78+
@Override
79+
public void onDataChange(DataSnapshot dataSnapshot) {
80+
User user = dataSnapshot.getValue(User.class);
81+
image.user = user;
82+
mAdapter.notifyDataSetChanged();
83+
}
84+
85+
@Override
86+
public void onCancelled(DatabaseError databaseError) {
87+
88+
}
89+
});
90+
91+
mAdapter.addImage(image);
92+
}
93+
94+
@Override
95+
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
96+
97+
}
98+
99+
@Override
100+
public void onChildRemoved(DataSnapshot dataSnapshot) {
101+
102+
}
103+
104+
@Override
105+
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
106+
107+
}
108+
109+
@Override
110+
public void onCancelled(DatabaseError databaseError) {
111+
112+
}
113+
});
47114
}
48115

49116
public void uploadImage(View view) {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.dragosholban.myinstagramapp;
2+
3+
import android.support.v7.widget.RecyclerView;
4+
import android.view.LayoutInflater;
5+
import android.view.View;
6+
import android.view.ViewGroup;
7+
import android.widget.Button;
8+
import android.widget.ImageView;
9+
import android.widget.TextView;
10+
11+
import com.squareup.picasso.Picasso;
12+
13+
import java.util.ArrayList;
14+
15+
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {
16+
private ArrayList<Image> mDataset;
17+
private FeedActivity mActivity;
18+
19+
public static class ViewHolder extends RecyclerView.ViewHolder {
20+
public TextView mTextView;
21+
public ImageView mImageView;
22+
public Button mLikeButton;
23+
24+
public ViewHolder(View v) {
25+
super(v);
26+
mTextView = v.findViewById(R.id.textView2);
27+
mImageView = v.findViewById(R.id.imageView);
28+
mLikeButton = v.findViewById(R.id.likeButton);
29+
}
30+
}
31+
32+
public ImageAdapter(ArrayList<Image> myDataset, FeedActivity activity) {
33+
mDataset = myDataset;
34+
mActivity = activity;
35+
}
36+
37+
// Create new views (invoked by the layout manager)
38+
@Override
39+
public ImageAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
40+
// create a new view
41+
View v = LayoutInflater.from(parent.getContext())
42+
.inflate(R.layout.image_view, parent, false);
43+
44+
ViewHolder vh = new ViewHolder(v);
45+
return vh;
46+
}
47+
48+
// Replace the contents of a view (invoked by the layout manager)
49+
@Override
50+
public void onBindViewHolder(ViewHolder holder, final int position) {
51+
final Image image = (Image) mDataset.get(position);
52+
if (image.user != null) {
53+
holder.mTextView.setText(image.user.displayName);
54+
}
55+
Picasso.get().load(image.downloadUrl).into(holder.mImageView);
56+
}
57+
58+
// Return the size of your dataset (invoked by the layout manager)
59+
@Override
60+
public int getItemCount() {
61+
return mDataset.size();
62+
}
63+
64+
public void addImage(Image image) {
65+
mDataset.add(0, image);
66+
notifyDataSetChanged();
67+
}
68+
}

app/src/main/res/layout/activity_feed.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,19 @@
2121
app:layout_constraintEnd_toEndOf="parent"
2222
app:layout_constraintStart_toStartOf="parent" />
2323

24+
<android.support.v7.widget.RecyclerView
25+
android:id="@+id/recyclerView"
26+
android:layout_width="0dp"
27+
android:layout_height="0dp"
28+
android:layout_marginBottom="8dp"
29+
android:layout_marginEnd="8dp"
30+
android:layout_marginLeft="8dp"
31+
android:layout_marginRight="8dp"
32+
android:layout_marginStart="8dp"
33+
android:layout_marginTop="8dp"
34+
app:layout_constraintBottom_toTopOf="@+id/uploadButton"
35+
app:layout_constraintEnd_toEndOf="parent"
36+
app:layout_constraintStart_toStartOf="parent"
37+
app:layout_constraintTop_toTopOf="parent" />
38+
2439
</android.support.constraint.ConstraintLayout>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.constraint.ConstraintLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
xmlns:tools="http://schemas.android.com/tools"
6+
android:layout_width="match_parent"
7+
android:layout_height="wrap_content">
8+
9+
<ImageView
10+
android:id="@+id/imageView"
11+
android:layout_width="match_parent"
12+
android:layout_height="wrap_content"
13+
android:layout_marginBottom="8dp"
14+
android:layout_marginEnd="8dp"
15+
android:layout_marginLeft="8dp"
16+
android:layout_marginRight="8dp"
17+
android:layout_marginStart="8dp"
18+
android:layout_marginTop="8dp"
19+
app:layout_constraintBottom_toTopOf="@+id/textView2"
20+
app:layout_constraintEnd_toEndOf="parent"
21+
app:layout_constraintStart_toStartOf="parent"
22+
app:layout_constraintTop_toTopOf="parent"
23+
android:adjustViewBounds="true" />
24+
25+
<TextView
26+
android:id="@+id/textView2"
27+
android:layout_width="0dp"
28+
android:layout_height="wrap_content"
29+
android:layout_marginBottom="8dp"
30+
android:layout_marginEnd="8dp"
31+
android:layout_marginLeft="8dp"
32+
android:layout_marginRight="8dp"
33+
android:layout_marginStart="8dp"
34+
app:layout_constraintBottom_toBottomOf="parent"
35+
app:layout_constraintEnd_toStartOf="@id/likeButton"
36+
app:layout_constraintStart_toStartOf="parent" />
37+
38+
<Button
39+
android:id="@+id/likeButton"
40+
android:layout_width="wrap_content"
41+
android:layout_height="wrap_content"
42+
android:layout_marginBottom="8dp"
43+
android:layout_marginEnd="8dp"
44+
android:layout_marginLeft="8dp"
45+
android:layout_marginRight="8dp"
46+
android:layout_marginStart="8dp"
47+
android:text="Like"
48+
app:layout_constraintBottom_toBottomOf="parent"
49+
app:layout_constraintEnd_toEndOf="parent"
50+
app:layout_constraintStart_toEndOf="@id/textView2" />
51+
52+
</android.support.constraint.ConstraintLayout>

0 commit comments

Comments
 (0)