My application is not producing the information that it has been requested in the java files. It was hoped that the app would produce a recycler view of information that is being stored in a phpMyAdmin database.
However, all I am getting is a blank/empty activity.
The LogCat is producing an error that is to do with the RecyclerView, however, I am not sure how to get around this error/know if this is the error that is causing the blank activity.
LogCat
02-07 11:28:55.936 5622-5622/com.example.xxxx.myapplication E/RecyclerView: No adapter attached; skipping layout
Adapter
package com.example.benchalmers.myapplication;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {
private Context mCtx;
private List<Product> productList;
public ProductAdapter(Context mCtx, List<Product> productList) {
this.mCtx = mCtx;
this.productList = productList;
}
@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.list_layout, null);
return new ProductViewHolder(view);
}
@Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
Product product = productList.get(position);
holder.textViewType.setText(product.getType());
holder.textViewProperty.setText(product.getProperty());
holder.textViewMoreInfo.setText(String.valueOf(product.getMoreInfo()));
holder.textViewDate.setText(String.valueOf(product.getDate()));
//holder.imageView.setImageDrawable(mCtx.getResources().getDrawable(product.getImage()));
}
@Override
public int getItemCount() {
return productList.size();
}
class ProductViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textViewType, textViewProperty, textViewMoreInfo, textViewDate;
public ProductViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
textViewType = itemView.findViewById(R.id.textViewType);
textViewProperty = itemView.findViewById(R.id.textViewProperty);
textViewMoreInfo = itemView.findViewById(R.id.textViewMoreInfo);
textViewDate = itemView.findViewById(R.id.textViewDate);
}
}
}
Activity
package com.example.benchalmers.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class Maintenance extends AppCompatActivity {
private static final String PRODUCT_URL = "http://192.168.64.2/MyApi/api.php";
RecyclerView recyclerView;
private ProductAdapter adapter;
List<Product> productList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maintenance);
productList = new ArrayList<>();
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
this.adapter = new ProductAdapter(this, productList);
recyclerView.setAdapter(adapter);
loadProducts ();
}
private void loadProducts () {
StringRequest stringRequest = new StringRequest(Request.Method.GET, PRODUCT_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray products =new JSONArray(response);
for (int i = 0; i<products.length(); i++){
JSONObject productObject = products.getJSONObject(i);
int maintenance_id = productObject.getInt("maintenance_id");
String type = productObject.getString("type");
String property = productObject.getString("property");
String more_info = productObject.getString("more_info");
Double date = productObject.getDouble("date");
Product product = new Product(maintenance_id, type, property, more_info, date);
productList.add(product);
}
adapter = new ProductAdapter(Maintenance.this, productList);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Maintenance.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
Volley.newRequestQueue(this).add(stringRequest);
}
}
I have not attached any Java files at this stage, as I do not believe that they are the issue here.
I have looked at various threads on Stack, but none of which seem to do the job.
Any guidance would be appreciated!