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