Willie Redmond
Willie Redmond

Reputation: 9

Cant see my Recyclerview Cardview

I can't seem to get my adapter to attach. I'm trying to create a Recyclerview cardview. However, it will not display on the emulator. Ive get an error message:

E/RecyclerView: No adapter attached; skipping layout.

Here is where I set the adapter.

madapter = new CardAdapter( itemList, CardviewRecycler.this );
recyclerView.setAdapter(madapter); 



          public class CardviewRecycler extends AppCompatActivity {
        // URL string to call data from myPHPAdmin localhost database.
        private static final String URL_ITEMLIST = "http://192.168.240.1/ImageJsonData.php";

        public RecyclerView recyclerView;
        //private RecyclerView.Adapter adapter;
        private  CardAdapter madapter;
        public List<ItemList> itemList;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_cardviewrecycler);

            // Initializing ItemList Array.
            itemList = new ArrayList<>();


            //Calling the Recyclerview
            recyclerView = findViewById(R.id.recyclerview);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));


            //Loading the RecyclerView data.
            loadItemListData();
        }

        public void loadItemListData(){

            StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_ITEMLIST,
                    new Response.Listener<String>() {

                        @Override
                        public void onResponse(String response) {
                            try {
                                //converting the string to json array object
                                JSONArray array = new JSONArray(response);

                                //traversing through all the objects
                                for (int i = 0; i < array.length(); i++) {

                                    //getting item object from json array
                                    JSONObject itemobj = array.getJSONObject(i);

                                    int id = itemobj.getInt("id");
                                    String image_title = itemobj.getString("image_title");
                                    String image_url = itemobj.getString("image_url");

                                    ItemList item = new ItemList(id, image_title, image_url);
                                    itemList.add(item);
                                }

                                //creating adapter object and setting it to recyclerview
                                madapter = new CardAdapter( itemList, CardviewRecycler.this );
                                recyclerView.setAdapter(madapter);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    });

            //adding our stringrequest to queue
            Volley.newRequestQueue(this).add(stringRequest);
        }

    }

Here is where my Adapter Class.

        public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
    //Bringing in the ItemList java Class.
    private List<ItemList> itemList;
    private Context context;

    public CardAdapter(List <ItemList> itemList, Context context) {
        this.itemList = itemList;
        this.context = context;
    }

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

        //Inflating the cardview.xml from the LayoutInflator parent Context.
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.cardview, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        // To bind the itemList data with the UI elements from the cardview.xml written in
        //the ViewHolder subclass. The UI elements are inside the holder object.
        ItemList itemlist = itemList.get(position);

        //The holder object is used to bind the cardview.xml UI element to the itemList data.
        holder.ImageTitleTextView.setText(itemlist.getImage());

        //The holder object is binding the cardview.xml  UI elementimageView to the itemList
        // data using the Glide dependecy in the build.gradle file.
        Glide.with(context)
             .load(itemlist.getImage())
             .into((ImageView) holder.itemView);
    }

    @Override
    public int getItemCount() {

        //itemList.size to show the items in the Array list.
        return itemList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        //Creating UI elements from the cardview.xml
        TextView ImageTitleTextView;
        ImageView VollyImageView;

        ViewHolder(View itemView) {
            super(itemView);

            //Initializing the UI elements above to Views(itemView)
            ImageTitleTextView = itemView.findViewById(R.id.ImageNameTextView);
            VollyImageView = itemView.findViewById(R.id.VolleyImageView);
        }
    }
}

Here is my Item List data that Im pulling in from myPhPadmin

        public class ItemList
{
    private int id;
    private String image_url;
    private String image_title;

    ItemList(int id, String image_url, String image_title) {
        this.id = id;
        this.image_url = image_url;
        this.image_title = image_title;
    }

    public int getId() {
        return id;
    }

    public String getImage() {
        return image_url;
    }

    public String getTitle() {
        return image_title;
    }
}

and this is my cardview layout file

        <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="5dp"
card_view:contentPadding="5dp"
card_view:cardCornerRadius="5dp"
card_view:cardMaxElevation="5dp">

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ECEFF1">

    <ImageView
        android:id="@+id/VolleyImageView"
        android:layout_width="150dp"
        android:layout_height="100dp"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:scaleType="center"
        android:src="@mipmap/ic_launcher"
        android:contentDescription="TODO" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/VolleyImageView"
        android:layout_toEndOf="@+id/VolleyImageView"
        android:layout_marginLeft="20dp"
        android:layout_marginStart="20dp"
        android:textSize="20sp"
        android:textColor="#000"
        android:text="@string/JSonImage"
        android:id="@+id/ImageNameTextView"
        android:layout_centerVertical="true"/>

</RelativeLayout>

</android.support.v7.widget.CardView>
</LinearLayout>

Upvotes: 0

Views: 409

Answers (1)

Khemraj Sharma
Khemraj Sharma

Reputation: 58984

You are not setting list item fields properly.

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ItemList itemlist = itemList.get(position);
        holder.ImageTitleTextView.setText(itemlist.getTitle()); // you were setting getImage here 
        Glide.with(context)
                .load(itemlist.getImage())
                .into(holder.VollyImageView); // you were setting image on itemView which is not an ImageView.
    }

And you should use wrap_content for list item views. If you put match_parent, your one item will cover all screen.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" //changed this
    android:orientation="vertical">

E/RecyclerView: No adapter attached

This is just a warning, you can ignore this.

Upvotes: 1

Related Questions