项目列表未显示在RecyclerView中

huangapple go评论113阅读模式
英文:

List of items aren't showing up in the RecyclerView

问题

以下是翻译好的内容:

我正在自学安卓应用开发(以及Java)。在弄清楚为什么我的列表项没有显示出来时遇到了麻烦。弹出窗口运行得很好,所以我没有包含它的代码。

我尝试通过添加Log.d()消息来查看GroceryItemAdapter是否正常工作,但它没有显示出来。我猜想是我制作的适配器有问题?

更新:
修复了一行代码,但现在我遇到了这个问题(稍后会更新,要研究一下是什么原因或者进行调试):

AutoCrab的答案帮助解决了空对象问题,我还发现在将整数传递给setText()之前需要将其转换为字符串。

  1. java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法 'android.view.View android.widget.TextView.findViewById(int)'
  2. at com.example.myapplication.GroceryListAdapter$ViewHolder.<init>
  1. package com.example.myapplication;
  2. import android.content.Context;
  3. import android.util.Log;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.TextView;
  8. import androidx.annotation.NonNull;
  9. import androidx.recyclerview.widget.RecyclerView;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. //负责将模型中的数据显示到RecyclerView中的行中
  13. public class GroceryListAdapter extends RecyclerView.Adapter<GroceryListAdapter.ViewHolder>{
  14. private List<GroceryList> lists = new ArrayList<>();
  15. public GroceryListAdapter(List<GroceryList> lists) {
  16. this.lists = lists;
  17. }
  18. @NonNull
  19. @Override
  20. public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  21. View groceryListView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grocery_list_entry, parent, false);
  22. Log.d("MEJD", "我在这里吗?");
  23. ViewHolder viewHolder = new ViewHolder(groceryListView);
  24. return viewHolder;
  25. }
  26. @Override
  27. public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  28. GroceryList groceryListItem = lists.get(position);
  29. TextView textView = holder.listTitle;
  30. textView.setText(groceryListItem.getListName());
  31. Log.d("MEJD", "我在这里");
  32. TextView numItemsView = holder.numItems;
  33. numItemsView.setText(String.valueOf(groceryListItem.getNumItems()));
  34. }
  35. @Override
  36. public int getItemCount() {
  37. return lists.size();
  38. }
  39. class ViewHolder extends RecyclerView.ViewHolder {
  40. public TextView listTitle;
  41. public TextView numItems;
  42. public ViewHolder(@NonNull View itemView) {
  43. super(itemView);
  44. listTitle = itemView.findViewById(R.id.groceryListName);
  45. numItems = itemView.findViewById(R.id.numItemsList);
  46. }
  47. }
  48. }

GroceryList类:

  1. package com.example.myapplication;
  2. import java.util.List;
  3. public class GroceryList {
  4. private String listName;
  5. private int numItems;
  6. public GroceryList(String listName, int numItems) {
  7. this.listName = listName;
  8. this.numItems = numItems;
  9. }
  10. public String getListName() {
  11. return listName;
  12. }
  13. public void setListName(String listName) {
  14. this.listName = listName;
  15. }
  16. public int getNumItems() {
  17. return numItems;
  18. }
  19. public void setNumItems(int numItems) {
  20. this.numItems = numItems;
  21. }
  22. }

GroceryListAdapter适配器:

  1. import android.content.Context;
  2. import android.util.Log;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.TextView;
  7. import androidx.annotation.NonNull;
  8. import androidx.recyclerview.widget.RecyclerView;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. public class GroceryListAdapter extends RecyclerView.Adapter<GroceryListAdapter.ViewHolder> {
  12. private List<GroceryList> lists = new ArrayList<>();
  13. public GroceryListAdapter(List<GroceryList> lists) {
  14. this.lists = lists;
  15. }
  16. @NonNull
  17. @Override
  18. public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  19. View groceryListView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grocery_list_entry, parent, false);
  20. Log.d("MEJD", "我在这里吗?");
  21. ViewHolder viewHolder = new ViewHolder(groceryListView);
  22. return viewHolder;
  23. }
  24. @Override
  25. public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  26. GroceryList groceryListItem = lists.get(position);
  27. TextView textView = holder.listTitle;
  28. textView.setText(groceryListItem.getListName());
  29. Log.d("MEJD", "我在这里");
  30. TextView numItemsView = holder.numItems;
  31. numItemsView.setText(String.valueOf(groceryListItem.getNumItems()));
  32. }
  33. @Override
  34. public int getItemCount() {
  35. return lists.size();
  36. }
  37. class ViewHolder extends RecyclerView.ViewHolder {
  38. public TextView listTitle;
  39. public TextView numItems;
  40. public ViewHolder(@NonNull View itemView) {
  41. super(itemView);
  42. listTitle = itemView.findViewById(R.id.groceryListName);
  43. numItems = itemView.findViewById(R.id.numItemsList);
  44. }
  45. }
  46. }

MainActivity的XML文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <androidx.appcompat.widget.Toolbar
  9. android:id="@+id/homeToolbar"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:background="#27D3C2"
  13. android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
  14. app:layout_constraintTop_toTopOf="parent"
  15. app:title="Add Grocery List"
  16. app:titleMarginStart="130dp" />
  17. <ImageButton
  18. android:id="@+id/addButton"
  19. android:layout_width="61dp"
  20. android:layout_height="56dp"
  21. android:layout_alignBottom="@+id/homeToolbar"
  22. android:layout_alignParentStart="true"
  23. android:layout_alignParentTop="true"
  24. android:layout_alignParentEnd="true"
  25. android:layout_marginStart="322dp"
  26. android:layout_marginTop="0dp"
  27. android:layout_marginEnd="29dp"
  28. android:layout_marginBottom="0dp"
  29. android:backgroundTint="#00FFFFFF"
  30. android:clickable="true"
  31. android:contentDescription="@+string/add list button"
  32. app:layout_constraintEnd_toEndOf="@+id/homeToolbar"
  33. app:layout_constraintHorizontal_bias="1.0"
  34. app:layout_constraintStart_toStartOf="@+id/homeToolbar"
  35. app:layout_constraintTop_toTopOf="@+id/homeToolbar"
  36. app:srcCompat="@drawable/add_icon"
  37. android:focusable="true" />
  38. <android
  39. <details>
  40. <summary>英文:</summary>
  41. 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&#39;t include the code for it.
  42. I tried to see if the `GroceryItemAdapter` is working by adding a `Log.d()` message and it doesn&#39;t show up. I&#39;m guessing it&#39;s the Adapter I made?
  43. **Update:**
  44. Fixed a line, but now I&#39;m getting this(will update this later, going to research what&#39;s the cause or debug it):
  45. AutoCrab&#39;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()`.
  46. java.lang.NullPointerException: Attempt to invoke virtual method &#39;android.view.View android.widget.TextView.findViewById(int)&#39; on a null object reference
  47. at com.example.myapplication.GroceryListAdapter$ViewHolder.&lt;init&gt;
  48. package com.example.myapplication;
  49. import android.content.Context;
  50. import android.util.Log;
  51. import android.view.LayoutInflater;
  52. import android.view.View;
  53. import android.view.ViewGroup;
  54. import android.widget.TextView;
  55. import androidx.annotation.NonNull;
  56. import androidx.recyclerview.widget.RecyclerView;
  57. import java.util.ArrayList;
  58. import java.util.List;
  59. //responsible for displaying data from the model into a row in the recycler view
  60. public class GroceryListAdapter extends RecyclerView.Adapter&lt;GroceryListAdapter.ViewHolder&gt;{
  61. private List&lt;GroceryList&gt; lists = new ArrayList&lt;&gt;();
  62. public GroceryListAdapter(List&lt;GroceryList&gt; lists) {
  63. this.lists = lists;
  64. }
  65. //onCreateViewHolder is responsible for creating each view
  66. @NonNull
  67. @Override
  68. public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  69. //create a new view and wrap it inside a new viewholder
  70. //use layout inflator to inflate a view
  71. View groceryListView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grocery_list_entry,parent,false);
  72. Log.d(&quot;MEJD&quot;, &quot;Iamhere?&quot;);
  73. //wrap it inside a ViewHolder and return it
  74. ViewHolder viewHolder = new ViewHolder(groceryListView);
  75. return viewHolder;
  76. }
  77. //onBindViewHolder is responsible for taking data at a particular position and
  78. //putting it into a viewholder
  79. @Override
  80. public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  81. //get data model by position
  82. GroceryList groceryListItem = lists.get(position);
  83. //set the item views based on the data models
  84. TextView textView = holder.listTitle;
  85. textView.setText(groceryListItem.getListName());
  86. Log.d(&quot;MEJD&quot;, &quot;Iamhere&quot;);
  87. TextView numItemsView = holder.numItems;
  88. numItemsView.setText(groceryListItem.getNumItems());
  89. }
  90. //get number of items in the data
  91. @Override
  92. public int getItemCount() {
  93. lists.size();
  94. }
  95. class ViewHolder extends RecyclerView.ViewHolder{
  96. //member variables
  97. public TextView listTitle;
  98. public TextView numItems;
  99. public ViewHolder(@NonNull View itemView) {
  100. super(itemView);
  101. listTitle.findViewById(R.id.groceryListName);
  102. numItems.findViewById(R.id.numItemsList);
  103. }
  104. }
  105. }
  106. GroceryList Class:
  107. package com.example.myapplication;
  108. import java.util.List;
  109. public class GroceryList {
  110. //class to hold the info of each list item
  111. private String listName;
  112. private int numItems;
  113. //will need to refactor later.....
  114. public GroceryList(String listName, int numItems) {
  115. this.listName = listName;
  116. this.numItems = numItems;
  117. }
  118. public String getListName() {
  119. return listName;
  120. }
  121. public void setListName(String listName) {
  122. this.listName = listName;
  123. }
  124. public int getNumItems() {
  125. return numItems;
  126. }
  127. public void setNumItems(int numItems) {
  128. this.numItems = numItems;
  129. }
  130. }
  131. GroceryList Adapter:
  132. import android.content.Context;
  133. import android.util.Log;
  134. import android.view.LayoutInflater;
  135. import android.view.View;
  136. import android.view.ViewGroup;
  137. import android.widget.TextView;
  138. import androidx.annotation.NonNull;
  139. import androidx.recyclerview.widget.RecyclerView;
  140. import java.util.ArrayList;
  141. import java.util.List;
  142. //responsible for displaying data from the model into a row in the recycler view
  143. public class GroceryListAdapter extends RecyclerView.Adapter&lt;GroceryListAdapter.ViewHolder&gt;{
  144. private List&lt;GroceryList&gt; lists = new ArrayList&lt;&gt;();
  145. public GroceryListAdapter(List&lt;GroceryList&gt; lists) {
  146. this.lists = lists;
  147. }
  148. //create a constructor
  149. //onCreateViewHolder is responsible for creating each view
  150. @NonNull
  151. @Override
  152. public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  153. //create a new view and wrap it inside a new viewholder
  154. //use layout inflator to inflate a view
  155. View groceryListView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grocery_list_entry,parent,false);
  156. Log.d(&quot;MEJD&quot;, &quot;Iamhere?&quot;);
  157. //wrap it inside a ViewHolder and return it
  158. ViewHolder viewHolder = new ViewHolder(groceryListView);
  159. return viewHolder;
  160. }
  161. //onBindViewHolder is responsible for taking data at a particular position and
  162. //putting it into a viewholder
  163. @Override
  164. public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  165. //get data model by position
  166. GroceryList groceryListItem = lists.get(position);
  167. //set the item views based on the data models
  168. TextView textView = holder.listTitle;
  169. textView.setText(groceryListItem.getListName());
  170. Log.d(&quot;MEJD&quot;, &quot;Iamhere&quot;);
  171. TextView numItemsView = holder.numItems;
  172. numItemsView.setText(groceryListItem.getNumItems());
  173. }
  174. //get number of items in the data
  175. @Override
  176. public int getItemCount() {
  177. return 0;
  178. }
  179. class ViewHolder extends RecyclerView.ViewHolder{
  180. //member variables
  181. public TextView listTitle;
  182. public TextView numItems;
  183. public ViewHolder(@NonNull View itemView) {
  184. super(itemView);
  185. listTitle.findViewById(R.id.groceryListName);
  186. numItems.findViewById(R.id.numItemsList);
  187. }
  188. }
  189. }
  190. XML file for MainActivity:
  191. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  192. &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  193. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  194. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  195. android:layout_width=&quot;match_parent&quot;
  196. android:layout_height=&quot;match_parent&quot;
  197. tools:context=&quot;.MainActivity&quot;&gt;
  198. &lt;androidx.appcompat.widget.Toolbar
  199. android:id=&quot;@+id/homeToolbar&quot;
  200. android:layout_width=&quot;match_parent&quot;
  201. android:layout_height=&quot;wrap_content&quot;
  202. android:background=&quot;#27D3C2&quot;
  203. android:theme=&quot;@style/ThemeOverlay.AppCompat.Dark.ActionBar&quot;
  204. app:layout_constraintTop_toTopOf=&quot;parent&quot;
  205. app:title=&quot;Add Grocery List&quot;
  206. app:titleMarginStart=&quot;130dp&quot; /&gt;
  207. &lt;ImageButton
  208. android:id=&quot;@+id/addButton&quot;
  209. android:layout_width=&quot;61dp&quot;
  210. android:layout_height=&quot;56dp&quot;
  211. android:layout_alignBottom=&quot;@+id/homeToolbar&quot;
  212. android:layout_alignParentStart=&quot;true&quot;
  213. android:layout_alignParentTop=&quot;true&quot;
  214. android:layout_alignParentEnd=&quot;true&quot;
  215. android:layout_marginStart=&quot;322dp&quot;
  216. android:layout_marginTop=&quot;0dp&quot;
  217. android:layout_marginEnd=&quot;29dp&quot;
  218. android:layout_marginBottom=&quot;0dp&quot;
  219. android:backgroundTint=&quot;#00FFFFFF&quot;
  220. android:clickable=&quot;true&quot;
  221. android:contentDescription=&quot;@+string/add list button&quot;
  222. app:layout_constraintEnd_toEndOf=&quot;@+id/homeToolbar&quot;
  223. app:layout_constraintHorizontal_bias=&quot;1.0&quot;
  224. app:layout_constraintStart_toStartOf=&quot;@+id/homeToolbar&quot;
  225. app:layout_constraintTop_toTopOf=&quot;@+id/homeToolbar&quot;
  226. app:srcCompat=&quot;@drawable/add_icon&quot;
  227. android:focusable=&quot;true&quot; /&gt;
  228. &lt;androidx.recyclerview.widget.RecyclerView
  229. android:id=&quot;@+id/rvView&quot;
  230. android:layout_width=&quot;match_parent&quot;
  231. android:layout_height=&quot;match_parent&quot;
  232. android:layout_below=&quot;@+id/homeToolbar&quot;
  233. android:layout_alignParentStart=&quot;true&quot;
  234. android:layout_alignParentEnd=&quot;true&quot;
  235. android:layout_alignParentBottom=&quot;true&quot;
  236. android:layout_marginStart=&quot;0dp&quot;
  237. android:layout_marginTop=&quot;0dp&quot;
  238. android:layout_marginEnd=&quot;0dp&quot;
  239. android:layout_marginBottom=&quot;1dp&quot; /&gt;
  240. &lt;/RelativeLayout&gt;
  241. XML for the listItem I want to use:
  242. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  243. &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  244. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  245. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  246. android:id=&quot;@+id/relativeLayout&quot;
  247. android:layout_width=&quot;match_parent&quot;
  248. android:layout_height=&quot;wrap_content&quot;&gt;
  249. &lt;androidx.constraintlayout.widget.Guideline
  250. android:id=&quot;@+id/guideline2&quot;
  251. android:layout_width=&quot;wrap_content&quot;
  252. android:layout_height=&quot;wrap_content&quot;
  253. android:orientation=&quot;vertical&quot;
  254. app:layout_constraintGuide_begin=&quot;15dp&quot;
  255. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  256. app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  257. &lt;androidx.constraintlayout.widget.Guideline
  258. android:id=&quot;@+id/guideline4&quot;
  259. android:layout_width=&quot;wrap_content&quot;
  260. android:layout_height=&quot;wrap_content&quot;
  261. android:orientation=&quot;vertical&quot;
  262. app:layout_constraintGuide_begin=&quot;35dp&quot;
  263. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  264. app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  265. &lt;TextView
  266. android:id=&quot;@+id/numItemsList&quot;
  267. android:layout_width=&quot;28dp&quot;
  268. android:layout_height=&quot;23dp&quot;
  269. android:layout_marginEnd=&quot;34dp&quot;
  270. android:paddingTop=&quot;0dp&quot;
  271. android:paddingBottom=&quot;0dp&quot;
  272. android:text=&quot;@string/numberItems&quot;
  273. android:textSize=&quot;12sp&quot;
  274. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  275. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  276. app:layout_constraintHorizontal_bias=&quot;1.0&quot;
  277. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  278. app:layout_constraintTop_toTopOf=&quot;parent&quot;
  279. app:layout_constraintVertical_bias=&quot;0.0&quot; /&gt;
  280. &lt;androidx.constraintlayout.widget.Guideline
  281. android:id=&quot;@+id/guideline5&quot;
  282. android:layout_width=&quot;wrap_content&quot;
  283. android:layout_height=&quot;wrap_content&quot;
  284. android:orientation=&quot;vertical&quot;
  285. app:layout_constraintGuide_end=&quot;15dp&quot; /&gt;
  286. &lt;androidx.constraintlayout.widget.Guideline
  287. android:id=&quot;@+id/guideline6&quot;
  288. android:layout_width=&quot;wrap_content&quot;
  289. android:layout_height=&quot;wrap_content&quot;
  290. android:orientation=&quot;vertical&quot;
  291. app:layout_constraintGuide_end=&quot;35dp&quot; /&gt;
  292. &lt;TextView
  293. android:id=&quot;@+id/groceryListName&quot;
  294. android:layout_width=&quot;270dp&quot;
  295. android:layout_height=&quot;22dp&quot;
  296. android:layout_marginStart=&quot;35dp&quot;
  297. android:layout_marginEnd=&quot;44dp&quot;
  298. android:text=&quot;@string/groceryListName&quot;
  299. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  300. app:layout_constraintEnd_toStartOf=&quot;@+id/numItemsList&quot;
  301. app:layout_constraintHorizontal_bias=&quot;1.0&quot;
  302. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  303. app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  304. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  305. </details>
  306. # 答案1
  307. **得分**: 0
  308. 也许是这样的:
  309. ```java
  310. // 获取数据中的项目数量
  311. @Override
  312. public int getItemCount() {
  313. return 0;
  314. }

编辑

  1. // 获取数据中的项目数量
  2. @Override
  3. public int getItemCount() {
  4. return lists.size();
  5. }
英文:

Maybe this:

  1. //get number of items in the data
  2. @Override
  3. public int getItemCount() {
  4. return 0;
  5. }

edit

  1. //get number of items in the data
  2. @Override
  3. public int getItemCount() {
  4. return lists.size();
  5. }

答案2

得分: 0

Add itemCount

  1. //
  2. @Override
  3. public int getItemCount() {
  4. return lists.size();
  5. }

Change ViewHolder class

  1. class ViewHolder extends RecyclerView.ViewHolder{
  2. //member variables
  3. public TextView listTitle;
  4. public TextView numItems;
  5. public ViewHolder(@NonNull View itemView) {
  6. super(itemView);
  7. listTitle = itemView.findViewById(R.id.groceryListName);
  8. numItems = itemView.findViewById(R.id.numItemsList);
  9. }
  10. }
英文:

Add itemCount

  1. //
  2. @Override
  3. public int getItemCount() {
  4. return lists.size();
  5. }

Change ViewHolder class

  1. class ViewHolder extends RecyclerView.ViewHolder{
  2. //member variables
  3. public TextView listTitle;
  4. public TextView numItems;
  5. public ViewHolder(@NonNull View itemView) {
  6. super(itemView);
  7. listTitle = itemView.findViewById(R.id.groceryListName);
  8. numItems = itemView.findViewById(R.id.numItemsList);
  9. }
  10. }

huangapple
  • 本文由 发表于 2020年9月16日 09:50:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63912023.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定