RecyclerView没有正确显示ArrayList。

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

RecyclerView not displaying ArrayList correctly

问题

  1. Shopping_List
  2. package com.stu54259.plan2cook.Model;
  3. public class Shopping_List {
  4. private int id;
  5. private String ingredient_type;
  6. private String ingredient_name;
  7. private Double quantity;
  8. private String measurement_name;
  9. public Shopping_List() {
  10. }
  11. public Shopping_List(String ingredient_type, String ingredient_name, Double quantity, String measurement_name) {
  12. this.ingredient_type = ingredient_type;
  13. this.ingredient_name = ingredient_name;
  14. this.quantity = quantity;
  15. this.measurement_name = measurement_name;
  16. }
  17. public Shopping_List(int id, String ingredient_type, String ingredient_name, Double quantity, String measurement_name) {
  18. this.id = id;
  19. this.ingredient_type = ingredient_type;
  20. this.ingredient_name = ingredient_name;
  21. this.quantity = quantity;
  22. this.measurement_name = measurement_name;
  23. }
  24. public int getId() {
  25. return id;
  26. }
  27. public void setId(int id) {
  28. this.id = id;
  29. }
  30. public String getIngredient_type() {
  31. return ingredient_type;
  32. }
  33. public void setIngredient_type(String ingredient_type) {
  34. this.ingredient_type = ingredient_type;
  35. }
  36. public String getIngredient_name() {
  37. return ingredient_name;
  38. }
  39. public void setIngredient_name(String ingredient_name) {
  40. this.ingredient_name = ingredient_name;
  41. }
  42. public Double getQuantity() {
  43. return quantity;
  44. }
  45. public void setQuantity(Double quantity) {
  46. this.quantity = quantity;
  47. }
  48. public String getMeasurement_name() {
  49. return measurement_name;
  50. }
  51. public void setMeasurement_name(String measurement_name) {
  52. this.measurement_name = measurement_name;
  53. }
  54. }
  55. Activity
  56. public class ShoppingList extends MainActivity {
  57. ShoppingListAdapter adapterRecipe;
  58. List<Shopping_List> shopList = new ArrayList<>();
  59. RecyclerView listIngredient;
  60. SQLiteDatabase db;
  61. Cursor c;
  62. EditText edittext;
  63. String search;
  64. protected void onCreate(Bundle savedInstanceState) {
  65. super.onCreate(savedInstanceState);
  66. setContentView(R.layout.shopping_list);
  67. edittext = findViewById(R.id.editPlanName);
  68. edittext.setOnKeyListener(new View.OnKeyListener() {
  69. public boolean onKey(View v, int keyCode, KeyEvent event) {
  70. search = edittext.getText().toString();
  71. Log.d("Search value", search);
  72. if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
  73. (keyCode == KeyEvent.KEYCODE_ENTER)) {
  74. loadIngredient();
  75. adapterRecipe.notifyDataSetChanged();
  76. return true;
  77. }
  78. return false;
  79. }
  80. });
  81. BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
  82. navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
  83. @Override
  84. public boolean onNavigationItemSelected(@NonNull MenuItem item) {
  85. switch (item.getItemId()) {
  86. case R.id.home:
  87. Intent a = new Intent(ShoppingList.this, MainActivity.class);
  88. startActivity(a);
  89. break;
  90. case R.id.recipes:
  91. Intent b = new Intent(ShoppingList.this, RecipeSearch.class);
  92. startActivity(b);
  93. break;
  94. case R.id.shoppingList:
  95. Intent c = new Intent(ShoppingList.this, ShoppingList.class);
  96. startActivity(c);
  97. break;
  98. case R.id.mealPlan:
  99. Intent d = new Intent(ShoppingList.this, MenuPlan.class);
  100. startActivity(d);
  101. break;
  102. case R.id.reminder:
  103. Intent e = new Intent(ShoppingList.this, Reminders.class);
  104. startActivity(e);
  105. break;
  106. }
  107. return false;
  108. }
  109. });
  110. adapterRecipe = new ShoppingListAdapter(this, shopList);
  111. listIngredient = findViewById(R.id.listIngredient);
  112. RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this,
  113. LinearLayoutManager.VERTICAL, false);
  114. listIngredient.setLayoutManager(mLayoutManager);
  115. listIngredient.setItemAnimator(new DefaultItemAnimator());
  116. listIngredient.setAdapter(adapterRecipe);
  117. }
  118. public void loadIngredient() {
  119. shopList.clear();
  120. db = (new DatabaseManager(this).getWritableDatabase());
  121. String RECIPE_SEARCH =
  122. "SELECT SUM(A.ingredient_quantity) quantity, A.ingredient ingredient_name, A.recipe, B.ingredient_type, B.measurement_name, C.id, D.plan_name " +
  123. "FROM " + DatabaseManager.TABLE_QUANTITY + " AS A JOIN " + DatabaseManager.TABLE_INGREDIENTS + " AS B ON A.ingredient = B.ingredient_name " +
  124. "JOIN " + DatabaseManager.TABLE_PLAN_RECIPES + " AS C ON A.recipe = C.recipe_name " +
  125. "JOIN " + DatabaseManager.TABLE_MEAL_PLAN + " AS D ON C.id = D.plan_recipe " +
  126. "WHERE D.plan_name LIKE ? GROUP BY A.ingredient";
  127. Log.d("Search query", RECIPE_SEARCH);
  128. c = db.rawQuery(RECIPE_SEARCH, new String[]{"%" + search + "%"});
  129. if (c.moveToFirst()) {
  130. do {
  131. Shopping_List shopping_list = new Shopping_List();
  132. shopping_list.setQuantity(c.getDouble(c.getColumnIndex("quantity")));
  133. shopping_list.setIngredient_name(c.getString(c.getColumnIndex("ingredient_name")));
  134. shopping_list.setIngredient_type(c.getString(c.getColumnIndex("ingredient_type")));
  135. shopping_list.setMeasurement_name(c.getString(c.getColumnIndex("measurement_name")));
  136. shopList.add(shopping_list);
  137. } while (c.moveToNext());
  138. }
  139. c.close();
  140. db.close();
  141. }
  142. }
  143. Adapter
  144. public class ShoppingListAdapter extends RecyclerView.Adapter<com.stu54259.plan2cook.Adapters.ShoppingListAdapter.ViewHolder> {
  145. private List<Shopping_List> shopList;
  146. private LayoutInflater mInflater;
  147. private com.stu54259.plan2cook.Adapters.RecyclerViewAdapter.ItemClickListener mClickListener;
  148. public ShoppingListAdapter(Context context, List<Shopping_List> data) {
  149. this.mInflater = LayoutInflater.from(context);
  150. this.shopList = data;
  151. }
  152. @Override
  153. public com.stu54259.plan2cook.Adapters.ShoppingListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  154. View view = mInflater.inflate(R.layout.fragment_item, parent, false);
  155. return new com.stu54259.plan2cook.Adapters.ShoppingListAdapter.ViewHolder(view);
  156. }
  157. @Override
  158. public void onBindViewHolder(com.stu54259.plan2cook.Adapters.ShoppingListAdapter.ViewHolder holder, int position) {
  159. if (shopList.get(position) != null) {
  160. holder.myTextView.setText(shopList.get(position).toString());
  161. }
  162. }
  163. @Override
  164. public int getItemCount() {
  165. return shopList.size();
  166. }
  167. public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
  168. TextView myTextView;
  169. ViewHolder(View itemView) {
  170. super(itemView);
  171. myTextView = itemView.findViewById(R.id.quantity);
  172. itemView.setOnClickListener(this);
  173. }
  174. @Override
  175. public void onClick(View view) {
  176. if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
  177. }
  178. }
  179. void setClickListener(com.stu54259.plan2cook.Adapters.RecyclerViewAdapter.ItemClickListener itemClickListener) {
  180. this.mClickListener = itemClickListener;
  181. }
  182. public interface ItemClickListener {
  183. void onItemClick(View view, int position);
  184. }
  185. }
