英文:
List of items aren't showing up in the RecyclerView
问题
以下是翻译好的内容:
我正在自学安卓应用开发(以及Java)。在弄清楚为什么我的列表项没有显示出来时遇到了麻烦。弹出窗口运行得很好,所以我没有包含它的代码。
我尝试通过添加Log.d()
消息来查看GroceryItemAdapter
是否正常工作,但它没有显示出来。我猜想是我制作的适配器有问题?
更新:
修复了一行代码,但现在我遇到了这个问题(稍后会更新,要研究一下是什么原因或者进行调试):
AutoCrab的答案帮助解决了空对象问题,我还发现在将整数传递给setText()
之前需要将其转换为字符串。
java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法 'android.view.View android.widget.TextView.findViewById(int)'
at com.example.myapplication.GroceryListAdapter$ViewHolder.<init>
package com.example.myapplication;
import android.content.Context;
import android.util.Log;
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;
//负责将模型中的数据显示到RecyclerView中的行中
public class GroceryListAdapter extends RecyclerView.Adapter<GroceryListAdapter.ViewHolder>{
private List<GroceryList> lists = new ArrayList<>();
public GroceryListAdapter(List<GroceryList> lists) {
this.lists = lists;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View groceryListView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grocery_list_entry, parent, false);
Log.d("MEJD", "我在这里吗?");
ViewHolder viewHolder = new ViewHolder(groceryListView);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
GroceryList groceryListItem = lists.get(position);
TextView textView = holder.listTitle;
textView.setText(groceryListItem.getListName());
Log.d("MEJD", "我在这里");
TextView numItemsView = holder.numItems;
numItemsView.setText(String.valueOf(groceryListItem.getNumItems()));
}
@Override
public int getItemCount() {
return lists.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView listTitle;
public TextView numItems;
public ViewHolder(@NonNull View itemView) {
super(itemView);
listTitle = itemView.findViewById(R.id.groceryListName);
numItems = itemView.findViewById(R.id.numItemsList);
}
}
}
GroceryList类:
package com.example.myapplication;
import java.util.List;
public class GroceryList {
private String listName;
private int numItems;
public GroceryList(String listName, int numItems) {
this.listName = listName;
this.numItems = numItems;
}
public String getListName() {
return listName;
}
public void setListName(String listName) {
this.listName = listName;
}
public int getNumItems() {
return numItems;
}
public void setNumItems(int numItems) {
this.numItems = numItems;
}
}
GroceryListAdapter适配器:
import android.content.Context;
import android.util.Log;
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;
public class GroceryListAdapter extends RecyclerView.Adapter<GroceryListAdapter.ViewHolder> {
private List<GroceryList> lists = new ArrayList<>();
public GroceryListAdapter(List<GroceryList> lists) {
this.lists = lists;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View groceryListView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grocery_list_entry, parent, false);
Log.d("MEJD", "我在这里吗?");
ViewHolder viewHolder = new ViewHolder(groceryListView);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
GroceryList groceryListItem = lists.get(position);
TextView textView = holder.listTitle;
textView.setText(groceryListItem.getListName());
Log.d("MEJD", "我在这里");
TextView numItemsView = holder.numItems;
numItemsView.setText(String.valueOf(groceryListItem.getNumItems()));
}
@Override
public int getItemCount() {
return lists.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView listTitle;
public TextView numItems;
public ViewHolder(@NonNull View itemView) {
super(itemView);
listTitle = itemView.findViewById(R.id.groceryListName);
numItems = itemView.findViewById(R.id.numItemsList);
}
}
}
MainActivity的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<androidx.appcompat.widget.Toolbar
android:id="@+id/homeToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#27D3C2"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintTop_toTopOf="parent"
app:title="Add Grocery List"
app:titleMarginStart="130dp" />
<ImageButton
android:id="@+id/addButton"
android:layout_width="61dp"
android:layout_height="56dp"
android:layout_alignBottom="@+id/homeToolbar"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="322dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="29dp"
android:layout_marginBottom="0dp"
android:backgroundTint="#00FFFFFF"
android:clickable="true"
android:contentDescription="@+string/add list button"
app:layout_constraintEnd_toEndOf="@+id/homeToolbar"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/homeToolbar"
app:layout_constraintTop_toTopOf="@+id/homeToolbar"
app:srcCompat="@drawable/add_icon"
android:focusable="true" />
<android
<details>
<summary>英文:</summary>
I am teaching myself android app development(and Java). Having trouble figuring out why the items of my list are not showing up. The pop up works fine, so I didn't include the code for it.
I tried to see if the `GroceryItemAdapter` is working by adding a `Log.d()` message and it doesn't show up. I'm guessing it's the Adapter I made?
**Update:**
Fixed a line, but now I'm getting this(will update this later, going to research what's the cause or debug it):
AutoCrab's answer helped solve the null object issue, I also figured out I needed to convert my integer into a string before passing it to `setText()`.
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.TextView.findViewById(int)' on a null object reference
at com.example.myapplication.GroceryListAdapter$ViewHolder.<init>
package com.example.myapplication;
import android.content.Context;
import android.util.Log;
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;
//responsible for displaying data from the model into a row in the recycler view
public class GroceryListAdapter extends RecyclerView.Adapter<GroceryListAdapter.ViewHolder>{
private List<GroceryList> lists = new ArrayList<>();
public GroceryListAdapter(List<GroceryList> lists) {
this.lists = lists;
}
//onCreateViewHolder is responsible for creating each view
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//create a new view and wrap it inside a new viewholder
//use layout inflator to inflate a view
View groceryListView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grocery_list_entry,parent,false);
Log.d("MEJD", "Iamhere?");
//wrap it inside a ViewHolder and return it
ViewHolder viewHolder = new ViewHolder(groceryListView);
return viewHolder;
}
//onBindViewHolder is responsible for taking data at a particular position and
//putting it into a viewholder
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//get data model by position
GroceryList groceryListItem = lists.get(position);
//set the item views based on the data models
TextView textView = holder.listTitle;
textView.setText(groceryListItem.getListName());
Log.d("MEJD", "Iamhere");
TextView numItemsView = holder.numItems;
numItemsView.setText(groceryListItem.getNumItems());
}
//get number of items in the data
@Override
public int getItemCount() {
lists.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
//member variables
public TextView listTitle;
public TextView numItems;
public ViewHolder(@NonNull View itemView) {
super(itemView);
listTitle.findViewById(R.id.groceryListName);
numItems.findViewById(R.id.numItemsList);
}
}
}
GroceryList Class:
package com.example.myapplication;
import java.util.List;
public class GroceryList {
//class to hold the info of each list item
private String listName;
private int numItems;
//will need to refactor later.....
public GroceryList(String listName, int numItems) {
this.listName = listName;
this.numItems = numItems;
}
public String getListName() {
return listName;
}
public void setListName(String listName) {
this.listName = listName;
}
public int getNumItems() {
return numItems;
}
public void setNumItems(int numItems) {
this.numItems = numItems;
}
}
GroceryList Adapter:
import android.content.Context;
import android.util.Log;
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;
//responsible for displaying data from the model into a row in the recycler view
public class GroceryListAdapter extends RecyclerView.Adapter<GroceryListAdapter.ViewHolder>{
private List<GroceryList> lists = new ArrayList<>();
public GroceryListAdapter(List<GroceryList> lists) {
this.lists = lists;
}
//create a constructor
//onCreateViewHolder is responsible for creating each view
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//create a new view and wrap it inside a new viewholder
//use layout inflator to inflate a view
View groceryListView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grocery_list_entry,parent,false);
Log.d("MEJD", "Iamhere?");
//wrap it inside a ViewHolder and return it
ViewHolder viewHolder = new ViewHolder(groceryListView);
return viewHolder;
}
//onBindViewHolder is responsible for taking data at a particular position and
//putting it into a viewholder
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//get data model by position
GroceryList groceryListItem = lists.get(position);
//set the item views based on the data models
TextView textView = holder.listTitle;
textView.setText(groceryListItem.getListName());
Log.d("MEJD", "Iamhere");
TextView numItemsView = holder.numItems;
numItemsView.setText(groceryListItem.getNumItems());
}
//get number of items in the data
@Override
public int getItemCount() {
return 0;
}
class ViewHolder extends RecyclerView.ViewHolder{
//member variables
public TextView listTitle;
public TextView numItems;
public ViewHolder(@NonNull View itemView) {
super(itemView);
listTitle.findViewById(R.id.groceryListName);
numItems.findViewById(R.id.numItemsList);
}
}
}
XML file for MainActivity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<androidx.appcompat.widget.Toolbar
android:id="@+id/homeToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#27D3C2"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintTop_toTopOf="parent"
app:title="Add Grocery List"
app:titleMarginStart="130dp" />
<ImageButton
android:id="@+id/addButton"
android:layout_width="61dp"
android:layout_height="56dp"
android:layout_alignBottom="@+id/homeToolbar"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="322dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="29dp"
android:layout_marginBottom="0dp"
android:backgroundTint="#00FFFFFF"
android:clickable="true"
android:contentDescription="@+string/add list button"
app:layout_constraintEnd_toEndOf="@+id/homeToolbar"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/homeToolbar"
app:layout_constraintTop_toTopOf="@+id/homeToolbar"
app:srcCompat="@drawable/add_icon"
android:focusable="true" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/homeToolbar"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:layout_marginBottom="1dp" />
</RelativeLayout>
XML for the listItem I want to use:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="15dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="35dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/numItemsList"
android:layout_width="28dp"
android:layout_height="23dp"
android:layout_marginEnd="34dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:text="@string/numberItems"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="15dp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="35dp" />
<TextView
android:id="@+id/groceryListName"
android:layout_width="270dp"
android:layout_height="22dp"
android:layout_marginStart="35dp"
android:layout_marginEnd="44dp"
android:text="@string/groceryListName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/numItemsList"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</details>
# 答案1
**得分**: 0
也许是这样的:
```java
// 获取数据中的项目数量
@Override
public int getItemCount() {
return 0;
}
编辑
// 获取数据中的项目数量
@Override
public int getItemCount() {
return lists.size();
}
英文:
Maybe this:
//get number of items in the data
@Override
public int getItemCount() {
return 0;
}
edit
//get number of items in the data
@Override
public int getItemCount() {
return lists.size();
}
答案2
得分: 0
Add itemCount
//
@Override
public int getItemCount() {
return lists.size();
}
Change ViewHolder class
class ViewHolder extends RecyclerView.ViewHolder{
//member variables
public TextView listTitle;
public TextView numItems;
public ViewHolder(@NonNull View itemView) {
super(itemView);
listTitle = itemView.findViewById(R.id.groceryListName);
numItems = itemView.findViewById(R.id.numItemsList);
}
}
英文:
Add itemCount
//
@Override
public int getItemCount() {
return lists.size();
}
Change ViewHolder class
class ViewHolder extends RecyclerView.ViewHolder{
//member variables
public TextView listTitle;
public TextView numItems;
public ViewHolder(@NonNull View itemView) {
super(itemView);
listTitle = itemView.findViewById(R.id.groceryListName);
numItems = itemView.findViewById(R.id.numItemsList);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论