Thorvald
Thorvald

Reputation: 3563

Android - RecyclerView Data not showing up

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

Upvotes: 0

Views: 5886

Answers (2)

Sander Rito
Sander Rito

Reputation: 416

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.

Upvotes: 4

Stefan
Stefan

Reputation: 2178

After adding data to your dataset, you should call

adapter.notifyDataSetChanged();

Upvotes: 1

Related Questions