当数据来自数据存储时,如何在应用程序上显示recylcer视图项目?

发布于 2025-02-05 09:38:14 字数 1349 浏览 2 评论 0原文

我在片段上使用recylcer视图,并通过查询获取在回收器视图上显示的数据。我想在recycler View上显示有关OPE OPE OPE OPE YouTube和Instagram上的数据的数据,但我不能这样做。当我添加一个按钮并将recyler.setAdapter放入OnClicklister时,它在单击按钮后工作。但是我想在应用程序开始时执行此操作。如果您感到困惑,请查看以下代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view=inflater.inflate(R.layout.fragment_home, container, false);
    recyclerView=view.findViewById(R.id.recyclerView);

    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    arrayList=new ArrayList<>();
  apiQuery();
    adapter=new MyAdapter(getContext(),arrayList);
    view.findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            recyclerView.setAdapter(adapter);
        }
    });

    return view;
}
private void apiQuery(){
    Amplify.API.query(
            ModelQuery.list(Todo.class, Todo.NAME.contains(" ")),
            response -> {
                for (Todo todo : response.getData()) {
                  arrayList.add(todo);
                    Log.i("MyAmplifyApp", todo.getName());
                }
            },
            error -> Log.e("MyAmplifyApp", "Query failure", error)
    );
}

I am using recylcer view on a fragment and fetching the data that to show on recycler view form datastore through query.I want to show the data on recycler view on opeing the app like youtube and instagram but I can't do that. When I add a button and put the recyler.setadapter in onClicklister it worked after the button clicked.But I want to do that on app start.If you are confused then look at the below code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view=inflater.inflate(R.layout.fragment_home, container, false);
    recyclerView=view.findViewById(R.id.recyclerView);

    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    arrayList=new ArrayList<>();
  apiQuery();
    adapter=new MyAdapter(getContext(),arrayList);
    view.findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            recyclerView.setAdapter(adapter);
        }
    });

    return view;
}
private void apiQuery(){
    Amplify.API.query(
            ModelQuery.list(Todo.class, Todo.NAME.contains(" ")),
            response -> {
                for (Todo todo : response.getData()) {
                  arrayList.add(todo);
                    Log.i("MyAmplifyApp", todo.getName());
                }
            },
            error -> Log.e("MyAmplifyApp", "Query failure", error)
    );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剧终人散尽 2025-02-12 09:38:14

您正在按钮单击方法中设置适配器。您可以在Splash屏幕等上预加载数据,然后将数据保存在设备上,然后加载片段/活动时。您可以将适配器设置为加载数据。

You are setting the adapter in the button click method. You can preload the data on splash screen etc and save the data on device and then when your fragment/activity is loaded. You can set the adapter to load the data.

两个我 2025-02-12 09:38:14

收到API的响应后,您必须将数据发送到适配器类。然后更新您的recyclerview适配器列表,然后调用notifydatasetchanged()方法。

请参阅下面的示例代码;

片段

response -> {
                for (Todo todo : response.getData()) {
                  arrayList.add(todo);
                    Log.i("MyAmplifyApp", todo.getName());
                }
                adapter.updateData(arrayList)
            }

在适配器类中的

fun updateData(list: ArrayList<Model>) {
    arraylist.addAll(list)
    notifyDataSetChanged()
}

You have to send data to Adapter class after receiving response from API. Then update your recyclerview adapter's list and call notifyDataSetChanged() method.

Refer below sample code;

In Fragment

response -> {
                for (Todo todo : response.getData()) {
                  arrayList.add(todo);
                    Log.i("MyAmplifyApp", todo.getName());
                }
                adapter.updateData(arrayList)
            }

In Adapter class

fun updateData(list: ArrayList<Model>) {
    arraylist.addAll(list)
    notifyDataSetChanged()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文