Trying to implement RecyclerView into my application, I don't understand why i'm getting "No adapter attached; skipping layout" I thought i clearly set it up in the onCreateView. Some other posts suggested to move it to Oncreate() but when i do that i get many more errors. I followed this tutorial if it helps. Here is my Main Activity class
I do not believe this is a duplicate of another question, although it might be similar. when moved to onCreate() the entire application crashes.
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
private RecyclerView recyclerView;
private Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View layout=inflater.inflate(R.layout.activity_main, container, false);
recyclerView= (RecyclerView) layout.findViewById(R.id.meme_list);
adapter = new Adapter(this,getData());
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
return layout;
}
public static List<Memes> getData(){
List<Memes> data = new ArrayList<>();
String[] names ={"meme1", "meme2", "meme3"};
for(int i=0; i<names.length; i++)
{
Memes meme = new Memes();
meme.name=names[i];
data.add(meme);
}
return data;
}
Here is the Adapter Class
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
private LayoutInflater inflater;
List<Memes> data = Collections.emptyList();
public Adapter(Context context, List<Memes> data){
inflater = LayoutInflater.from(context);
this.data=data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.meme_item, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return null;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Memes current=data.get(position);
holder.title.setText(current.name);
}
@Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView title;
public MyViewHolder(View itemView) {
super(itemView);
title=(TextView)itemView.findViewById(R.id.name);
}
}
main activity xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar">
</include>
<android.support.v7.widget.RecyclerView
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:id="@+id/meme_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
XML for layout of recycler view items
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:layout_gravity="left"
android:padding="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dummy text"/>
(Yes, the app consists of memes, previous version pulled in a lot of users though)