0
To produce a shopping mall in my application I made a recyclerview in my main menu to demonstrate all the products that are in the database. I am trying to connect the field known as "Category" to "Carcategoryid" but when trying to do this the application ends up starting with a white page and with the error shown in the log. Does anyone know how I can solve this problem?
Log
E/RecyclerView: No adapter attached; skipping layout
Home Activity
package com.cars.evd.dealership.main;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Adapter;
import android.widget.TextView;
import android.widget.Toast;
import com.cars.evd.dealership.main.Common.Common;
import com.cars.evd.dealership.main.Interface.ItemClickListener;
import com.cars.evd.dealership.main.Model.Category;
import com.cars.evd.dealership.main.ViewHolder.CarViewHolder;
import com.cars.evd.dealership.main.ViewHolder.DealershipViewHolder;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
public class Home extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
FirebaseDatabase database;
DatabaseReference category;
TextView txtFullName;
//Load Menu
RecyclerView recycler_menu;
RecyclerView.LayoutManager layoutManager;
//Firebase Adapter
FirebaseRecyclerAdapter<Category,CarViewHolder> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Menu");
setSupportActionBar(toolbar);
//Initialise Firebase
database = FirebaseDatabase.getInstance();
category = database.getReference("Category");
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//Set a name for the user
View headerView = navigationView.getHeaderView(0);
txtFullName = (TextView)headerView.findViewById(R.id.txtFullName);
txtFullName.setText(Common.currentuser.getName());
//Menu Load
recycler_menu = (RecyclerView)findViewById(R.id.recycler_menu);
recycler_menu.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recycler_menu.setLayoutManager(layoutManager);
loadMenu();
}
private void loadMenu() {
new FirebaseRecyclerAdapter<Category, DealershipViewHolder>(Category.class, R.layout.car_selection, DealershipViewHolder.class,category) {
@Override
protected void populateViewHolder(DealershipViewHolder viewHolder, Category model, int position) {
viewHolder.txtMenuName.setText(model.getName());
Picasso.with(getBaseContext()).load(model.getImage()).into(viewHolder.imageView);
final Category clickItem = model;
viewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
//Toast.makeText(Home.this,""+clickItem.getName(),Toast.LENGTH_SHORT).show();
Intent CarList = new Intent(Home.this,CarList.class);
//Relate ID as part of a relationship between cars and dealerships
CarList.putExtra("CarCategoryID",adapter.getRef(position).getKey());
startActivity(CarList);
}
});
}
};
recycler_menu.setAdapter(adapter);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_menu) {
// Handle the camera action
} else if (id == R.id.nav_cart) {
} else if (id == R.id.nav_orders) {
} else if (id == R.id.nav_log_out) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Carlist
package com.cars.evd.dealership.main;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.cars.evd.dealership.main.Interface.ItemClickListener;
import com.cars.evd.dealership.main.Model.Cars;
import com.cars.evd.dealership.main.Model.Category;
import com.cars.evd.dealership.main.ViewHolder.CarViewHolder;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
public class CarList extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
FirebaseDatabase database;
DatabaseReference carList;
String CarCategoryID = "";
FirebaseRecyclerAdapter<Cars, CarViewHolder> adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_car_list);
//FIrebase
database= FirebaseDatabase.getInstance();
carList = database.getReference("Cars");
recyclerView = (RecyclerView)findViewById(R.id.recycler_cars);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
//Get intent here
if(getIntent() != null)
CarCategoryID = getIntent().getStringExtra("CarCategoryID");
if(!CarCategoryID.isEmpty() && CarCategoryID !=null){
loadCarsList(CarCategoryID);
}
}
private void loadCarsList(String CategoryId) {
adapter = new FirebaseRecyclerAdapter<Cars, CarViewHolder>(Cars.class
,R.layout.car_item,CarViewHolder.class,
carList.orderByChild("CarCategoryID").equalTo(CategoryId)) {
@Override
protected void populateViewHolder(CarViewHolder viewHolder, Cars model, int position) {
viewHolder.Carname.setText(model.getName());
Picasso.with(getBaseContext()).load(model.getImage())
.into(viewHolder.Car_image);
final Cars local = model;
viewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
//start new activity
Toast.makeText(CarList.this,local.getName(),Toast.LENGTH_SHORT).show();
}
});
}
};
Log.d("TAG",""+adapter.getItemCount());
//set adapter
recyclerView.setAdapter(adapter);
}
}
Carviewholder
package com.cars.evd.dealership.main.ViewHolder;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.cars.evd.dealership.main.Interface.ItemClickListener;
import com.cars.evd.dealership.main.R;
public class CarViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView Carname;
public ImageView Car_image;
private ItemClickListener itemClickListener;
public CarViewHolder(View itemView) {
super(itemView);
Carname = (TextView)itemView.findViewById(R.id.Carname);
Car_image = (ImageView)itemView.findViewById(R.id.Car_image);
itemView.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
@Override
public void onClick(View view) {
itemClickListener.onClick(view,getAdapterPosition(),false);
}
}
Dealershipviewholder
package com.cars.evd.dealership.main.ViewHolder;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.cars.evd.dealership.main.Interface.ItemClickListener;
import com.cars.evd.dealership.main.R;
public class DealershipViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txtMenuName;
public ImageView imageView;
private ItemClickListener itemClickListener;
public DealershipViewHolder(View itemView) {
super(itemView);
txtMenuName = (TextView)itemView.findViewById(R.id.dealership_name);
imageView = (ImageView)itemView.findViewById(R.id.menu_image);
itemView.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
@Override
public void onClick(View view){
itemClickListener.onClick(view,getAdapterPosition(),false);
}
}
Category
package com.cars.evd.dealership.main.Model;
import java.util.jar.Attributes;
public class Category {
private String Name;
private String Image;
public Category() {
}
public Category(String name, String image) {
Name = name;
Image = image;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
}
Cars
package com.cars.evd.dealership.main.Model;
import org.hamcrest.Description;
public class Cars {
private String Name, Image,Description, CarImage,CarPrice,Type, Doors,CarCategoryID;
public Cars(String name) {
Name = name;
}
public Cars(String name, String image, String description, String carImage, String carPrice, String type, String doors, String carCategoryID) {
Name = name;
Image = image;
Description = description;
CarImage = carImage;
CarPrice = carPrice;
Type = type;
Doors = doors;
CarCategoryID = carCategoryID;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public String getCarImage() {
return CarImage;
}
public void setCarImage(String carImage) {
CarImage = carImage;
}
public String getCarPrice() {
return CarPrice;
}
public void setCarPrice(String carPrice) {
CarPrice = carPrice;
}
public String getType() {
return Type;
}
public void setType(String type) {
Type = type;
}
public String getDoors() {
return Doors;
}
public void setDoors(String doors) {
Doors = doors;
}
public String getCarCategoryID() {
return CarCategoryID;
}
public void setCarCategoryID(String carCategoryID) {
CarCategoryID = carCategoryID;
}
}
Car_item XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app ="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardElevation="4dp"
android:layout_marginBottom="8dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/Carname"
android:scaleType="centerCrop"
android:src="@drawable/background"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/Car_image"
android:text="@string/name_of_car"
android:textColor="@android:color/white"
android:background="#4f0e0d0e"
android:textSize="20sp"
android:gravity="center"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Car_selection XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app ="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardElevation="4dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/menu_image"
android:scaleType="centerCrop"
android:src="@drawable/background"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/dealership_name"
android:text="@string/name_of_car"
android:textColor="@android:color/white"
android:background="#4f0e0d0e"
android:textSize="20sp"
android:gravity="center"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.CardView>
"but trying to do this the application ends up starting with a white page" - so if you don’t, the application shows the results?
– Rosário Pereira Fernandes
If you choose to demonstrate the categories yes, I can demonstrate them without problem. But when I try to implement a code that links the "Category" with the "Carcategoryid" it ends up failing and does not demonstrate the desired list.
– Rui