英文:

For some reason the only thing displayed in my RecyclerView is com.stu54259.plan2cook.Model.Shopping_list@5cb7482 repeated with various end codes not the contents of the ArrayList. Any suggestions must be something with the recylerview adapter. Can add xml etc if need be but i'm sure I've just missed something stupid.

Shopping_List class

  1. package com.stu54259.plan2cook.Model;
  2. public class Shopping_List {
  3. private int id;
  4. private String ingredient_type;
  5. private String ingredient_name;
  6. private Double quantity;
  7. private String measurement_name;
  8. public Shopping_List() {
  9. }
  10. public Shopping_List(String ingredient_type, String ingredient_name, Double quantity, String measurement_name) {
  11. this.ingredient_type = ingredient_type;
  12. this.ingredient_name = ingredient_name;
  13. this.quantity = quantity;
  14. this.measurement_name = measurement_name;
  15. }
  16. public Shopping_List(int id, String ingredient_type, String ingredient_name, Double quantity, String measurement_name) {
  17. this.id = id;
  18. this.ingredient_type = ingredient_type;
  19. this.ingredient_name = ingredient_name;
  20. this.quantity = quantity;
  21. this.measurement_name = measurement_name;
  22. }
  23. public int getId() {
  24. return id;
  25. }
  26. public void setId(int id) {
  27. this.id = id;
  28. }
  29. public String getIngredient_type() {
  30. return ingredient_type;
  31. }
  32. public void setIngredient_type(String ingredient_type) {
  33. this.ingredient_type = ingredient_type;
  34. }
  35. public String getIngredient_name() {
  36. return ingredient_name;
  37. }
  38. public void setIngredient_name(String ingredient_name) {
  39. this.ingredient_name = ingredient_name;
  40. }
  41. public Double getQuantity() {
  42. return quantity;
  43. }
  44. public void setQuantity(Double quantity) {
  45. this.quantity = quantity;
  46. }
  47. public String getMeasurement_name() {
  48. return measurement_name;
  49. }
  50. public void setMeasurement_name(String measurement_name) {
  51. this.measurement_name = measurement_name;
  52. }
  53. }

Activity

  1. public class ShoppingList extends MainActivity {
  2. ShoppingListAdapter adapterRecipe;
  3. List&lt;Shopping_List&gt; shopList = new ArrayList&lt;&gt;();
  4. RecyclerView listIngredient;
  5. SQLiteDatabase db;
  6. Cursor c;
  7. EditText edittext;
  8. String search;
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.shopping_list);
  12. edittext = findViewById(R.id.editPlanName);
  13. edittext.setOnKeyListener(new View.OnKeyListener() {
  14. public boolean onKey(View v, int keyCode, KeyEvent event) {
  15. search = edittext.getText().toString();
  16. Log.d(&quot;Search value&quot;, search);
  17. if ((event.getAction() == KeyEvent.ACTION_DOWN) &amp;&amp;
  18. (keyCode == KeyEvent.KEYCODE_ENTER)) {
  19. loadIngredient();
  20. adapterRecipe.notifyDataSetChanged();
  21. return true;
  22. }
  23. return false;
  24. }
  25. });
  26. BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
  27. navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
  28. @Override
  29. public boolean onNavigationItemSelected(@NonNull MenuItem item) {
  30. switch (item.getItemId()) {
  31. case R.id.home:
  32. Intent a = new Intent(ShoppingList.this,MainActivity.class);
  33. startActivity(a);
  34. break;
  35. case R.id.recipes:
  36. Intent b = new Intent(ShoppingList.this,RecipeSearch.class);
  37. startActivity(b);
  38. break;
  39. case R.id.shoppingList:
  40. Intent c = new Intent(ShoppingList.this, ShoppingList.class);
  41. startActivity(c);
  42. break;
  43. case R.id.mealPlan:
  44. Intent d = new Intent(ShoppingList.this, MenuPlan.class);
  45. startActivity(d);
  46. break;
  47. case R.id.reminder:
  48. Intent e = new Intent(ShoppingList.this, Reminders.class);
  49. startActivity(e);
  50. break;
  51. }
  52. return false;
  53. }
  54. });
  55. adapterRecipe = new ShoppingListAdapter(this, shopList);
  56. listIngredient = findViewById(R.id.listIngredient);
  57. RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this,
  58. LinearLayoutManager.VERTICAL, false);
  59. listIngredient.setLayoutManager(mLayoutManager);
  60. listIngredient.setItemAnimator(new DefaultItemAnimator());
  61. listIngredient.setAdapter(adapterRecipe);
  62. }
  63. public void loadIngredient() {
  64. shopList.clear();
  65. db = (new DatabaseManager(this).getWritableDatabase());
  66. String RECIPE_SEARCH =
  67. &quot;SELECT SUM(A.ingredient_quantity) quantity, A.ingredient ingredient_name, A.recipe, B.ingredient_type, B.measurement_name, C.id, D.plan_name &quot; +
  68. &quot;FROM &quot; + DatabaseManager.TABLE_QUANTITY + &quot; AS A JOIN &quot; + DatabaseManager.TABLE_INGREDIENTS + &quot; AS B ON A.ingredient = B.ingredient_name &quot; +
  69. &quot;JOIN &quot; + DatabaseManager.TABLE_PLAN_RECIPES + &quot; AS C ON A.recipe = C.recipe_name &quot; +
  70. &quot;JOIN &quot; + DatabaseManager.TABLE_MEAL_PLAN + &quot; AS D ON C.id = D.plan_recipe &quot; +
  71. &quot;WHERE D.plan_name LIKE ? GROUP BY A.ingredient&quot;;
  72. Log.d(&quot;Search query&quot;, RECIPE_SEARCH);
  73. c = db.rawQuery(RECIPE_SEARCH, new String[]{&quot;%&quot; + search + &quot;%&quot;});
  74. if (c.moveToFirst()) {
  75. do {
  76. Shopping_List shopping_list = new Shopping_List();
  77. shopping_list.setQuantity(c.getDouble(c.getColumnIndex(&quot;quantity&quot;)));
  78. shopping_list.setIngredient_name(c.getString(c.getColumnIndex(&quot;ingredient_name&quot;)));
  79. shopping_list.setIngredient_type(c.getString(c.getColumnIndex(&quot;ingredient_type&quot;)));
  80. shopping_list.setMeasurement_name(c.getString(c.getColumnIndex(&quot;measurement_name&quot;)));
  81. shopList.add(shopping_list);
  82. } while (c.moveToNext());
  83. }
  84. c.close();
  85. db.close();
  86. }

}

