【问题标题】:Issue using 2 different recyclerView in 2 activities with 1 room database在 1 个房间数据库的 2 个活动中使用 2 个不同的 recyclerView 问题
【发布时间】:2021-04-16 19:45:19
【问题描述】:

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

  1. 实现 Room 数据库,然后通过 recyclerView 将数据呈现给用户。
  2. 在主要活动中有 1 个 recyclerView,在第二个活动中有第二个 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 中跨两个 Activity 测试的布局。

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>

testing.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”的类,该类使用使用 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();
}```

【问题讨论】:

  • “recyclerView 的名称,以及每个活动的方法,第二个活动中的布局显示在第一个活动中”是什么意思?什么名字和什么方法?请出示相关代码。
  • 嗨@Code-Apprentice - 我已经在描述中粘贴了所有相关代码。这有帮助吗?
  • 您看到的问题是什么?您需要什么帮助?
  • 你好@Code-Apprentice - 问题是我为 2 个活动创建了 2 个布局,但是来自第 2 个活动的布局显示在两个活动中。我在描述中展示了 2 种不同的布局(超链接)。让我知道这是否有助于澄清。
  • “但是第二个活动的布局显示在两个活动中”是什么意思?在您的问题中添加一些屏幕截图,以显示每个屏幕的外观并说明您希望它变成什么样子。

标签: java android-activity android-recyclerview android-room


【解决方案1】:

问题是两个活动都使用相同的适配器作为它们的列表视图: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。

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

【讨论】:

  • 您好 - 感谢您的意见。正如您正确指出的那样,我确实希望两个活动的内容相同,因为使用了相同的 ViewModel。问题仍然是在 Main Activity 中,我希望 recyclerView 左侧有一个数字,中间有标题,右侧有描述。但是,布局被忽略了,我在主活动的第二个活动中看到了布局。问题不在于显示的数据,而在于它在 2 个活动中的布局方式。 (我没有解释它出现的问题!)
  • @MrBrady 我想我越来越了解这个问题了。让我重申一下我是否这样做:您希望回收站视图中的项目有不同的布局,具体取决于它显示在哪个活动中。这些布局在testing.xmlnote_item.xml 中定义,对吗?你在哪里膨胀这些布局?我认为应该在NoteAdapter,对吧?如果是这样,请编辑您的代码以显示NoteAdapter 的相关部分。
  • @MrBrady 切线,为什么你有两个活动来显示相同​​的数据列表?对我来说,这似乎是一个过于复杂的用户体验。
  • 您的理解是正确的 - 我希望根据所使用的活动对项目进行不同的布局,但这(当前)没有发生。我已经添加了请求的代码 - 我假设一个简单的错误,但我无法发现它。
  • 感谢@Code-Apprentice - 将查看建议的主/详细视图。
猜你喜欢
  • 2018-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-01
相关资源
最近更新 更多