Я слежу за серией приложений для AndroidStudio на YouTube здесь: https://www.youtube.com/watch?v=reSPN7mgshI
Я создал файл макета 'note_item. xml', который использует CardView для установки макетов карт, которые будут помещены в RecyclerView в моем файле 'activity_main. xml'.
Мой класс 'NoteAdapter. java' получает данные из моих объектов Note из Списка и использует данные для создания cardViews для помещения в RecyclerView.
Проблема, с которой я столкнулся, заключается в том, что один из моих методов не распознает мой файл 'note_item. xml'. Я получаю сообщение об ошибке «Не удается разрешить символ« note_item »Создать файл ресурсов макета« note_item. xml »». У меня уже есть файл note_item. xml, как вы можете видеть на изображении внизу.
Возможно, элемент note_item. xml root при создании имеет значение 'androidx. cardview.widget.CardView? Насколько я вижу, человек на видео все еще не использует AndroidX.
Вот код ...
package com.example.libraryroomapp;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
/* NoteAdapter gets data from Note objects into the RecyclerView items
* */
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder> {
private List<Note> notes = new ArrayList<>();
@NonNull
@Override
//method to create and return a NoteHolder
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();
}
//ViewHolder class holds the views in our single recyclerView items
class NoteHolder extends RecyclerView.ViewHolder {
private TextView textViewTitle;
private TextView textViewDescription;
private TextView textViewPriority;
//constructor
public NoteHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.text_view_title);
textViewDescription = itemView.findViewById(R.id.text_view_description);
textViewPriority = itemView.findViewById(R.id.text_view_priority);
}
}
}
Код моего примечания_ 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_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp">
<TextView
android:id="@+id/text_view_priority"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="1"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<TextView
android:id="@+id/text_view_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_toStartOf="@id/text_view_priority"
android:ellipsize="end"
android:maxLines="1"
android:text="Title"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<TextView
android:id="@+id/text_view_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text_view_title"
android:text="Description" />
</RelativeLayout>
</androidx.cardview.widget.CardView>