0

It's not showing the default data I set in restoList. I was just trying to follow the Recycler View Example from https://www.androidhive.info/2016/01/android-working-with-recycler-view/ I think my code is somewhat identical now but it still doesn't work.

package com.example.jmcervantes02.jan_24;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import java.util.ArrayList;

public class Recycler_Activity extends AppCompatActivity {

    private ArrayList<RestaurantModel> restoList = new ArrayList<>();
    private RecyclerView recyclerView;
    private RestaurantAdapter restoAdapter;

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

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

        restoAdapter = new RestaurantAdapter(restoList);
        RecyclerView.LayoutManager rLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(rLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(restoAdapter);

        this.createDefaultRestaurants();
    }

    public void createDefaultRestaurants(){
        ArrayList<RestaurantModel> restoList = new ArrayList<RestaurantModel>();

        restoList.add(new RestaurantModel("MacDonnels", "Indian McDonalds", 5));
        restoList.add(new RestaurantModel("Burger MachKing", "Ultimate Street Burger", 6));
        restoList.add(new RestaurantModel("SubwayDog", "Subway pero hotdog", 2));
        restoList.add(new RestaurantModel("Mr. Shrooms", "May contain hallucinogens", 20));
        restoList.add(new RestaurantModel("Jomboys Bagnet", "Oilssss", 7));
        restoList.add(new RestaurantModel("Slamma Jamma Crackas", "Skyflakes pero cool", 5));
        restoList.add(new RestaurantModel("Ham n Cheese ice cream", "WHAT?", 1));
        restoList.add(new RestaurantModel("Turks", "Best shawarma in the world", 15));
        restoList.add(new RestaurantModel("MINISTOP", "FRIED CHICKEN IS LIFE", 12));
        restoList.add(new RestaurantModel("Brodie's Bro House", "Food forever", 5));
        restoList.add(new RestaurantModel("Vikings", "Buffet", 6));
        restoAdapter.notifyDataSetChanged();

        for(int i = 0; i < restoList.size(); i++){
            String restLog = restoList.get(i).getName() + ", " + restoList.get(i).getDescription() + ", " + restoList.get(i).getWeight();
            Log.v("Restaurant", restLog);
        }

        String count = String.valueOf(restoAdapter.getItemCount());
        Log.v("Count", count);

    }

}

Adapter Class

package com.example.jmcervantes02.jan_24;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import junit.framework.Test;

import java.util.ArrayList;

public class RestaurantAdapter extends RecyclerView.Adapter<RestaurantAdapter.MyViewHolder> {
    private ArrayList<RestaurantModel> restoList;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView name, desc, weight;

        public MyViewHolder(View view){
            super(view);
            name = (TextView)view.findViewById(R.id.name_text);
            desc = (TextView)view.findViewById(R.id.desc_text);
            weight = (TextView)view.findViewById(R.id.weight_text);

        }
    }

    public RestaurantAdapter(ArrayList<RestaurantModel>restoList){
        this.restoList = restoList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_view, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position){
        RestaurantModel resto = restoList.get(position);
        holder.name.setText(resto.getName());
        holder.desc.setText(resto.getDescription());
        holder.weight.setText(resto.getWeight());
    }

    @Override
    public int getItemCount(){
        return restoList.size();
    }

}

The xml for the main view and the recycler items are also identical to the example

Gabby Cervantes
  • 85
  • 2
  • 10
  • Just remove this line `ArrayList restoList = new ArrayList();` from **createDefaultRestaurants()** method.It may work. – Patel Dhara Feb 13 '18 at 07:30

5 Answers5

0

Change code like flowing flow

 ArrayList<RestaurantModel> restoList; // declare global

