簡體   English   中英

如何從不同的活動向 RecyclerView 添加開關布局?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM