在 Android Studio 中使用 RecyclerView,但没有任何内容被打印出来。

huangapple 未分类评论44阅读模式
英文:

Using RecyclerView in android studio but nothing is getting printed

问题

以下是已翻译的内容:

这是一个页面,我想在这里打印出从 Parse 仪表板中提取的所有用户名和价格:

  1. public class HomePage extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_home_page);
  6. final RecyclerView list = (RecyclerView)findViewById(R.id.list);
  7. list.setLayoutManager(new LinearLayoutManager(this));
  8. final ArrayList<String> username = new ArrayList<String>();
  9. final ArrayList<String> netPrice = new ArrayList<String>();
  10. ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Image");
  11. query.findInBackground(new FindCallback<ParseObject>() {
  12. @Override
  13. public void done(List<ParseObject> objects, ParseException e) {
  14. if (e == null) {
  15. if (objects.size() > 0) {
  16. for(ParseObject object : objects) {
  17. String user = object.getString("username");
  18. String price = object.getString("price");
  19. username.add(user);
  20. netPrice.add(price);
  21. }
  22. }
  23. } else {
  24. e.printStackTrace();
  25. }
  26. }
  27. });
  28. list.setAdapter(new ListAdapter(username, netPrice));
  29. }
  30. }

这是我的适配器,名为 ListAdapter:

  1. public class ListAdapter extends RecyclerView.Adapter {
  2. private ArrayList<String> data1;
  3. private ArrayList<String> data2;
  4. public ListAdapter(ArrayList<String> data1, ArrayList<String> data2) {
  5. this.data1 = data1;
  6. this.data2 = data2;
  7. }
  8. @NonNull
  9. @Override
  10. public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  11. LayoutInflater inflater = LayoutInflater.from(parent.getContext());
  12. View view = inflater.inflate(R.layout.list_layout, parent, false);
  13. return new ListViewHolder(view);
  14. }
  15. @Override
  16. public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
  17. String title1 = data1.get(position);
  18. final TextView viewById1 = holder.itemView.findViewById(R.id.text1);
  19. viewById1.setText(title1);
  20. String title2 = data2.get(position);
  21. final TextView viewById2 = holder.itemView.findViewById(R.id.text2);
  22. viewById2.setText(title2);
  23. }
  24. @Override
  25. public int getItemCount() {
  26. return data1.size();
  27. }
  28. public class ListViewHolder extends RecyclerView.ViewHolder {
  29. public ListViewHolder(View itemView) {
  30. super(itemView);
  31. }
  32. }
  33. }

虽然没有报错,但什么都没有被打印出来。我在适配器中传递了 ArrayList,这样做是允许的吗?请帮忙,我已经被困在这里尝试了 8 小时。

英文:

This is the page where i want to print all the usernames and price extracted from Parse dashboard

  1. public class HomePage extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_home_page);
  6. final RecyclerView list=(RecyclerView)findViewById(R.id.list);
  7. list.setLayoutManager(new LinearLayoutManager(this));
  8. final ArrayList&lt;String&gt; username = new ArrayList&lt;String&gt;();
  9. final ArrayList&lt;String&gt; netPrice = new ArrayList&lt;String&gt;();
  10. ParseQuery&lt;ParseObject&gt; query=new ParseQuery&lt;ParseObject&gt;(&quot;Image&quot;);
  11. query.findInBackground(new FindCallback&lt;ParseObject&gt;() {
  12. @Override
  13. public void done(List&lt;ParseObject&gt; objects, ParseException e) {
  14. if (e==null){
  15. if (objects.size()&gt;0) {
  16. for(ParseObject object : objects){
  17. String user=object.getString(&quot;username&quot;);
  18. String price=object.getString(&quot;price&quot;);
  19. username.add(user);
  20. netPrice.add(price);
  21. }
  22. }
  23. }
  24. else{
  25. e.printStackTrace();
  26. }
  27. }
  28. });
  29. list.setAdapter(new ListAdapter(username,netPrice));
  30. }
  31. }

This is my Adapter named ListAdapter

  1. public class ListAdapter extends RecyclerView.Adapter {
  2. private ArrayList&lt;String&gt;data1;
  3. private ArrayList&lt;String&gt;data2;
  4. public ListAdapter(ArrayList&lt;String&gt; data1, ArrayList&lt;String&gt; data2){
  5. this.data1=data1;
  6. this.data2=data2;
  7. }
  8. @NonNull
  9. @Override
  10. public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  11. LayoutInflater inflater=LayoutInflater.from(parent.getContext());
  12. View view= inflater.inflate(R.layout.list_layout,parent,false);
  13. return new ListViewHolder(view);
  14. }
  15. @Override
  16. public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
  17. String title1=data1.get(position);
  18. final TextView viewById1 = holder.itemView.findViewById(R.id.text1);
  19. viewById1.setText(title1);
  20. String title2=data2.get(position);
  21. final TextView viewById2 = holder.itemView.findViewById(R.id.text2);
  22. viewById2.setText(title2);
  23. }
  24. @Override
  25. public int getItemCount() {
  26. return data1.size();
  27. }
  28. public class ListViewHolder extends RecyclerView.ViewHolder {
  29. public ListViewHolder(View itemView) {
  30. super(itemView);
  31. }
  32. }
  33. }

This shows no error but nothing is getting printed.
I have passed ArrayList in my Adapter, am I allowed to that?
please help, I am struck badly trying to do this since 8 hrs.

答案1

得分: 1

  1. 你的项目数量应该与你想展示的项目大小相同
  2. @Override
  3. public int getItemCount() {
  4. return 1; // 在这里返回项目的数量
  5. }
英文:

your item count should have the size of the items you want to show

  1. @Override
  2. public int getItemCount() {
  3. return 1; // return here the number of items
  4. }

