【发布时间】:2021-04-16 19:45:19
【问题描述】:
美好的一天 - 我是 recyclerViews 和连接到 Room 数据库的新手。我有一个在教程的帮助下工作,现在我想添加它。我的目标是:
- 实现 Room 数据库,然后通过 recyclerView 将数据呈现给用户。
- 在主要活动中有 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