call adapter after setting values

    this.createDefaultRestaurants();
    restoAdapter = new RestaurantAdapter(restoList);
    RecyclerView.LayoutManager rLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(rLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(restoAdapter);

your method like

   public void createDefaultRestaurants(){
    restoList= new ArrayList<RestaurantModel>();
    restoList.add(new RestaurantModel("MacDonnels", "Indian McDonalds", 5));
    restoList.add(new RestaurantModel("Burger MachKing", "Ultimate Street Burger", 6));
    restoList.add(new RestaurantModel("SubwayDog", "Subway pero hotdog", 2));
    restoList.add(new RestaurantModel("Mr. Shrooms", "May contain hallucinogens", 20));
    restoList.add(new RestaurantModel("Jomboys Bagnet", "Oilssss", 7));
    restoList.add(new RestaurantModel("Slamma Jamma Crackas", "Skyflakes pero cool", 5));
    restoList.add(new RestaurantModel("Ham n Cheese ice cream", "WHAT?", 1));
    restoList.add(new RestaurantModel("Turks", "Best shawarma in the world", 15));
    restoList.add(new RestaurantModel("MINISTOP", "FRIED CHICKEN IS LIFE", 12));
    restoList.add(new RestaurantModel("Brodie's Bro House", "Food forever", 5));
    restoList.add(new RestaurantModel("Vikings", "Buffet", 6));
    restoAdapter.notifyDataSetChanged();

    for(int i = 0; i < restoList.size(); i++){
        String restLog = restoList.get(i).getName() + ", " + restoList.get(i).getDescription() + ", " + restoList.get(i).getWeight();
        Log.v("Restaurant", restLog);
    }

    String count = String.valueOf(restoAdapter.getItemCount());
    Log.v("Count", count);

}
sasikumar
  • 12,540
  • 3
  • 28
  • 48
  • no need to change order of method calling this.createDefaultRestaurants(); as restoAdapter.notifyDataSetChanged(); is called in the end. – Aj 27 Feb 13 '18 at 06:31
0

In your Recycler_Activity.java According to your code first you are calling Adapter Constructor then your are setting data to your POJO/Model class, Set data to POJO before calling Adapter.

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

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

    this.createDefaultRestaurants();

    restoAdapter = new RestaurantAdapter(restoList);
    RecyclerView.LayoutManager rLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(rLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(restoAdapter);


}
Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44
  • no need to change order of method calling this.createDefaultRestaurants(); as restoAdapter.notifyDataSetChanged(); is called in the end. – Aj 27 Feb 13 '18 at 06:33
0

The reference to the global arraylist

private ArrayList<RestaurantModel> restoList = new ArrayList<>();

and local arraylist are different

remove private ArrayList<RestaurantModel> restoList = new ArrayList<>(); from createDefaultRestaurants like this:

public void createDefaultRestaurants(){
        restoList = new ArrayList<>();
        restoList.add(new RestaurantModel("MacDonnels", "Indian McDonalds", 5));
        restoList.add(new RestaurantModel("Burger MachKing", "Ultimate Street Burger", 6));
        restoList.add(new RestaurantModel("SubwayDog", "Subway pero hotdog", 2));
        restoList.add(new RestaurantModel("Mr. Shrooms", "May contain hallucinogens", 20));
        restoList.add(new RestaurantModel("Jomboys Bagnet", "Oilssss", 7));
        restoList.add(new RestaurantModel("Slamma Jamma Crackas", "Skyflakes pero cool", 5));
        restoList.add(new RestaurantModel("Ham n Cheese ice cream", "WHAT?", 1));
        restoList.add(new RestaurantModel("Turks", "Best shawarma in the world", 15));
        restoList.add(new RestaurantModel("MINISTOP", "FRIED CHICKEN IS LIFE", 12));
        restoList.add(new RestaurantModel("Brodie's Bro House", "Food forever", 5));
        restoList.add(new RestaurantModel("Vikings", "Buffet", 6));
        restoAdapter.notifyDataSetChanged();

        for(int i = 0; i < restoList.size(); i++){
            String restLog = restoList.get(i).getName() + ", " + restoList.get(i).getDescription() + ", " + restoList.get(i).getWeight();
            Log.v("Restaurant", restLog);
        }

        String count = String.valueOf(restoAdapter.getItemCount());
        Log.v("Count", count);

    }
Aj 27
  • 2,316
  • 21
  • 29
0

no need to recreate an object of ArrayList<RestaurantModel> restoList in the function createDefaultRestaurants() just clear list and reassign will work

public void createDefaultRestaurants(){
    restoList.clear();
    restoList = new ArrayList<>();
    restoList.add(new RestaurantModel("MacDonnels", "Indian McDonalds", 5));
    restoList.add(new RestaurantModel("Burger MachKing", "Ultimate Street Burger", 6));
   //
   // added model to list
   //
    restoList.add(new RestaurantModel("Vikings", "Buffet", 6));
    restoAdapter.notifyDataSetChanged();

    for(int i = 0; i < restoList.size(); i++){
        String restLog = restoList.get(i).getName() + ", " + restoList.get(i).getDescription() + ", " + restoList.get(i).getWeight();
        Log.v("Restaurant", restLog);
    }

    String count = String.valueOf(restoAdapter.getItemCount());
    Log.v("Count", count);

}
Upendra Shah
  • 2,218
  • 17
  • 27
0

You need to supply the new list to adapter and then call restoAdapter.notifyDataSetChanged(); without supplying the new list to the adapter, notifyDataSetChanged will do nothing.

add a function in the adapter which takes list as parameter and puts it in the current list of adapter, then call notifydatasetchanged.

example : make this function in your adatper

public void refreshList(List<Resto> restoList){
  this.restoList = restoList;
  notifyDataSetChanged();
}

instead of restoAdapter.notifyDataSetChanged(); , call restoAdapter.refreshList(restoList);

Mohit
  • 134
  • 7