答案2

得分: 0

问题出在背景查询代码中。

它调用了 query.findInBackground,然后直接调用了 list.setAdapter(...),而此时 [username, netPrice] 仍然是空列表。所以你需要等待,直到 done 方法完成,然后在 RecyclerView 中设置数据。

你可以这样做:

1 - 创建一个名为 setRecyclerViewData 的方法,用于设置 RecyclerView 的数据。

2 - 在 done 方法中的 for each 循环完成后调用 setRecyclerViewData,这样你就可以确保 [username, netPrice] 有一些数据。

  1. query.findInBackground(new FindCallback&lt;ParseObject&gt;() {
  2. @Override
  3. public void done(List&lt;ParseObject&gt; objects, ParseException e) {
  4. if (e == null) {
  5. if (objects.size() &gt; 0) {
  6. for (ParseObject object : objects) {
  7. String user = object.getString(&quot;username&quot;);
  8. String price = object.getString(&quot;price&quot;);
  9. username.add(user);
  10. netPrice.add(price);
  11. }
  12. setRecyclerViewData(username, netPrice);
  13. } else {
  14. Log.d(&quot;Tag&quot;, &quot;Objects size = 0&quot;);
  15. }
  16. } else {
  17. e.printStackTrace();
  18. }
  19. }
  20. });
  1. private void setRecyclerViewData(ArrayList&lt;String&gt; username, ArrayList&lt;String&gt; netPrice) {
  2. list.setAdapter(new ListAdapter(username, netPrice));
  3. }

此外,通常在 RecyclerView 适配器中不会这样做,这样虽然不会显示错误,但不符合约定。

英文:

problem is with the background word.

It is calling query.findInBackground and directly after that list.setAdapter(...) which at this time, [username, netPrice] are still empty lists.
So you need to wait, until you done method is done and then set data in recycleView.

You can do that by:

1 - Create a method setRecyclerViewData which will set the recyclerview data.

2 - call setRecyclerViewData inside done after it finishes the for each loop
that way you will be sure that [username, netPrice] have some data.

  1. query.findInBackground(new FindCallback&lt;ParseObject&gt;() {
  2. @Override
  3. public void done(List&lt;ParseObject&gt; objects, ParseException e) {
  4. if (e==null){
  5. if (objects.size()&gt;0) {
  6. for(ParseObject object : objects){
  7. String user=object.getString(&quot;username&quot;);
  8. String price=object.getString(&quot;price&quot;);
  9. username.add(user);
  10. netPrice.add(price);
  11. }
  12. setRecyclerView(username, netPrice);
  13. }
  14. else{
  15. log.d(&quot;Tag&quot;, &quot;Objects size = 0&quot;);
  16. }
  17. }
  18. else{
  19. e.printStackTrace();
  20. }
  21. }
  22. });
  1. private void setRecyclerView(ArrayList&lt;String&gt; username, ArrayList&lt;String&gt; netPrice){
  2. list.setAdapter(newListAdapter(username,netPrice));
  3. }

plus thing don't usually go this way in recylerview adapter, it is not showing error OK, but this is not the convention.

答案3

得分: 0

空白屏幕出现的原因是因为您的适配器中没有数据,在获取query.findInBackground中的数据后,您没有通知适配器有新数据。

所以,您需要做的是:

首先,将适配器实例存储到一个变量中:

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. ...
  4. final ArrayList<String> username = new ArrayList<String>();
  5. final ArrayList<String> netPrice = new ArrayList<String>();
  6. ListAdapter listAdapter = new ListAdapter(username,netPrice); // 添加这行
  7. ...
  8. list.setAdapter(listAdapter);
  9. }

然后,在您的query.findInBackground中通知适配器有新数据:

  1. query.findInBackground(new FindCallback<ParseObject>() {
  2. @Override
  3. public void done(List<ParseObject> objects, ParseException e) {
  4. if (e==null){
  5. if (objects.size()>0) {
  6. for(ParseObject object : objects){
  7. String user=object.getString("username");
  8. String price=object.getString("price");
  9. username.add(user);
  10. netPrice.add(price);
  11. }
  12. listAdapter.notifyDataSetChanged(); // 添加这行
  13. }
  14. }
  15. else{
  16. e.printStackTrace();
  17. }
  18. }
  19. });
英文:

The reason you are seeing an empty screen is because your adapter contains no data, after getting the data in query.findInBackground, you don't notify the adapter that there is a new data.

So, what you need to do is

First, store your adapter instance into a variable

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. ...
  4. final ArrayList&lt;String&gt; username = new ArrayList&lt;String&gt;();
  5. final ArrayList&lt;String&gt; netPrice = new ArrayList&lt;String&gt;();
  6. ListAdapter listAdapter = new ListAdapter(username,netPrice); // Add this line
  7. ...
  8. list.setAdapter(listAdapter);
  9. }

Then, in your query.findInBackground notify the adapter that there is a new data

  1. query.findInBackground(new FindCallback&lt;ParseObject&gt;() {
  2. @Override
  3. public void done(List&lt;ParseObject&gt; objects, ParseException e) {
  4. if (e==null){
  5. if (objects.size()&gt;0) {
  6. for(ParseObject object : objects){
  7. String user=object.getString(&quot;username&quot;);
  8. String price=object.getString(&quot;price&quot;);
  9. username.add(user);
  10. netPrice.add(price);
  11. }
  12. listAdapter.notifyDataSetChanged(); // Add this line
  13. }
  14. }
  15. else{
  16. e.printStackTrace();
  17. }
  18. }
  19. });

huangapple
  • 本文由 发表于 2020年7月27日 17:49:40
  • 转载请务必保留本文链接:https://java.coder-hub.com/63112743.html
匿名

发表评论

匿名网友

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

确定