android – Redirect Page to a No Results Activity when data is no present in Firebase Database while using Firebase Recycler Adapter-ThrowExceptions

Exception or error:

There are various categories of events given and each category is selected based on which button you click. When a respective button is clicked, for that category, data is called from the Firebase Database in the form of a Recycler View, however if that category is not present in my database, I want show a NoResults Page.

I tried using an if-else condition however it is not working

Categories_Frag.java

public class Categories_Frag extends Fragment implements View.OnClickListener {
    ImageView arch, art, business, literature, science, tech;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.categories2, container, false);
        arch = (ImageView) view.findViewById(R.id.arch);
        art = (ImageView) view.findViewById(R.id.artImg);
        business = (ImageView) view.findViewById(R.id.businessImg);
        literature = (ImageView) view.findViewById(R.id.literatureImg);
        science = (ImageView) view.findViewById(R.id.scienceImg);
        tech = (ImageView) view.findViewById(R.id.techImg);

        arch.setOnClickListener(this);
        art.setOnClickListener(this);
        business.setOnClickListener(this);
        literature.setOnClickListener(this);
        science.setOnClickListener(this);
        tech.setOnClickListener(this);
        return view;
    }
    public void onClick(View view){

        Intent i = new Intent(getActivity(), EventView.class);

        switch(view.getId()) {
            case R.id.arch:
                String architecture = "Architecture";
                i.putExtra("Category", architecture);
                break;
            case R.id.artImg:
                String art = "Art";
                i.putExtra("Category", art);
                break;
            case R.id.businessImg:
                String business = "Business";
                i.putExtra("Category", business);
                break;
            case R.id.literatureImg:
                String literature = "Literature";
                i.putExtra("Category", literature);
                break;
            case R.id.scienceImg:
                String science = "Science";
                i.putExtra("Category", science);
                break;
            case R.id.techImg:
                String technology = "Technology";
                i.putExtra("Category", technology);
                break;
        }
        startActivity(i);
    }
}

EventView.java

public class EventView extends AppCompatActivity {

    DatabaseReference databaseReference;

    RecyclerView recyclerView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_view);

        databaseReference = FirebaseDatabase.getInstance().getReference().child("ApprovedEvents");

        recyclerView = (RecyclerView) findViewById(R.id.request_EventList);

        //Avoid unnecessary layout passes by setting setHasFixedSize to true
        recyclerView.setHasFixedSize(true);

        //Select the type of layout manager you would use for your recyclerView
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void onStart() {
        super.onStart();
        final String event_cat = getIntent().getStringExtra("Category");
        ActionBar actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.setTitle(event_cat);
        getSupportActionBar().setTitle(event_cat);

        databaseReference.orderByChild("category").equalTo(event_cat).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if(dataSnapshot !=null){
                    startShowing();
                }
                else{
                    Intent i = new Intent(getApplicationContext(), NoResults.class);
                    startActivity(i);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

    private void startShowing() {
        final String event_cat = getIntent().getStringExtra("Category");
        FirebaseRecyclerAdapter<Event, EventView.RequestViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Event, EventView.RequestViewHolder>(
                Event.class,
                R.layout.events_list_row,
                EventView.RequestViewHolder.class,
                databaseReference.orderByChild("category").equalTo(event_cat)

        ) {
            @Override
            protected void populateViewHolder(EventView.RequestViewHolder viewHolder, Event model, int position) {


                viewHolder.setTitle(model.getTitle());
                viewHolder.setCategory(model.getCategory());
                viewHolder.setLocation(model.getLocation());
                viewHolder.setPrice(model.getPrice());
                viewHolder.setImageUrl(getApplicationContext(), model.getImageUrl());

                viewHolder.setEvent_user_image(getApplicationContext(), model.getEvent_user_image());
                viewHolder.setEvent_username(model.getEvent_username());


                viewHolder.imageButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(EventView.this, EventDetails.class);

                        startActivity(intent);
                    }
                });
            }
        };
        recyclerView.setAdapter(firebaseRecyclerAdapter);
    }

    public static class RequestViewHolder extends RecyclerView.ViewHolder {
        View mView;
        ImageView imageButton;

        public RequestViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            imageButton = (ImageView) mView.findViewById(R.id.request_image);
        }

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

        public void setLocation(String location) {
            TextView a_desc = (TextView) mView.findViewById(R.id.request_location);
            a_desc.setText(location);
        }

        public void setCategory(String category) {
            TextView a_category = (TextView) mView.findViewById(R.id.request_category);
            a_category.setText(category);
        }

        public void setPrice(String price) {
            TextView a_price = (TextView) mView.findViewById(R.id.request_price);
            a_price.setText(price);
        }

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

        public void setEvent_username(String event_username) {
            TextView a_event_username = (TextView) mView.findViewById(R.id.userName);
            a_event_username.setText(event_username);
        }

        public void setEvent_user_image(Context ctx, String event_user_image) {
            CircleImageView a_event_user_image = (CircleImageView) mView.findViewById(R.id.event_user_image);
            Picasso.with(ctx).load(event_user_image).into(a_event_user_image);
        }
    }
}

NoResults.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class NoResults extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_no_results);
    }
}

activity_no_results.xml

<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:src="@drawable/noresults">

</ImageView>
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *