简体   繁体   中英

How can I add a switch layout to a RecyclerView from a different activity?

I try to add a new card view to the recycler view by sending an intent from a different activity. The recycler view element 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>

I send the intent from 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);
            }
        });

I get the intent in NotificationManager.java:

Intent intent = getIntent();
        String newNotifName = intent.getStringExtra("notifName");
        addToNotificationList(newNotifName);

addToNotificationList function:

private void addToNotificationList(String newNotifName) {
        this.notificationList.add(new Notification(newNotifName,false));

    }

Finally, here is the Adapter class that I populate the RecyclerView:

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);

        }
    }
}

However, even before I add the notification by the category input, from the NewNotification class, the layout displays a Switch without a text and adds the text after sending the Intent. What I want is creating the text and the switch only after the addition, meaning after the intent is sent. How can I get around this?

It looks like you are missing a null check here:

Intent intent = getIntent();
String newNotifName = intent.getStringExtra("notifName");
if(newNotifName != null){
    addToNotificationList(newNotifName);
}

Without the null check, you always try to get the extra for "notifName" and you call addToNotificationList even when no value has been passed to your activity. This will add an item to your RecyclerCiew without a text, which leads to the creation of a CardView where the TextView has no text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM