Data in recycler view not being uploaded to Firebase

313 views Asked by At

So I did some research about this but was unable to solve this problem and every solution online led to me to a new error.

I am currently working on an Events App and relatively new to Android Studio.

I have a RecyclerView in an Admin app that verifies all the Event data uploaded on Firebase by an organizer. The organizer uploads the data such as event_tile, event_desc and event_image. This is stored in Firebase under root "Event". Later the Admin App receives these requests in the form of a recycler view, and has a button to approve them. All the approved events are would be stored in a separate table in Firebase with root "Approved_Events".

I am getting stuck in the approval part. The code is running fine with no errors but no data is being uploaded to my Firebase console.

Here is my Main Activity

     public class MainActivity extends AppCompatActivity {

        private RecyclerView request_eventList;
        private DatabaseReference mRef, aRef;
        private Button verify_button;
        private TextView request_title, request_desc;
        private ImageView request_image;
        ProgressDialog progressDialog;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            progressDialog = new ProgressDialog(this);
            mRef = FirebaseDatabase.getInstance().getReference().child("Event");
            setContentView(R.layout.activity_main);
            request_eventList = (RecyclerView) findViewById(R.id.request_eventList);
            request_eventList.setHasFixedSize(true);
            request_eventList.setLayoutManager(new LinearLayoutManager(this));


        }

        @Override
        protected void onStart() {
            super.onStart();

            FirebaseRecyclerAdapter<Event, RequestViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Event, RequestViewHolder>(
                    Event.class,
                    R.layout.admin_event_row,
                    RequestViewHolder.class,
                    mRef

            ) {
                @Override
                protected void populateViewHolder(RequestViewHolder viewHolder, Event model, int position) {
                    viewHolder.setTitle(model.getTitle());
                    viewHolder.setDesc(model.getDesc());
                    viewHolder.setImage(getApplicationContext(), model.getImage());
                }
            };
            request_eventList.setAdapter(firebaseRecyclerAdapter);

        }

        public static class RequestViewHolder extends RecyclerView.ViewHolder {
            View mView;
            public Button verify_button;
            DatabaseReference mRef, aRef;
            ProgressDialog progressDialog;

            public RequestViewHolder(View itemView) {
                super(itemView);
                mView = itemView;


                progressDialog = new ProgressDialog(mView.getContext());
                verify_button = (Button) mView.findViewById(R.id.approve_button);
                mRef = FirebaseDatabase.getInstance().getReference().child("Event");
                aRef = FirebaseDatabase.getInstance().getReference().child("ApprovedEvents");

                verify_button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {mRef.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            Map <String, String> map = (Map<String, String>) dataSnapshot.getValue();

                            String title_val = map.get("title");
                            String desc_val = map.get("desc");
                            String image_val = map.get("image");

                            aRef.child("approved_title").setValue(title_val);
                            aRef.child("approved_desc").setValue(desc_val);
                            aRef.child("approved_image").setValue(image_val);
                            progressDialog.dismiss();
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });


                    }
                });

            }

            public void setTitle(String title) {
                TextView request_title = (TextView) mView.findViewById(R.id.request_title);
                request_title.setText(title);


            }

            public void setDesc(String desc) {
                TextView request_desc = (TextView) mView.findViewById(R.id.request_desc);
                request_desc.setText(desc);
            }

            public void setImage(Context ctx, String image) {
                ImageView request_image = (ImageView) mView.findViewById(R.id.request_image);
                Picasso.with(ctx).load(image).into(request_image);
            }

        }
    }

MainActivity XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.admin.admin.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/request_eventList"
        android:clickable="true">
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

Event Java Class

    public class Event {
        private String title, desc, image;

        public Event(String title, String desc, String image) {
            this.title = title;
            this.desc = desc;
            this.image = image;
        }
        public Event(){

        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }

        public String getImage() {
            return image;
        }

        public void setImage(String image) {
            this.image = image;
        }
    }

admin_event_row XML file that will fill the recyclerView

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/request_image"
    android:src="@drawable/abc_btn_check_material"
    android:adjustViewBounds="true" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/request_title"
    android:text="Title will come here "
    android:padding="10dp"
    android:textStyle="bold"
    android:textSize="15dp" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/request_desc"
    android:text="Desc will come here "
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingBottom="10dp" />
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/approve_button"
    android:text="Yeah it's Cool "/>
</LinearLayout>

Final App Look

1

There are 1 answers

8
eurosecom On BEST ANSWER

If your Event has more unapproved child, move verify_button.setOnClickListener from Holder to Adapter-populateViewHolder. You have to connect verify_button.setOnClickListener to row in recycleviewer to get values of title, desc and image from clicked item.

protected void populateViewHolder(RequestViewHolder viewHolder, Event model, int position) {
                viewHolder.setTitle(model.getTitle());
                viewHolder.setDesc(model.getDesc());
                viewHolder.setImage(getApplicationContext(), model.getImage());

            final String title = model.getTitle();
            final String desc = model.getDesc();
            final String image = model.getImage();

            final DatabaseReference aRef = FirebaseDatabase.getInstance().getReference();

            viewHolder.verify_button.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {

                    String key = aRef.child("ApprovedEvents").push().getKey();

                    Event event = new Event(title, desc, image );

                    Map<String, Object> eventValues = event.toMap();

                    Map<String, Object> childUpdates = new HashMap<>();

                    childUpdates.put("/ApprovedEvents/" + key, eventValues);

                    aRef.updateChildren(childUpdates);


                }
            });


        }
    };