Adapter

  1. public class ShoppingListAdapter extends RecyclerView.Adapter&lt;com.stu54259.plan2cook.Adapters.ShoppingListAdapter.ViewHolder&gt; {
  2. private List&lt;Shopping_List&gt; shopList;
  3. private LayoutInflater mInflater;
  4. private com.stu54259.plan2cook.Adapters.RecyclerViewAdapter.ItemClickListener mClickListener;
  5. // data is passed into the constructor
  6. public ShoppingListAdapter(Context context, List&lt;Shopping_List&gt; data) {
  7. this.mInflater = LayoutInflater.from(context);
  8. this.shopList = data;
  9. }
  10. // inflates the row layout from xml when needed
  11. @Override
  12. public com.stu54259.plan2cook.Adapters.ShoppingListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  13. View view = mInflater.inflate(R.layout.fragment_item, parent, false);
  14. return new com.stu54259.plan2cook.Adapters.ShoppingListAdapter.ViewHolder(view);
  15. }
  16. // binds the data to the TextView in each row
  17. @Override
  18. public void onBindViewHolder(com.stu54259.plan2cook.Adapters.ShoppingListAdapter.ViewHolder holder, int position) {
  19. if(shopList.get(position) != null)
  20. {
  21. holder.myTextView.setText(shopList.get(position).toString());
  22. }
  23. }
  24. // total number of rows
  25. @Override
  26. public int getItemCount() {
  27. return shopList.size();
  28. }
  29. // stores and recycles views as they are scrolled off screen
  30. public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
  31. TextView myTextView;
  32. ViewHolder(View itemView) {
  33. super(itemView);
  34. myTextView = itemView.findViewById(R.id.quantity);
  35. itemView.setOnClickListener(this);
  36. }
  37. @Override
  38. public void onClick(View view) {
  39. if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
  40. }
  41. }
  42. // allows clicks events to be caught
  43. void setClickListener(com.stu54259.plan2cook.Adapters.RecyclerViewAdapter.ItemClickListener itemClickListener) {
  44. this.mClickListener = itemClickListener;
  45. }
  46. // parent activity will implement this method to respond to click events
  47. public interface ItemClickListener {
  48. void onItemClick(View view, int position);
  49. }
  50. }

答案1

得分: 1

在你的Shopping_List类中添加这个方法,这样当你对一个Shopping_List实例使用toString()时,你将会得到它的所有属性,这些属性会用空格分隔:

  1. public String toString() {
  2. return ingredient_name + " " + ingredient_type + " " + quantity + " " + measurement_name;
  3. }

你可以更改属性的顺序。

英文:

Add this method in your Shopping_List class, so when you use toString() for a Shopping_List instance you will get all its properties separated by spaces:

  1. public String toString() {
  2. return ingredient_name + &quot; &quot; + ingredient_type + &quot; &quot; + quantity + &quot; &quot; + measurement_name;
  3. }

You can change the order of the properties.

答案2

得分: 0

你在onBindViewHolder方法中的代码有误。你应该使用来自Shopping_List对象的某个字段设置文本:

  1. @Override
  2. public void onBindViewHolder(com.stu54259.plan2cook.Adapters.ShoppingListAdapter.ViewHolder holder, int position) {
  3. if (shopList.get(position) != null) {
  4. holder.myTextView.setText(shopList.get(position).toString());
  5. }
  6. }

你没有在这里放入Shopping_List对象,但如果你有类似这样的代码:

  1. public class Shopping_List {
  2. public String title;
  3. public String getTitle() {
  4. return title;
  5. }
  6. }

那么你应该这样做:

  1. @Override
  2. public void onBindViewHolder(com.stu54259.plan2cook.Adapters.ShoppingListAdapter.ViewHolder holder, int position) {
  3. if (shopList.get(position) != null) {
  4. holder.myTextView.setText(shopList.get(position).getTitle());
  5. }
  6. }
英文:

You have wrong code in onBindViewHolder method. You should set text with some field from Shopping_List object:

  1. @Override
  2. public void onBindViewHolder(com.stu54259.plan2cook.Adapters.ShoppingListAdapter.ViewHolder holder, int position) {
  3. if(shopList.get(position) != null)
  4. {
  5. holder.myTextView.setText(shopList.get(position).toString());
  6. }
  7. }

You haven't put the Shopping_List object here, but if you have something like this:

  1. public class Shopping_List {
  2. public String title;
  3. public String getTitle() {
  4. return title;
  5. }
  6. }

Then you should do something like this:

  1. @Override
  2. public void onBindViewHolder(com.stu54259.plan2cook.Adapters.ShoppingListAdapter.ViewHolder holder, int position) {
  3. if(shopList.get(position) != null)
  4. {
  5. holder.myTextView.setText(shopList.get(position).getTitle());
  6. }
  7. }

答案3

得分: 0

虽然它并没有提供直接的解决方案,但我建议您使用groupie库。这很可能会消除您的错误,并减少样板代码和复杂性。

英文:

Although it doesn't give a direct solution, I would suggest that you use the groupie library. It will most likely remove your error and reduce boilerplate code and complexity.

huangapple
  • 本文由 发表于 2020年10月4日 02:46:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64187792.html
匿名

发表评论

匿名网友

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

确定