无法看到我的Recyclerview Cardview

时间:2018-05-11 17:35:36

标签: android android-recyclerview android-adapter android-cardview

我似乎无法连接我的适配器。我正在尝试创建Recyclerview cardview。但是,它不会显示在模拟器上。我收到一条错误消息:

  

E/RecyclerView:没有附加适配器;跳过布局。

这是我设置适配器的地方。

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);
        }

    }

这是我的适配器类。

        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);
        }
    }
}

这是我从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;
    }
}

这是我的cardview布局文件

        <?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>

1 个答案:

答案 0 :(得分:1)

您没有正确设置列表项字段。

    @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.
    }

您应该将wrap_content用于列表项视图。如果你放match_parent,你的一个项目将覆盖所有屏幕。

<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:未附加适配器

这只是一个警告,你可以忽略这一点。