0

enter image description hereIm attempting a FireBase Horizontal RecyclerView (like an Netflix clone).

I have looked at Git and Android Arsenal to find programing similar, but had been unsuccessful finding anything with a firebase backend. I had played with the adapter and the database. I have work on this for a couple days without finding a solution.

public class RestaurantList extends AppCompatActivity {

    RecyclerView recyclerView1;
    RecyclerView recyclerView2;


    FirebaseRecyclerOptions<Restaurant> options = new FirebaseRecyclerOptions.Builder<Restaurant>()
            .setQuery(FirebaseDatabase.getInstance()
                            .getReference()
                            .child("Restaurants1")
                    ,Restaurant.class)
            .build();

    FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder> adapter = new FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder>(options) {

        @NonNull
        @Override
        public RestaurantViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.restaurant_item, parent, false);
            return new  RestaurantViewHolder(itemView);
        }

        @Override
        protected void onBindViewHolder(@NonNull  RestaurantViewHolder viewHolder, int position, @NonNull Restaurant model) {

            viewHolder.txt_restaurant_name.setText(model.getName());
            Picasso.get().load(model.getImage())
                    .into(viewHolder.img_restaurant);
            final Restaurant clickItem = model;
            viewHolder.setItemClickListener(new ItemClickListener() {
                @Override
                public void onClick(View view, int position, boolean isLongClick) {
                    Intent foodList = new Intent(RestaurantList.this, Home.class);
                    Common.restaurantSelected=adapter.getRef(position).getKey();
                    startActivity(foodList);
                }
            });

        }

    };

    FirebaseRecyclerOptions<Restaurant> options2 = new FirebaseRecyclerOptions.Builder<Restaurant>()
            .setQuery(FirebaseDatabase.getInstance()
                            .getReference()
                            .child("Restaurants2")
                    ,Restaurant.class)
            .build();

    FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder> adapter2= new FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder>(options2) {


        @NonNull
        @Override
        public RestaurantViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.restaurant_item, parent, false);
            return new  RestaurantViewHolder(itemView);
        }

        @Override
        protected void onBindViewHolder(@NonNull  RestaurantViewHolder viewHolder, int position, @NonNull Restaurant model) {

            viewHolder.txt_restaurant_name.setText(model.getName());
            Picasso.get().load(model.getImage())
                    .into(viewHolder.img_restaurant);
            final Restaurant clickItem = model;
            viewHolder.setItemClickListener(new ItemClickListener() {
                @Override
                public void onClick(View view, int position, boolean isLongClick) {
                    Intent foodList = new Intent(RestaurantList.this, Home.class);
                    Common.restaurantSelected=adapter2.getRef(position).getKey();
                    startActivity(foodList);
                }
            });

        }


    };

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


        RelativeLayout relativeLayout = findViewById(R.id.root2_layout);
        AnimationDrawable animationDrawable = (AnimationDrawable)relativeLayout.getBackground();
        animationDrawable.setEnterFadeDuration(5);
        animationDrawable.setExitFadeDuration(1000);
        animationDrawable.start();

        LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
        LinearLayoutManager layoutManager2 = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);

        recyclerView1 = (RecyclerView) findViewById(R.id.recycler_restaurant);

        recyclerView1.setLayoutManager(layoutManager);

        recyclerView2 = (RecyclerView) findViewById(R.id.recycler_restaurant2);
        recyclerView1.setLayoutManager(layoutManager2);

        loadAsianFood();
        loadSteakHouse();

    }

    private void loadSteakHouse() {
        adapter.startListening();
        recyclerView1.setAdapter(adapter);
        recyclerView1.getAdapter().notifyDataSetChanged();
        recyclerView1.scheduleLayoutAnimation();
    }

    private void loadAsianFood() {
        adapter2.startListening(); // Did you mean adapter2.startListening()?
        recyclerView2.setAdapter(adapter2);
        recyclerView2.getAdapter().notifyDataSetChanged();
        recyclerView2.scheduleLayoutAnimation();
    }

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
        adapter2.stopListening();;

    }
    @Override
    protected void onResume() {
        super.onResume();

        if (adapter != null)
            adapter.startListening();
        adapter2.startListening();

    }



}

It only runs the first category (Restaurant1) only.

Brian Duncan
  • 231
  • 1
  • 2
  • 10
  • 1
    And your question is ... ? https://stackoverflow.com/help/how-to-ask – dominicoder Aug 04 '19 at 04:18
  • Duplicate of https://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type – iamnaran Aug 04 '19 at 05:45
  • 1
    So what's your question? What's wrong with this code? Please also responde with @AlexMamo – Alex Mamo Aug 04 '19 at 10:06
  • @AlexMamo I am trying to populate RecyclerView2 with Restaurant2 in the firebase DB. – Brian Duncan Aug 04 '19 at 16:05
  • @iamnaran it is not a duplicate – Brian Duncan Aug 04 '19 at 16:06
  • @dominicoder i have updated my question. I am sorry for the confusion – Brian Duncan Aug 04 '19 at 16:07
  • 1
    You still have not asked a question. A question ends in a question mark ("?"). You have "attempted" something and "did not find a solution", but solution to what? What is your problem? – dominicoder Aug 04 '19 at 20:57
  • @dominicoder Okay you require a question mark to understand the problem? I am attempting to run 2 horizontal recyclerView 's containing the Restaurant1 in RecyclerView1 and Restaurant2 in RecyclerView2. Here is the question get ready Dom, How can I make that happen ???? because currently it is running Restaurant1 in both recyclerViews and that is not what I want to do. – Brian Duncan Aug 04 '19 at 22:24
  • 1
    Yes, given how english works, a _question_ ends with a _question mark_. Notice that you finally explained your issue in your comment whereas you did not in your actual question. I have attempted an answer. See below. – dominicoder Aug 05 '19 at 02:21

