CardView项目不可见

时间:2016-04-26 19:57:39

标签: java android

我试图简单地将TextView放在CardView中,但无济于事。我需要做的就是在CardViews中显示两个信息。目前,使用下面的代码,会出现两个CardView,但不会在其中显示任何文本/内容。

Heres是xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <android.support.v7.widget.CardView
        android:id="@+id/cvSingleUser"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="12dp">


            <TextView
                android:id="@+id/tvCourseCode"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:visibility="visible"
                android:textColor="#020202" />

            <TextView
                android:id="@+id/tvCourseName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/tvCourseCode"
                android:visibility="visible"
                android:textColor="#020202" />

            <TextView
                android:id="@+id/tvCourseLecturer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/tvCourseName"
                android:visibility="visible"
                android:textColor="#020202" />
            <TextView
                android:id="@+id/tvCourseRoom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/tvCourseLecturer"
                android:visibility="visible"
                android:textColor="#020202" />
            <TextView
                android:id="@+id/tvCourseTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/tvCourseRoom"
                android:visibility="visible"
                android:textColor="#020202" />
            <TextView
                android:id="@+id/tvCourseStartDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/tvCourseTime"
                android:visibility="visible"
                android:textColor="#020202" />

        </RelativeLayout>

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

</RelativeLayout>

AllCourses.java

public class AllCoursessAdapter extends RecyclerView.Adapter<AllCoursessAdapter.UserViewHolder>{

    private List<AllUsers.User> courseList;
    private Context context;
    public AllCoursessAdapter(List<AllUsers.User> courseList, Context context) {
        this.courseList = courseList;
        this.context = context;
    }

    @Override
    public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.single_cardview_layout,null);
        UserViewHolder userViewHolder = new UserViewHolder(view);
        return userViewHolder;
    }

    @Override
    public void onBindViewHolder(UserViewHolder holder, int position) {
        AllUsers.User user = courseList.get(position);

        holder.tvCourseCode.setText(user.getCourseCode());
        holder.tvCourseName.setText(user.getCourseName());
        holder.tvCourseLecturer.setText(user.getCourseLecturer());
        holder.tvCourseRoom.setText(user.getCourseRoom());
        holder.tvCourseTime.setText(user.getCourseTime());
        holder.tvCourseStartDate.setText(user.getCourseStartDate());

    }

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

    public static class UserViewHolder extends RecyclerView.ViewHolder {


        TextView tvCourseCode;
        TextView tvCourseName;
        TextView tvCourseLecturer;
        TextView tvCourseRoom;
        TextView tvCourseTime;
        TextView tvCourseStartDate;
        public UserViewHolder(View itemView) {
            super(itemView);

            tvCourseCode = (TextView) itemView.findViewById(R.id.tvCourseCode);
            tvCourseName = (TextView) itemView.findViewById(R.id.tvCourseName);
            tvCourseLecturer = (TextView) itemView.findViewById(R.id.tvCourseLecturer);
            tvCourseRoom = (TextView) itemView.findViewById(R.id.tvCourseRoom);
            tvCourseTime = (TextView) itemView.findViewById(R.id.tvCourseTime);
            tvCourseStartDate = (TextView) itemView.findViewById(R.id.tvCourseStartDate);


        }
    }
}

AllUsers.java

public class AllUsers extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_users);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rvAllUsers);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        AllCoursessAdapter allcoursesAdapter = new AllCoursessAdapter(getUserInformation(), this);
        recyclerView.setAdapter(allcoursesAdapter);
    }

    private List<User> getUserInformation() {

        List<User> userList = new ArrayList<>();
        userList.add(new User("COMP407", "Information Systems Analysis",
                "B1C","11:30-12:15","August 19th"));
        userList.add(new User("COMP407", "Information Systems Analysis",
                "B1C","11:30-12:15","August 19th"));
        userList.add(new User("COMP407", "Information Systems Analysis",
                "B1C","11:30-12:15","August 19th"));
        userList.add(new User("COMP407", "Information Systems Analysis",
                "B1C","11:30-12:15","August 19th"));
        userList.add(new User("COMP407", "Information Systems Analysis",
                "B1C","11:30-12:15","August 19th"));
        userList.add(new User("COMP407", "Information Systems Analysis",
                "B1C","11:30-12:15","August 19th"));
        userList.add(new User("COMP407", "Information Systems Analysis",
                "B1C","11:30-12:15","August 19th"));

        return userList;
    }

    public class User {
        private String courseCode;
        private String courseName;
        private String courseLecturer;
        private String courseRoom;
        private String courseTime;
        private String courseStartdate;


        public User(String code, String name, String lecturer, String room, String time) {

        }

        public String getCourseCode() {
            return courseCode;
        }
        public String getCourseName() {
            return courseName;
        }
        public String getCourseLecturer() {
            return courseLecturer;
        }
        public String getCourseRoom() {
            return courseRoom;
        }
        public String getCourseTime() {
            return courseTime;
        }
        public String getCourseStartDate() {
            return courseStartdate;
        }

        public User(String courseCode, String courseName, String courseLecturer, String courseRoom, String courseTime, String courseStartdate) {
            this.courseCode = courseCode;
            this.courseName = courseName;
            this.courseLecturer = courseLecturer;
            this.courseRoom = courseRoom;
            this.courseTime = courseTime;
            this.courseStartdate = courseStartdate;
        }
    }
}

0 个答案:

没有答案