Android-未显示RecyclerView数据 [英] Android - RecyclerView Data not showing up

查看:69
本文介绍了Android-未显示RecyclerView数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个RecyclerView显示两个相邻的列表,到目前为止很好,但是我面临的问题是:我将数据存储在公共void方法中,但是当我调用该方法初始化列表时,它不起作用换句话说,这些物品没有显示

I am using one RecyclerView to display two adjacent lists, so far so good but the problem I am facing is : I am storing the data in a public void method but when I call that method to initialize the lists it does not work in other words the items do not show up

#这是方法

public void InitData(){       
    DataHolder item1 = new DataHolder();
    item1.setEnglish("word in eng");
    item1.setGerman("word in ger");
    list.add(item1);

    DataHolder item2 = new DataHolder();
    item2.setEnglish("word in eng");
    item2.setGerman("word in ger");
    list.add(item2);

    DataHolder item3 = new DataHolder();
    item3.setEnglish("word in eng");
    item3.setGerman("word in ger");
    list.add(item3);

    DataHolder item4 = new DataHolder();
    item4.setEnglish("word in eng");
    item4.setGerman("word in ger");
    list.add(item4);

    DataHolder item5 = new DataHolder();
    item5.setEnglish("word in eng");
    item5.setGerman("word in ger");
    list.add(item5);
}

#UPDATE 主要代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_two_fragment, container, false);

        rv = (RecyclerView) view.findViewById(R.id.list_view_m);
        rv.setHasFixedSize(true);
        inputSearch = (EditText) view.findViewById(R.id.inputSearch);

        mAdapter = new DataAdapter(list, getContext());
        rv.setAdapter(mAdapter);
        InitData();

     return view;
    }

推荐答案

首先为RecyclerView添加一个LayoutManager,以告知其如何显示元素.

First add a LayoutManager for the RecyclerView in order to tell it how to display the elements.

第二,我建议先将数据初始化,然后再将其放入适配器

Second, I recommend initializate the data first before putting it in the Adapter

rv = (RecyclerView) view.findViewById(R.id.list_view_m);
rv.setHasFixedSize(true);
//set a vertical layout so the list is displayed top down
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
rv.setLayoutManager(layoutManager);

//initialize the data before binding it to the Adapter
InitData();
mAdapter = new DataAdapter(list, getContext());
rv.setAdapter(mAdapter);

@Stefan的答案是正确的,在更改绑定到RecyclerView的列表的内容之后,您可以使用 mAdapter.notifyDataSetChanged(); ,并且如果它已对您的代码起作用在RecyclerView中安装了LinearLayoutManager.

The answer from @Stefan is correct, you use mAdapter.notifyDataSetChanged(); after you have changed the content of the list binded to the RecyclerView, and it would have worked on your code if it had had the LinearLayoutManager in the RecyclerView.

但是在将其绑定到适配器之前最好有一个完整(或部分)列表,因此RecyclerView会显示开头的内容以及更改列表的元素(添加,编辑或删除)之后,调用 mAdapter.notifyDataSetChanged(); ,以便RecyclerView自动更新显示.

But its better to have a complete (or partial) list before binding it to an Adapter, so the RecyclerView shows the content from the beggining, and after you have changed the elements of the list (add, edit or delete), call the mAdapter.notifyDataSetChanged(); so the RecyclerView updates the display automatically.

这篇关于Android-未显示RecyclerView数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