繁体   English   中英

在 1 个房间数据库的 2 个活动中使用 2 个不同的 recyclerView 问题

[英]Issue using 2 different recyclerView in 2 activities with 1 room database

美好的一天 - 我是 recyclerViews 并连接到 Room 数据库的新手。 我有一个在教程的帮助下工作,现在我想添加它。 我的目标是:

  1. 实现一个 Room 数据库,然后通过 recyclerView 将数据呈现给用户。
  2. 在主要活动中有 1 个 recyclerView,在第二个活动中有第 2 个 recyclerView。 我试图在每个活动中使用不同的布局,如以下链接所示。

主要活动:

在此处输入图像描述

第二个活动:

在此处输入图像描述

我遇到的问题是即使 XML 文件对于每个活动都是唯一的,以及其中的变量、recyclerView 的名称以及每个活动的方法,第二个活动中的布局显示在第一个活动中。

activity_main 有以下内容:

<androidx.recyclerview.widget.RecyclerView
    android:layout_marginTop="100dp"
    android:id="@+id/recycler_view111"
    android:layout_width="match_parent"
    android:layout_height="114dp"
    tools:listitem="@layout/testing" />

第二个活动 XML 文件有以下内容:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="488dp"
    tools:listitem="@layout/testing1_item" />

有人可以帮我确定为什么在 2 个活动中显示相同的 recyclerView 吗?

为了解决问题,我已经:

  • 浏览所有 XML 文件,以确保文件/方法等的名称没有问题。
  • 从 activity_main xml 复制布局并将其应用于第二个 xml 文件。 然后这又显示了我在 MainActivity 中跨两个活动测试的布局。

MainActivity 使用 activity_main.xml,它使用一个名为 testing.xml 的测试文件 - 代码如下:

主要活动:

 protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        
        Button button = (Button) findViewById(R.id.button); 

        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(getApplicationContext(), AllWordsActivity.class);
                startActivity(intent);
            }
        });

        RecyclerView recyclerView111 = findViewById(R.id.recycler_view111);
        recyclerView111.setLayoutManager(new LinearLayoutManager(this));
        recyclerView111.setHasFixedSize(true);

        NoteAdapter adapter = new NoteAdapter();
        recyclerView111.setAdapter(adapter);

        noteViewModel.getAllNotes().observe(this, new Observer<List<Note>>()
        {
            @Override
            public void onChanged(List<Note> notes)
            {
               adapter.setNotes(notes);
            }
        });

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="500dp"
        android:layout_marginRight="50dp"
        android:background="@color/cardview_shadow_start_color"
        android:maxLines="1"
        android:text="Open new Activity!"
        android:textColor="@color/black"
        android:textSize="20dp"
        android:textStyle="bold" />

    <androidx.recyclerview.widget.RecyclerView
        android:layout_marginTop="100dp"
        android:id="@+id/recycler_view111"
        android:layout_width="match_parent"
        android:layout_height="114dp"
        tools:listitem="@layout/testing" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

测试.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp">

        <TextView
            android:id="@+id/text_view_priority1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:textColorHighlight="@color/black"
            android:text="1"
            android:textAppearance="@style/TextAppearance.AppCompat.Large" />

        <TextView
            android:id="@+id/text_view_title1"
            android:layout_width="323dp"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="183dp"
            android:layout_toStartOf="@id/text_view_priority1"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="Title1"
            android:textAppearance="@style/TextAppearance.AppCompat.Large" />

        <TextView
            android:id="@+id/text_view_description1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_below="@id/text_view_title1"
            android:text="Description1" />

    </RelativeLayout>
</androidx.cardview.widget.CardView>

第二个活动基于名为 AllWordsActivity 的 class”,它使用使用 note_item 测试的 activity_all_words - 代码如下:

AllWordsActivity.java

private NoteViewModel noteViewModel;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_all_words);


getSupportActionBar().setDisplayHomeAsUpEnabled(true);

RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);

NoteAdapter adapter = new NoteAdapter();
recyclerView.setAdapter(adapter);

//noteViewModel = ViewModelProviders.of(this).get(NoteViewModel.class);
noteViewModel.getAllNotes().observe(this, new Observer<List<Note>>()
{
    @Override
    public void onChanged(List<Note> notes)
    {
        adapter.setNotes(notes);
    }
});

activity_all_words.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AllWordsActivity">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="488dp"
        tools:listitem="@layout/note_item" />




</androidx.coordinatorlayout.widget.CoordinatorLayout>

note_item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp">

        <TextView
            android:id="@+id/text_view_priority"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignParentEnd="true"
            android:textColor="@color/purple_500"
            android:text="1"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"/>

        <TextView
            android:id="@+id/text_view_title"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Title"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            android:maxLines="1"
            android:layout_toStartOf="@id/text_view_priority"
            android:layout_alignParentStart="true"
            android:ellipsize="end"/>

        <TextView
            android:id="@+id/text_view_description"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_below="@id/text_view_title"
            android:text="Description" />

    </RelativeLayout>


</androidx.cardview.widget.CardView>

noteAdapter 的代码:

```public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder>

{ 私有列表注释 = new ArrayList<>();

@NonNull
@Override
public NoteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.note_item, parent, false);
    return new NoteHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull NoteHolder holder, int position)
{
    Note currentNote = notes.get(position);
    holder.textViewTitle.setText(currentNote.getTitle());
    holder.textViewDescription.setText(currentNote.getDescription());
    holder.textViewPriority.setText(String.valueOf(currentNote.getPriority()));
}

@Override
public int getItemCount()
{
    return notes.size();
}

public void setNotes(List<Note> notes)
{
    this.notes = notes;
    notifyDataSetChanged();
}```

问题是这两个活动的列表视图使用相同的适配器: NoteAdapter adapter = new NoteAdapter(); . 反过来, NoteAdapter只为列表中的每个项目膨胀note_item.xml

View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.note_item, parent, false);

要解决此问题,您需要更改NoteAdapter以使用其行的多个布局文件。 或者您需要为每个RecyclerView创建不同的适配器。

原答案:

听起来您在两个回收站视图中都看到了相同的项目。 问题是 MainActivity 和 AllWordsActivity 都有这些行:

        NoteAdapter adapter = new NoteAdapter();
        recyclerView.setAdapter(adapter);

        noteViewModel.getAllNotes().observe(this, new Observer<List<Note>>()
        {
            @Override
            public void onChanged(List<Note> notes)
            {
               adapter.setNotes(notes);
            }
        });

所以这两个活动都从同一个 ViewModel 获取数据来显示。 要显示不同的数据,请在 MainActivity 中使用不同的 ViewModel。

ps 我建议使用一致的命名。 你有AllWordsActivityNotesAdapater 使用AllWordsNotes ,但不要混搭。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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