0

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();
}
}
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Arpan Sarkar
  • 189
  • 3
  • 14

2 Answers2

0

I believe this error happens because you haven't added your adapter to your recyclerview at the moment the view is rendered.

Have you tried to put this two lines after your recyclerview initialisation instead of inside your onResponse methode?

customCategoryList = new CustomCategoryList(getActivity(),categoryList);
recyclerView.setAdapter(customCategoryList);

then in your onResponse you would actually do:

customCategoryList.notifyDataSetChanged();

after you have added all the data to your list

Macmist
  • 713
  • 3
  • 11
  • The error is irrelevant. The RecyclerView redraws when the adapter is set. – TheWanderer Sep 28 '18 at 14:52
  • Thanks for reply.It worked but rows remain same as picture.overlap each other – Arpan Sarkar Sep 28 '18 at 14:54
  • Have you tried moving your cardview inside a framelayout that has padding? Because I'm not sure margin applies on layouts for elements in recyclerview. You could also try to add an item decoration to your recyclerview, see https://stackoverflow.com/questions/24618829/how-to-add-dividers-and-spaces-between-items-in-recyclerview – Macmist Sep 28 '18 at 15:32
0

Create this file.

CategoryListAdapter.java

public class CategoryListAdapter extends RecyclerView.Adapter<CategoryListAdapter.ItemHolder> {

    private List<Category> categoryList;
    private Context mContext;

    public CategoryListAdapter(List<Category> categoryList, Context mContext) {
        this.categoryList = categoryList;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);

        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ItemHolder holder, final int position) {

        // Get each category, one by one.
        Category singleCategory = categoryList.get(position);

        // Set the category content to the layout
        holder.name.setText(singleCategory.getName());
        // Do something with the image. You can load the url using GlideApp
        holder.imageView ... 


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


    // --------------------------------------------------------------------------------
    //                                    ViewHolder
    // --------------------------------------------------------------------------------
    public class ItemHolder extends RecyclerView.ViewHolder {

        public ImageView imageView;

        public TextView name;

        public ItemHolder(View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.image_view);

            name = itemView.findViewById(R.id.catname);

        }
    }

}

And finally set the recycler view adapter, (set it as Macmist answer)

mRecyclerView.setAdapter(new CategoryListAdapter(categoryList, getActivity()));
Soon Santos
  • 2,107
  • 22
  • 43