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

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

Using RecyclerView in android studio but nothing is getting printed

问题

以下是已翻译的内容:

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

public class HomePage extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);
        final RecyclerView list = (RecyclerView)findViewById(R.id.list);
        list.setLayoutManager(new LinearLayoutManager(this));

        final ArrayList<String> username = new ArrayList<String>();
        final ArrayList<String> netPrice = new ArrayList<String>();

        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Image");
        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, ParseException e) {
                if (e == null) {
                    if (objects.size() > 0) {
                        for(ParseObject object : objects) {
                            String user = object.getString("username");
                            String price = object.getString("price");
                            username.add(user);
                            netPrice.add(price);
                        }
                    }
                } else {
                    e.printStackTrace();
                }
            }
        });
        list.setAdapter(new ListAdapter(username, netPrice));
    }
}

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

public class ListAdapter extends RecyclerView.Adapter {
    private ArrayList<String> data1;
    private ArrayList<String> data2;

    public ListAdapter(ArrayList<String> data1, ArrayList<String> data2) {
        this.data1 = data1;
        this.data2 = data2;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.list_layout, parent, false);
        return new ListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        String title1 = data1.get(position);
        final TextView viewById1 = holder.itemView.findViewById(R.id.text1);
        viewById1.setText(title1);
        String title2 = data2.get(position);
        final TextView viewById2 = holder.itemView.findViewById(R.id.text2);
        viewById2.setText(title2);
    }

    @Override
    public int getItemCount() {
        return data1.size();
    }

    public class ListViewHolder extends RecyclerView.ViewHolder {
        public ListViewHolder(View itemView) {
            super(itemView);
        }
    }
}

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

英文:

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

public class HomePage extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
final RecyclerView list=(RecyclerView)findViewById(R.id.list);
list.setLayoutManager(new LinearLayoutManager(this));
final ArrayList&lt;String&gt; username = new ArrayList&lt;String&gt;();
final ArrayList&lt;String&gt; netPrice = new ArrayList&lt;String&gt;();
ParseQuery&lt;ParseObject&gt; query=new ParseQuery&lt;ParseObject&gt;(&quot;Image&quot;);
query.findInBackground(new FindCallback&lt;ParseObject&gt;() {
@Override
public void done(List&lt;ParseObject&gt; objects, ParseException e) {
if (e==null){
if (objects.size()&gt;0) {
for(ParseObject object : objects){
String user=object.getString(&quot;username&quot;);
String price=object.getString(&quot;price&quot;);
username.add(user);
netPrice.add(price);
}
}
}
else{
e.printStackTrace();
}
}
});
list.setAdapter(new ListAdapter(username,netPrice));
}
}

This is my Adapter named ListAdapter

public class ListAdapter extends RecyclerView.Adapter {
private ArrayList&lt;String&gt;data1;
private ArrayList&lt;String&gt;data2;
public ListAdapter(ArrayList&lt;String&gt; data1, ArrayList&lt;String&gt; data2){
this.data1=data1;
this.data2=data2;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater=LayoutInflater.from(parent.getContext());
View view= inflater.inflate(R.layout.list_layout,parent,false);
return new ListViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
String title1=data1.get(position);
final TextView viewById1 = holder.itemView.findViewById(R.id.text1);
viewById1.setText(title1);
String title2=data2.get(position);
final TextView viewById2 = holder.itemView.findViewById(R.id.text2);
viewById2.setText(title2);
}
@Override
public int getItemCount() {
return data1.size();
}
public class ListViewHolder extends RecyclerView.ViewHolder {
public ListViewHolder(View itemView) {
super(itemView);
}
}
}

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

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

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

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

答案2

得分: 0

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

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

你可以这样做:

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

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

query.findInBackground(new FindCallback&lt;ParseObject&gt;() {
    @Override
    public void done(List&lt;ParseObject&gt; objects, ParseException e) {
        if (e == null) {
            if (objects.size() &gt; 0) {
                for (ParseObject object : objects) {
                    String user = object.getString(&quot;username&quot;);
                    String price = object.getString(&quot;price&quot;);
                    username.add(user);
                    netPrice.add(price);
                }
                setRecyclerViewData(username, netPrice);
            } else {
                Log.d(&quot;Tag&quot;, &quot;Objects size = 0&quot;);
            }
        } else {
            e.printStackTrace();
        }
    }
});
private void setRecyclerViewData(ArrayList&lt;String&gt; username, ArrayList&lt;String&gt; netPrice) {
    list.setAdapter(new ListAdapter(username, netPrice));
}

此外,通常在 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.

query.findInBackground(new FindCallback&lt;ParseObject&gt;() {
    @Override
    public void done(List&lt;ParseObject&gt; objects, ParseException e) {
        if (e==null){
            if (objects.size()&gt;0) {
                for(ParseObject object : objects){
                    String user=object.getString(&quot;username&quot;);
                    String price=object.getString(&quot;price&quot;);
                    username.add(user);
                    netPrice.add(price);
                  }
                  setRecyclerView(username, netPrice);
             }
             else{
                 log.d(&quot;Tag&quot;, &quot;Objects size = 0&quot;);
             }
         }
         else{
             e.printStackTrace();
         }
    }
});
private void setRecyclerView(ArrayList&lt;String&gt; username, ArrayList&lt;String&gt; netPrice){
    list.setAdapter(newListAdapter(username,netPrice));
}

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中的数据后,您没有通知适配器有新数据。

所以,您需要做的是:

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    final ArrayList<String> username = new ArrayList<String>();
    final ArrayList<String> netPrice = new ArrayList<String>();
    ListAdapter listAdapter = new ListAdapter(username,netPrice); // 添加这行
    ...
    list.setAdapter(listAdapter);
}

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

query.findInBackground(new FindCallback<ParseObject>() {
    @Override
    public void done(List<ParseObject> objects, ParseException e) {
        if (e==null){
            if (objects.size()>0) {
                for(ParseObject object : objects){
                    String user=object.getString("username");
                    String price=object.getString("price");
                    username.add(user);
                    netPrice.add(price);
                }
                listAdapter.notifyDataSetChanged(); // 添加这行
            }

        }
        else{
            e.printStackTrace();
        }

    }
});
英文:

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

    @Override
protected void onCreate(Bundle savedInstanceState) {
...
final ArrayList&lt;String&gt; username = new ArrayList&lt;String&gt;();
final ArrayList&lt;String&gt; netPrice = new ArrayList&lt;String&gt;();
ListAdapter listAdapter = new ListAdapter(username,netPrice); // Add this line
...
list.setAdapter(listAdapter);
}

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

        query.findInBackground(new FindCallback&lt;ParseObject&gt;() {
@Override
public void done(List&lt;ParseObject&gt; objects, ParseException e) {
if (e==null){
if (objects.size()&gt;0) {
for(ParseObject object : objects){
String user=object.getString(&quot;username&quot;);
String price=object.getString(&quot;price&quot;);
username.add(user);
netPrice.add(price);
}
listAdapter.notifyDataSetChanged(); // Add this line
}
}
else{
e.printStackTrace();
}
}
});

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

发表评论

匿名网友

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

确定