activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="@ id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@ id/addNotificationPanel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.207">
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/notificationPanelRecView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:visibility="visible" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
notification_panel_cardview.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.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:id="@ id/notificationPanelParent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardCornerRadius="7dp"
app:cardElevation="4dp">
<RelativeLayout
android:layout_width="335dp"
android:layout_height="130dp">
<EditText
android:id="@ id/notificationName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:maxLength="29"
android:text="Notification"
android:textSize="14sp"
tools:ignore="TouchTargetSizeCheck" />
<CheckBox
android:id="@ id/enableNotificationCheckbox"
android:layout_width="34dp"
android:layout_height="34dp"
android:layout_marginRight="4dp"
android:layout_toLeftOf="@ id/deleteNotificationPanel"
android:gravity="fill"
tools:ignore="TouchTargetSizeCheck" />
<ImageButton
android:id="@ id/deleteNotificationPanel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="6dp"
android:background="@null"
android:src="@android:drawable/ic_delete"
tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck" />
<TextView
android:id="@ id/notifyEvery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@ id/notificationName"
android:layout_marginLeft="6dp"
android:layout_marginTop="1dp"
android:text="Notify me every :"
android:textSize="17sp" />
<Spinner
android:id="@ id/hoursSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@ id/notificationName"
android:layout_toRightOf="@ id/notifyEvery"
tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@ id/notifyEvery"
android:layout_marginTop="5dp">
<CheckBox
android:id="@ id/mondayCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center"
android:text="Mon"
android:textSize="13sp" />
<CheckBox
android:id="@ id/tuesdayCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@ id/mondayCheckBox"
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center"
android:text="Tue"
android:textSize="13sp" />
<CheckBox
android:id="@ id/wednesdayCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@ id/tuesdayCheckBox"
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center"
android:text="Wed"
android:textSize="13sp" />
<CheckBox
android:id="@ id/thursdayCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/wednesdayCheckBox"
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center"
android:text="Thu"
android:textSize="13sp" />
<CheckBox
android:id="@ id/fridayCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@ id/thursdayCheckBox"
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center"
android:text="Fri"
android:textSize="13sp" />
<CheckBox
android:id="@ id/saturdayCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@ id/fridayCheckBox"
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center"
android:text="Sat"
android:textSize="13sp" />
<CheckBox
android:id="@ id/sundayCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/saturdayCheckBox"
android:button="@null"
android:drawableBottom="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center"
android:text="Sun"
android:textSize="13sp"
tools:ignore="TouchTargetSizeCheck" />
</RelativeLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
NotificationPanel.java
public class NotificationPanel {
private int id;
private String notificationName ;
public NotificationPanel(int id, String notificationName) {
this.id = id;
this.notificationName = notificationName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNotificationName() {
return notificationName;
}
public void setNotificationName(String notificationName) {
this.notificationName = notificationName;
}
@Override
public String toString() {
return "NotificationPanel{"
"id=" id
", notificationName='" notificationName '\''
'}';
}
}
NotificationPanelRecViewAdapter.java
public class NotificationPanelRecViewAdapter extends RecyclerView.Adapter<NotificationPanelRecViewAdapter.ViewHolder> {
private String TAG = "NotificationPanelRecViewAdapter";
private ArrayList<NotificationPanel> notificationPanel = new ArrayList<>();
private Context mContext;
public NotificationPanelRecViewAdapter(Context mContext) {
this.mContext = mContext;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.notification_panel_cardview,null,false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder,final int position) {
Log.d(TAG,"onBindViewHolder: Called");
holder.notificationName.setText(notificationPanel.get(position).getNotificationName());
}
@Override
public int getItemCount() {
return 0;
}
public void setNotificationPanel(ArrayList<NotificationPanel> notificationPanel) {
this.notificationPanel = notificationPanel;
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private CardView parent;
private EditText notificationName ;
private CheckBox monCheckBox ,tueCheckBox,wedCheckBox,thuCheckBox,friCheckBox,satCheckBox,sunCheckBox,enableNotifCheckbox;
private TextView notifyEvery;
private Spinner hoursSpinner;
private ImageButton deleteNotificationPanel;
public ViewHolder(@NonNull View itemView) {
super(itemView);
parent = itemView.findViewById(R.id.notificationPanelParent);
notificationName = itemView.findViewById(R.id.notificationName);
monCheckBox = itemView.findViewById(R.id.mondayCheckBox);
tueCheckBox = itemView.findViewById(R.id.tuesdayCheckBox);
wedCheckBox = itemView.findViewById(R.id.wednesdayCheckBox);
thuCheckBox = itemView.findViewById(R.id.thursdayCheckBox);
friCheckBox = itemView.findViewById(R.id.fridayCheckBox);
satCheckBox = itemView.findViewById(R.id.saturdayCheckBox);
sunCheckBox = itemView.findViewById(R.id.sundayCheckBox);
enableNotifCheckbox = itemView.findViewById(R.id.enableNotificationCheckbox);
notifyEvery = itemView.findViewById(R.id.notifyEvery);
hoursSpinner = (Spinner) itemView.findViewById(R.id.hoursSpinner);
deleteNotificationPanel = itemView.findViewById(R.id.deleteNotificationPanel);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(mContext,R.array.hoursSpinnerValues,R.layout.notification_panel_cardview);
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private RecyclerView notificationPanelRecView;
private NotificationPanelRecViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
notificationPanelRecView = findViewById(R.id.notificationPanelRecView);
ArrayList<NotificationPanel> notificationPanel = new ArrayList<>();
notificationPanel.add(new NotificationPanel(1, "Pills"));
NotificationPanelRecViewAdapter adapter = new NotificationPanelRecViewAdapter(this);
adapter.setNotificationPanel(notificationPanel);
notificationPanelRecView.setLayoutManager(new LinearLayoutManager(this));
notificationPanelRecView.setAdapter(adapter);
}
I made an ArrayList with one item added in MainActivity just to test the layout and my problem is that it is not showing at all. Any hint about why is this happening would be helpful.---------------------------------------------------------------------------------------------------------------
CodePudding user response:
The adapter won't bind anything if it thinks there is nothing to bind.
@Override
public int getItemCount() {
return 0;
}
needs to be
@Override
public int getItemCount() {
return notificationPanel.size();
}