Android - RecyclerView Data not showing up

那年仲夏 提交于 2020-01-07 02:28:32

问题


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

# This is the method

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 The Main code

@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;
    }

回答1:


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);

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.

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.




回答2:


After adding data to your dataset, you should call

adapter.notifyDataSetChanged();


来源:https://stackoverflow.com/questions/40685805/android-recyclerview-data-not-showing-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!