Issue
I'm following an AndroidStudio App creation series on YouTube here: https://www.youtube.com/watch?v=reSPN7mgshI
I've created a layout file 'note_item.xml' that uses CardView to set up card layouts that will be put into a RecyclerView in my 'activity_main.xml' file.
My 'NoteAdapter.java' class gets the data from my Note objects out of a List and uses the data to create cardViews to be put into the RecyclerView.
The problem I'm having is that one of my methods won't recognise my 'note_item.xml' file. I get an error that says "Cannot resolve symbol 'note_item' Create layout resource file 'note_item.xml'". I already have a note_item.xml file, as you can see in the image at the bottom.
Could it be that the note_item.xml root element upon creation is set to 'androidx.cardview.widget.CardView'? The person in the video is still not using AndroidX as far as I can see.
Here's the code...
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);
}
}
}
The code for my note_item.xml is here too.
<?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>
Solution
Such a problem periodically occurs in Android Studio. Just click rebuild the project or exit and re-enter the android studio. And try to generate apk. If it builds successfully, then there are no problems. And if there was an error, the apk would simply not be generated.
Answered By - droidbaza
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.