[英]How can I add a switch layout to a RecyclerView from a different activity?
我尝试通过从不同的活动发送一个意图来向回收者视图添加一个新的卡片视图。 回收者视图元素 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 发送意图:
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 = 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 = getIntent();
String newNotifName = intent.getStringExtra("notifName");
if(newNotifName != null){
addToNotificationList(newNotifName);
}
如果没有空检查,您总是会尝试为“notifName”获取额外的值,并且即使没有任何值传递给您的活动,您也会调用addToNotificationList
。 这将向您的RecyclerCiew
添加一个没有文本的项目,这会导致创建一个CardView
,其中TextView
没有文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.