2 Answers2

0

A few problems I noticed, though I don't know if they'll solve your actual issue.

1 - You are calling startListening on the first adapter when it should be the second:

private void loadAsianFood() {
    adapter.startListening(); // Did you mean adapter2.startListening()?
    recyclerView2.setAdapter(adapter2);
    recyclerView2.getAdapter().notifyDataSetChanged();
    recyclerView2.scheduleLayoutAnimation();

}

2 - You are only stopping the first adapter, not the second.

@Override
protected void onStop() {
    super.onStop();
    adapter.stopListening();

}

3 - You are only resuming the first adapter, not the second:

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

    if (adapter != null)
        adapter.startListening();
}
dominicoder
  • 9,338
  • 1
  • 26
  • 32
  • Muchos gracious dude, but it didn't work. thanks for the adapter2, I didnt see that. – Brian Duncan Aug 05 '19 at 13:56
  • I figured it out. I was a combination of the adapter issue that I didn't see and the DBTree. Thanks for the help and your company, :"Reverb", looks great. – Brian Duncan Aug 05 '19 at 16:00
0

RecyclerView recyclerView1; RecyclerView recyclerView2;

FirebaseRecyclerOptions<Restaurant> options = new FirebaseRecyclerOptions.Builder<Restaurant>()
        .setQuery(FirebaseDatabase.getInstance()
                        .getReference("RestaurantCategory")
                        .child("01").child("SteakHouse")
                ,Restaurant.class)
        .build();

FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder> adapter = new FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder>(options) {

    @NonNull
    @Override
    public RestaurantViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.restaurant_item, parent, false);
        return new  RestaurantViewHolder(itemView);
    }

    @Override
    protected void onBindViewHolder(@NonNull  RestaurantViewHolder viewHolder, int position, @NonNull Restaurant model) {

        viewHolder.txt_restaurant_name.setText(model.getName());
        Picasso.get().load(model.getImage())
                .into(viewHolder.img_restaurant);
        final Restaurant clickItem = model;
        viewHolder.setItemClickListener(new ItemClickListener() {
            @Override
            public void onClick(View view, int position, boolean isLongClick) {
                Intent foodList = new Intent(RestaurantList.this, Home.class);
                Common.restaurantSelected=adapter.getRef(position).getKey();
                startActivity(foodList);
            }
        });

    }

};

FirebaseRecyclerOptions<Restaurant> options2 = new FirebaseRecyclerOptions.Builder<Restaurant>()
        .setQuery(FirebaseDatabase.getInstance()
                        .getReference("RestaurantCategory")
                        .child("02").child("AsianFood")
                ,Restaurant.class)
        .build();

FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder> adapter2= new FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder>(options2) {


    @NonNull
    @Override
    public RestaurantViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.restaurant_item, parent, false);
        return new  RestaurantViewHolder(itemView);
    }

    @Override
    protected void onBindViewHolder(@NonNull  RestaurantViewHolder viewHolder, int position, @NonNull Restaurant model) {

        viewHolder.txt_restaurant_name.setText(model.getName());
        Picasso.get().load(model.getImage())
                .into(viewHolder.img_restaurant);
        final Restaurant clickItem = model;
        viewHolder.setItemClickListener(new ItemClickListener() {
            @Override
            public void onClick(View view, int position, boolean isLongClick) {
                Intent foodList = new Intent(RestaurantList.this, Home.class);
                Common.restaurantSelected=adapter2.getRef(position).getKey();
                startActivity(foodList);
            }
        });

    }


};

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


    RelativeLayout relativeLayout = findViewById(R.id.root2_layout);
    AnimationDrawable animationDrawable = (AnimationDrawable)relativeLayout.getBackground();
    animationDrawable.setEnterFadeDuration(5);
    animationDrawable.setExitFadeDuration(1000);
    animationDrawable.start();

    LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
    LinearLayoutManager layoutManager2 = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);

    recyclerView1 = (RecyclerView) findViewById(R.id.recycler_restaurant);

    recyclerView1.setLayoutManager(layoutManager);

    recyclerView2 = (RecyclerView) findViewById(R.id.recycler_restaurant2);
    recyclerView1.setLayoutManager(layoutManager2);

    loadAsianFood();
    loadSteakHouse();

}

private void loadSteakHouse() {
    adapter.startListening();
    recyclerView1.setAdapter(adapter);
    recyclerView1.getAdapter().notifyDataSetChanged();
    recyclerView1.scheduleLayoutAnimation();
}

private void loadAsianFood() {
    adapter2.startListening(); // Did you mean adapter2.startListening()?
    recyclerView2.setAdapter(adapter2);
    recyclerView2.getAdapter().notifyDataSetChanged();
    recyclerView2.scheduleLayoutAnimation();
}

@Override
protected void onStop() {
    super.onStop();
    adapter.stopListening();
    adapter2.stopListening();;

}
@Override
protected void onResume() {
    super.onResume();

    if (adapter != null)
        adapter.startListening();
    adapter2.startListening();

}

} enter image description here

Brian Duncan
  • 231
  • 1
  • 2
  • 10