I want to display the class card dynamically.BUT it can't show on the screen.

I add a LinearLayout and set it params, and then add it into the Layout.

        private void createItemcourseView(final Course course) {
    int dayId = 0;
   //switch()
    day = (RelativeLayout)findViewById(dayId);
    int height = 180;
    View v = LayoutInflater.from(this).inflate(R.layout.class_card_layout, null); //load the classcard
    v.setY(height * (course.getStart() - 1)); //set the height of the class
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
            (ViewGroup.LayoutParams.MATCH_PARENT, (course.getEnd() - course.getStart() + 1) * height - 8); //set the layout height
    v.setLayoutParams(params);
    TextView text = v.findViewById(R.id.text_view);
    text.setText(course.getCourse() + "\n" + course.getTeacher() + "\n" + course.getClassroom()); //show the course name
    day.addView(v);

    //delete the class with long time press
    v.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            v.setVisibility(View.GONE);
            day.removeView(v);
            LitePal.delete(Course.class, course.getmainId());
            return true;
        }
    });
}

the layout.xml is below:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="45dp"
android:layout_height="70dp"
android:foreground="?android:attr/selectableItemBackground"
app:cardBackgroundColor="#7feacdd1"
app:cardCornerRadius="7dp"
android:id="@+id/card_view">

<TextView
    android:id="@+id/text_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"/>

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

I used the litepal database and create a class named course

0 Answers