简体   繁体   中英

Recycler View and Card View not displaying cards

This is my Activity:

public String id;
public String passPhrase;
public ArrayList<SongCell> songs;

private RecyclerView recyclerView;
private MyAdapter myAdapter;
private RecyclerView.LayoutManager layoutManager;


@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.songs_view_layout);

    Context context = this.getApplicationContext();
    Bundle stateData = getIntent().getExtras();

    try
    {
        id = stateData.getString("id");
        passPhrase = stateData.getString("passPhrase");

        ArrayList data;

        recyclerView = (RecyclerView) findViewById(R.id.song_list);
        recyclerView.setHasFixedSize(true);

        layoutManager = new LinearLayoutManager(this);

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        data = new ArrayList<SongCell>();

        for (int i=0; i<5;i++)
        {
            data.add(new SongCell("Song "+i,"Artist "+i, null));
        }


        myAdapter = new MyAdapter(data);
        recyclerView.setAdapter(myAdapter);

    }
    catch(Exception e)
    {
        Log.d("Error: ", e.toString());
    }

}

card.xml

<android.support.v7.widget.CardView android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="6dp"
        >

        <ImageView
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:id="@+id/song_photo"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="16dp"
            android:background="@drawable/error" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/song_name"
            android:textSize="30sp"
            android:text="Song Name"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:textColor="#000000" />

        <ImageButton
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:id="@+id/vote_button"
            android:layout_alignParentRight="true"
            android:layout_marginRight="8dp"
            android:background="@drawable/arrow" />

    </RelativeLayout>

</android.support.v7.widget.CardView>

Activity.xml

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

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:paddingBottom="30dp"
        android:layout_marginBottom="10dp"
        android:background="#222222"></TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:paddingBottom="10dp">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center">

            <ImageView
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:id="@+id/featSongImage1"
                android:contentDescription="@string/newsongimage"
                android:background="@drawable/error" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/songname"
                android:id="@+id/featSongName1" />

            <ImageButton
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:id="@+id/featSongButt1"
                android:background="@drawable/arrow"
                android:contentDescription="@string/votearrow" />
        </LinearLayout>

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:foregroundGravity="fill_horizontal"
        android:gravity="fill_horizontal|center">




        <android.support.v7.widget.RecyclerView
            android:id="@+id/song_list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_horizontal" />


    </TableRow>
</TableLayout>
</LinearLayout>

And this is my custom adapter

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>
{
private ArrayList<SongCell> data;

public MyAdapter(ArrayList<SongCell> dataI)
{
    data = dataI;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
{
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.song_card,viewGroup,false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder viewholder, int pos) {
    viewholder.songName.setText(data.get(pos).getName());
    viewholder.artist.setText(data.get(pos).getArtist());
    viewholder.image.setImageBitmap(data.get(pos).getArtwork());
}

@Override
public int getItemCount() {
    return 0;
}

@Override
public int getItemViewType(int position) {
    return 0;
}

public class ViewHolder extends RecyclerView.ViewHolder
{
    TextView songName;
    TextView artist;
    ImageView image;
    ImageButton voteButton;

    public ViewHolder(View v)
    {
        super(v);
        this.songName = (TextView) v.findViewById(R.id.song_name);
        this.image = (ImageView) v.findViewById(R.id.song_photo);
        this.voteButton = (ImageButton) v.findViewById(R.id.vote_button);
    }
}

}

I have looked at a ton of guides on how to get this working but it still doesn't work. Anyone have any clue what's going on? I'm on version 25.0.1, and have all the modules imported. But it's not adding a single card into the layout.

I thought it would be difficult to debug such a big code you uploaded. But luckily my eye went on this line

@Override
public int getItemCount() {
    return 0;
}

return 0; in adapter.

Your list size is always 0.

instead write this

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

EDIT-1:
You will also get null pointer exception here

viewholder.artist.setText(data.get(pos).getArtist());

Because you are not initializing the artist variable in ViewHolder class .

Edit-2

@Override
public int getItemCount() {
    return 0;
}

you can remove this particular code from your adapter. Because overriding the methods of class that we don't use might sometimes result in errors that you can't even imagine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM