我尝试通过从不同的 Activity 发送 Intent 来将新的卡片 View 添加到回收器 View 中。 回收器 View 元素category_switch_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".NotificationManager">
<androidx.cardview.widget.CardView
android:id="@+id/notificationCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/yourNotificationsLayout"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="15dp">
<TextView
android:id="@+id/notificationSwitchText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="15dp"
android:textSize="13sp"
android:hint="Switch"
android:textStyle="bold"/>
<Switch
android:id="@+id/notificationSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
我从 NewNotification.java 发送 Intent :
createNotifButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),NotificationManager.class);
i.putExtra("notifName",categoryInput.getText().toString());
startActivity(i);
}
});
我在NotificationManager.java中得到了 Intent :
Intent intent = getIntent();
String newNotifName = intent.getStringExtra("notifName");
addToNotificationList(newNotifName);
addToNotificationList函数:
private void addToNotificationList(String newNotifName) {
this.notificationList.add(new Notification(newNotifName,false));
}
最后,这是我填充 RecyclerView 的 Adapter 类:
public class NotificationAdapter extends RecyclerView.Adapter<NotificationAdapter.NotificationViewHolder> {
private LayoutInflater linflater;
private ArrayList<Notification> notifList;
public NotificationAdapter(Context ctx, ArrayList<Notification> notifList) {
linflater = LayoutInflater.from(ctx);
this.notifList = notifList;
}
@NonNull
@Override
public NotificationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = linflater.inflate(R.layout.category_switch_row,parent,false);
return new NotificationViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull NotificationViewHolder holder, int position) {
holder.notificationText.setText(notifList.get(position).getName());
holder.notificationSwitch.setChecked(notifList.get(position).isSwitched());
}
@Override
public int getItemCount() {
return notifList.size();
}
public class NotificationViewHolder extends RecyclerView.ViewHolder{
TextView notificationText;
Switch notificationSwitch;
public NotificationViewHolder(@NonNull View itemView) {
super(itemView);
notificationText = itemView.findViewById(R.id.notificationSwitchText);
notificationSwitch = itemView.findViewById(R.id.notificationSwitch);
}
}
}
但是,即使在我通过类别输入添加通知之前,从 NewNotification 类中,布局也会显示一个不带文本的 Switch,并在发送 Intent 后添加文本。我想要的是仅在添加后(即发送 Intent 后)创建文本和开关。我该如何解决这个问题?
最佳答案
您似乎在这里缺少空检查:
Intent intent = getIntent();
String newNotifName = intent.getStringExtra("notifName");
if(newNotifName != null){
addToNotificationList(newNotifName);
}
如果没有空检查,您总是尝试获取“notifName”的额外内容,然后调用 addToNotificationList
即使没有任何值(value)传递给您的 Activity 。这将向您的 RecyclerCiew
添加一个项目没有文本,这会导致创建 CardView
其中TextView
没有文字。
关于java - 如何从不同的 Activity 将开关布局添加到 RecyclerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59002437/