当所有东西都已设置时,RecyclerView不会显示任何内容

时间:2016-02-21 19:00:18

标签: java android android-recyclerview

我目前正在使用来自YouTube的Prabeesh R K制作的方法。我目前正在执行所有步骤而不会错过,我只是稍微修改它,但问题是我的手机中的RecyclerView没有显示任何内容。此外,日志中有一个错误:E / RecyclerView:没有连接适配器;跳过布局。能帮我解决这个问题吗?顺便说一下,这是我的代码

GenreView.java(活动)

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

/**
 * Created by A46CB on 2/21/2016.
 */
public class GenreView extends AppCompatActivity{
RecyclerView rvgenre;
GenreAdapter adaptergenre;
RecyclerView.LayoutManager genrelayoutmanager;
String[] judulgenre;
int []imgres = {R.drawable.w06, R.drawable.w100, R.drawable.w102, R.drawable.w144, R.drawable.w173,
R.drawable.w179, R.drawable.w182, R.drawable.w36, R.drawable.w77, R.drawable.w79, R.drawable.w93,
R.drawable.w97};
ArrayList<GenreContent> arrayList = new ArrayList<GenreContent>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_genre);
    rvgenre = (RecyclerView)findViewById(R.id.genre_recycler_view);
    rvgenre.setHasFixedSize(true);
    judulgenre = getResources().getStringArray(R.array.genre_title);
    int i = 0;
    for(String name : judulgenre)
    {
        GenreContent genrecontent = new GenreContent(imgres[i],judulgenre[i]);
        arrayList.add(genrecontent);
        i++;
    }

    genrelayoutmanager = new GridLayoutManager(this, 2);
    rvgenre.setLayoutManager(genrelayoutmanager);

    adaptergenre = new GenreAdapter(arrayList);
    rvgenre.setAdapter(adaptergenre);
    }
}

GenreAdapter.Java(适配器)

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by A46CB on 2/21/2016.
 */
public class GenreAdapter extends RecyclerView.Adapter<GenreAdapter.GenreRecyclerViewHolder> {
private ArrayList<GenreContent> arrayList = new ArrayList<GenreContent>();

public GenreAdapter(ArrayList<GenreContent> arrayList)
{
    this.arrayList = arrayList;
}
@Override
public GenreRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.genre_layout, parent, false);
    GenreRecyclerViewHolder grvh = new GenreRecyclerViewHolder(view);
    return grvh;
}

@Override
public void onBindViewHolder(GenreRecyclerViewHolder holder, int position) {
    GenreContent genreContent = arrayList.get(position);
    holder.imgView.setImageResource(genreContent.getImg());
    holder.tvgenre.setText(genreContent.getTitle());
}

@Override
public int getItemCount() {
    return arrayList.size();
}

public static class GenreRecyclerViewHolder extends RecyclerView.ViewHolder {
    ImageView imgView;
    TextView tvgenre;
    public GenreRecyclerViewHolder (View view){
        super (view);
        imgView = (ImageView)view.findViewById(R.id.genrepic);
        tvgenre = (TextView)view.findViewById(R.id.titlegenre);
        }
    }
}

GenreContent.java

public class GenreContent {
private int img;
private String title;

public GenreContent(int img, String title){
    this.setImg(img);
    this.setTitle(title);
}

public int getImg() {
    return img;
}

public void setImg(int img) {
    this.img = img;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}
}

我的布局 TabGenre.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/genre_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

GenreLayout.xml(自定义布局)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dip"
android:orientation="vertical">

<ImageView
android:layout_width="160dp"
android:layout_height="120dp"
android:src="@drawable/bgheader"
android:id="@+id/genrepic"/>

<TextView
android:id="@+id/titlegenre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mystery"
android:layout_marginTop="-5dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

你应该改变这一行

adaptergenre = new GenreAdapter(new ArrayList<>(arrayList));

adaptergenre = new GenreAdapter(arrayList);

新的需要创建新的ArrayList