In my app I am using recycler view in fragment. That recycler view data comes from database. I get all data perfectly but all data overlap each other and getting an error in log cat.I am using card view to create each row of recycler view and use volley library to fetch data.
fragment.java:
public class HomeFragment extends Fragment {
private static final String CATEGORY_URL = "http://192.168.0.101/cart/category/get_all_category.php";
List<Category> categoryList;
RecyclerView recyclerView;
CustomCategoryList customCategoryList;
public HomeFragment() {
// Required empty public constructor
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_home, container,false);
//getting the recyclerview from xml
recyclerView = (RecyclerView) rootView.findViewById(R.id.recylcerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
recyclerView.setHasFixedSize(true);
categoryList = new ArrayList<>();
loadCategory();
//customCategoryList = new CustomCategoryList(getActivity(),categoryList);
return rootView;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
private void loadCategory(){
StringRequest stringRequest= new StringRequest(Request.Method.GET, CATEGORY_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
JSONArray array = obj.getJSONArray("category");
for (int i = 0; i < array.length(); i++){
//getting the user from the response
JSONObject userJson = array.getJSONObject(i);
categoryList.add(new Category(
userJson.getInt("categoryid"),
userJson.getString("categoryname")
));
}
customCategoryList = new CustomCategoryList(getActivity(),categoryList);
recyclerView.setAdapter(customCategoryList);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
Volley.newRequestQueue(getActivity()).add(stringRequest);
}
}
fragment.xml:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CATEGORIES : -"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/blue"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recylcerView"
android:layout_below="@+id/header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/activity_horizontal_margin">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
row.xml:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:orientation="vertical"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="5dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/top_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:layout_width="100dp"
android:layout_height="90dp"
android:layout_gravity="center_vertical"
android:src="@drawable/home_appliances"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/catname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HOME DECORE"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="@color/black"
android:padding="@dimen/nav_header_vertical_spacing"/>
</RelativeLayout>
</LinearLayout>
Error:
ERROR: 09-28 20:08:27.991 19249-19249/com.example.arpan.e_cart E/RecyclerView: No adapter attached; skipping layout
adapter class below:
public class CustomCategoryList extends RecyclerView.Adapter<CustomCategoryList.ViewHolder>{
private Context mCtx;
private List<Category> categoryList;
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView category;
public ViewHolder (View view) {
super(view);
category = view.findViewById(R.id.catname);
}
}
public CustomCategoryList(Context mCtx, List<Category> categoryList) {
this.mCtx = mCtx;
this.categoryList = categoryList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.custom_category_list,null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Category category = categoryList.get(position);
holder.category.setText(category.getCategoryname());
}
@Override
public int getItemCount() {
return categoryList.size();
}
}