Prasang February 2016

Android Recyclerview not showing any data

I took this tutorial as reference. Despite of checking everything, no data is appearing on screen ie. main_activity. I've already referred to other pages on stackoverflow regarding this issue. Can't find a viable solution.

NOTE: There is no warning or error while I deploy app on the device.

This is the source code.

MainActivity.java:

public class MainActivity extends Activity
{
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mAdapter = new MyRecyclerViewAdapter(getDataSet());
    mRecyclerView.setAdapter(mAdapter);
    RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(this, LinearLayoutManager.VERTICAL);
    mRecyclerView.addItemDecoration(itemDecoration);

    // Code to Add an item with default animation
    DataObject obj = new DataObject("red", "foo");
    ((MyRecyclerViewAdapter) mAdapter).addItem(obj, 0);

    // Code to remove an item with default animation
    //((MyRecyclerViewAdapter) mAdapter).deleteItem(index);
}

@Override
protected void onResume() {
    super.onResume();
    ((MyRecyclerViewAdapter) mAdapter).setOnItemClickListener(new MyRecyclerViewAdapter.MyClickListener()
    {
        @Override
        public void onItemClick(int position, View v) {
            Toast.makeText(MainActivity.this, "Clicked item: " + position, Toast.LENGTH_SHORT).show();
        }
    });
}

private ArrayList<DataObject> getDataSet() {
    ArrayList results = new Array        

Answers


Dalma Racz February 2016

The problem is in activity_main.xml. You should remove ScrollView and it will work (There's no reason to have one since the recyclerview has an implicit one). There is no scrollview in the tutorial either: check activity_recycler_view.xml.

Post Status

Asked in February 2016
Viewed 2,408 times
Voted 6
Answered 1 times

Search




Leave an answer