我尝试通过发送来自其他活动的意图将新的卡片视图添加到回收者视图。回收者视图元素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
没有文本。
发布问题后几分钟,我就收到了问题。我想我不得不问清楚解决方案。无论如何,如果有人遇到相同的事情,我的错误是在onCreate函数中调用了intent和addNotificationList,这将某些东西添加到列表中,并在回收器视图中填充了冗余的开关视图。
当我将代码移出onCreate时,问题已解决。
private ArrayList<Notification> generateNotifications() {
ArrayList<Notification> tempList = new ArrayList<>();
Intent intent = getIntent();
String newNotifName = null;
if (intent.hasExtra("notifName")){
newNotifName = intent.getStringExtra("notifName");
tempList.add(new Notification(newNotifName,true));
}
tempList.add(new Notification("foo",true));
tempList.add(new Notification("dummy",true));
return tempList;
}