由于任何错误,CardView无法显示内容

时间:2016-07-23 08:49:36

标签: java android

我在main_activity中创建了一个Recyclerview,我希望在部分活动中使用此cardview ...我将额外的变量和数据添加到新活动中,但任何未显示在新活动中的内容......

这是我的部分xml代码(hotel_page.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view1"
        android:layout_width="match_parent"
        android:layout_height="288dp"
        android:layout_margin="@dimen/card_margin"
        android:elevation="3dp"
        card_view:cardCornerRadius="0dp"
        android:gravity="top">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/card"
            android:focusable="true"
            android:contextClickable="true"
            android:gravity="top"
            android:layout_alignParentBottom="true">

        </RelativeLayout>

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

    <TextView
        android:id="@+id/title1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="25dp"
        android:paddingTop="10dp"
        android:textColor="@color/cardview_dark_background"
        android:textSize="15dp"
        android:layout_above="@+id/count1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageView
        android:id="@+id/thumbnail1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:clickable="true"
        android:scaleType="fitXY"
        android:contextClickable="true"
        android:layout_alignTop="@+id/title1" />

    <TextView
        android:id="@+id/count1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp"
        android:paddingRight="25dp"
        android:textSize="12dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</LinearLayout>

这是我的HotelPageList.java:

    package ir.homa;
public class HotelPageList {
    private String name;
    private int numOfRooms;
    private int thumbnail;


    public HotelPageList() {
    }

    public HotelPageList(String name, int numOfRooms, int thumbnail) {
        this.name = name;
        this.numOfRooms = numOfRooms;
        this.thumbnail = thumbnail;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNumOfRooms() {
        return numOfRooms;
    }

    public void setNumOfRooms(int numOfRooms) {
        this.numOfRooms = numOfRooms;
    }

    public int getThumbnail() {
        return thumbnail;
    }

    public void setThumbnail(int thumbnail) {
        this.thumbnail = thumbnail;
    }


}

这是HotelPageAdapter.java:

package ir.homa;

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 com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by SMQ on 7/20/2016.
 */
public class HotelPageAdapter extends RecyclerView.Adapter<HotelPageAdapter.MyViewHolder> {

    private Context mContext;
    private List<HotelPageList> hotelPage;





    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView title1, count1;
        public ImageView thumbnail1, overflow1;

        public MyViewHolder(View view) {
            super(view);

            title1 = (TextView) view.findViewById(R.id.title1);
            count1 = (TextView) view.findViewById(R.id.count1);
            thumbnail1 = (ImageView) view.findViewById(R.id.thumbnail1);
            overflow1 = (ImageView) view.findViewById(R.id.overflow);
        }
    }
    public HotelPageAdapter(Context mContext, List<HotelPageList> hotelPage) {
        this.mContext = mContext;
        this.hotelPage = hotelPage;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.hotel_page, parent, false);
        return new MyViewHolder(itemView);

    }

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



        final HotelPageList hotel = hotelPage.get(position);
        holder.title1.setText(hotel.getName());
        holder.count1.setText(hotel.getNumOfRooms() + " اتاق");

        // loading hotel cover using Glide library
        Glide.with(mContext).load(hotel.getThumbnail()).into(holder.thumbnail1);

    }


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

这就是结果:

enter image description here

问题在哪里?

1 个答案:

答案 0 :(得分:0)

Try this way

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="288dp">

        <android.support.v7.widget.CardView
            android:id="@+id/card_view1"
            android:layout_width="match_parent"
            android:layout_height="288dp"
            android:elevation="3dp"
            card_view:cardCornerRadius="0dp"
            android:gravity="top">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/card"
                android:focusable="true"
                android:contextClickable="true"
                android:gravity="top"
                android:layout_alignParentBottom="true">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView2"
                    android:src="@drawable/ic_launcher"
                    android:layout_centerVertical="true"
                    android:layout_centerHorizontal="true" />
            </RelativeLayout>

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

    </LinearLayout>
    <TextView
        android:id="@+id/title1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="25dp"
        android:paddingTop="10dp"
        android:textColor="@color/cardview_dark_background"
        android:textSize="15dp"
        android:text="Title"
        android:layout_above="@+id/count1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageView
        android:id="@+id/thumbnail1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:clickable="true"
        android:src="@drawable/ic_launcher"
        android:contextClickable="true"
        android:layout_alignTop="@+id/title1" />

    <TextView
        android:id="@+id/count1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp"
        android:paddingRight="25dp"
        android:textSize="12dp"
        android:text="sub title"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</LinearLayout>