Paul Joiner Paul Joiner - 5 months ago 26
Android Question

Error displaying content of RecyclerView (loads blank screen)

I'm very new to Android development but I am trying to build a simple app with a RecyclerView. I've followed instructions from a few different tutorials but whenever I launch the app, all I get is an empty white screen. Any ideas on where I might be going wrong? Am I missing some code? Thank you in advance.

Here is my RecyclerViewAdapter:

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.Collections;
import java.util.List;


public class Recycler_View_Adapter extends RecyclerView.Adapter<View_Holder> {
List<Data> list = Collections.emptyList();
Context context;
@Override
public View_Holder onCreateViewHolder(ViewGroup parent, int viewType) {
//Inflate the layout, initialize the View Holder
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
View_Holder holder = new View_Holder(v);
return holder;
}
@Override
public void onBindViewHolder(View_Holder holder, int position) {

//Use the provided View Holder on the onCreateViewHolder method to populate the current row on the RecyclerView
holder.title.setText(list.get(position).title);
holder.description.setText(list.get(position).description);
holder.imageView.setImageResource(list.get(position).imageId);

//animate(holder);
}
@Override
public int getItemCount() {
//returns the number of elements the RecyclerView will display
return list.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
// Insert a new item to the RecyclerView on a predefined position
public void insert(int position, Data data) {
list.add(position, data);
notifyItemInserted(position);
}
// Remove a RecyclerView item containing a specified Data object
public void remove(Data data) {
int position = list.indexOf(data);
list.remove(position);
notifyItemRemoved(position);
}

}


Here is the code for my MainActivity

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity {
private RecyclerView mRecyclerView;
private List<Data> data;

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


List<Data> data = fill_with_data();

Recycler_View_Adapter adapter = new Recycler_View_Adapter();
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

}
public List<Data> fill_with_data() {

List<Data> data = new ArrayList<>();

data.add(new Data("FTWT item", "ftwt item description ", R.drawable.ftwt1));
data.add(new Data("Ftwt item 2", "ftwt item2 description ", R.drawable.ftwt2));
data.add(new Data("ftwt merch", "ftwt merch description", R.drawable.ftwt_merch1));
data.add(new Data("luca item 1", "luca item1 description", R.drawable.luca1));
data.add(new Data("luca merch1", "luca merch1 description", R.drawable.luca_shirt1));
data.add(new Data("luca merch2", "luca merch2 item description ", R.drawable.luca_shirt2));

return data;
}
}

Answer

You don't seem to pass data to your adapter.

Change this

Recycler_View_Adapter adapter = new Recycler_View_Adapter();

to

Recycler_View_Adapter adapter = new Recycler_View_Adapter(data);

In your Recycler_View_Adapter list is always empty right now. Add a constructor to accept data:

public Recycler_View_Adapter(List<Data> data){
    list = data;
}