[英]Issue using 2 different recyclerView in 2 activities with 1 room database
美好的一天 - 我是 recyclerViews 並連接到 Room 數據庫的新手。 我有一個在教程的幫助下工作,現在我想添加它。 我的目標是:
主要活動:
第二個活動:
我遇到的問題是即使 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 嗎?
為了解決問題,我已經:
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 我建議使用一致的命名。 你有AllWordsActivity
和NotesAdapater
。 使用AllWords
或Notes
,但不要混搭。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.