【问题标题】:How can I add a switch layout to a RecyclerView from a different activity?如何从不同的活动向 RecyclerView 添加开关布局?
【发布时间】:2019-11-22 22:12:53
【问题描述】:

我尝试通过发送来自不同活动的意图来将新卡片视图添加到回收站视图。 回收站视图元素 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 后添加文本。我想要的是仅在添加之后创建文本和开关,这意味着在发送意图之后。我该如何解决这个问题?

【问题讨论】:

    标签: java android xml android-intent android-recyclerview


    【解决方案1】:

    您似乎在这里遗漏了一个空检查:

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

    如果没有空值检查,您总是会尝试为“notifName”获取额外的值,并且您调用addToNotificationList,即使您的活动没有传递任何值。这将向您的 RecyclerCiew 添加一个没有文本的项目,这会导致创建一个 CardView ,而 TextView 没有文本。

    【讨论】:

    • 是的,我认为问题在于调用 onCreate 函数中的函数。但是,添加一个空检查解决了我的问题。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多