1

I have a problem which is driving me nuts. Basically it is a simple RecyclerView which displays a cardview. There are a lot of posts out there already, which I checked. I have assigned a LayoutManager as indicated here (RecyclerView Not Displaying Any CardView Items) , I have implemented the getItemCount method as explained here (card view not showing up), and I changed to the following versions (changing version was suggested here RecyclerView of cards not showing anything):

compile 'com.android.support:support-v4:24.0.0'
compile 'com.android.support:cardview-v7:24.0.0'
compile 'com.android.support:recyclerview-v7:24.0.0'

I also did the notifyDataSetChanged() as indicated here (RecyclerView Not Displaying Any CardView Items). However, nothing fixed it for me.

I have an Activity which has the following code (only relevant sections, getDummyData returns a List with 1 dummy data):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    DatabaseHelper databaseHelper = new DatabaseHelper(getApplicationContext());

    setContentView(R.layout.activity_overview);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
    rv.setHasFixedSize(true);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    rv.setLayoutManager(linearLayoutManager);

    RVAdapter adapter = new RVAdapter(getDummyData());
    rv.setAdapter(adapter);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    adapter.notifyDataSetChanged();

}


public class RVAdapter extends RecyclerView.Adapter<RVAdapter.DocumentViewHolder> {

    List<Document> DocumentList;

    public RVAdapter(List<Document> Documents) {
        this.DocumentList = Documents;
    }

    @Override
    public DocumentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.document_item, parent, false);
        DocumentViewHolder pvh = new DocumentViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(DocumentViewHolder DocumentViewHolder, int i) {
        DocumentViewHolder.Date.setText(DocumentList.get(i).getCreatedFormatted());
        DocumentViewHolder.participants.setText(DocumentList.get(i).getParticipants(getApplicationContext()));
        DocumentViewHolder.counterOfItems.setText(DocumentList.get(i).getcounterOfItems(getApplicationContext()) + StringUtils.EMPTY);
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public int getItemCount() {

        //return DocumentList.size();
    return 1;
    }

    public class DocumentViewHolder extends RecyclerView.ViewHolder {
        TextView Date;
        TextView participants;
        TextView ideasPreview;
        TextView counterOfItems;
        ImageView Photo;

        public DocumentViewHolder(View itemView) {
            super(itemView);
            Date = (TextView) itemView.findViewById(R.id._date);
            participants = (TextView) itemView.findViewById(R.id.participants);
            ideasPreview = (TextView) itemView.findViewById(R.id.ideas_preview);
            counterOfItems = (TextView) itemView.findViewById(R.id.counter_of_items);
            Photo = (ImageView) itemView.findViewById(R.id._photo);
        }
    }


}

And I have a Document_item - 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:orientation="vertical">

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    card_view:cardCornerRadius="@dimen/_card_corner">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/_photo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.25" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingEnd="@dimen/activity_horizontal_margin"
            android:paddingStart="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin">

            <TextView
                android:id="@+id/_date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="21st June 2016" />

            <TextView
                android:id="@+id/participants"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Thomas, Christian and Johann" />

            <TextView
                android:id="@+id/counter_of_items"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Thomas, Christian and Johann" />

            <TextView
                android:id="@+id/ideas_preview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Hello, Test, Test, Test" />

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

The activity's xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout     
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.chmaurer.idea.OverviewActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/ic_playlist_add_white_24dp" />

</android.support.design.widget.CoordinatorLayout>

The code is compiling and executing normally, but no cards are shown. I have tried a few hour for myself now, but it is definitely the point where I'd be glad to have a hint...

Community
  • 1
  • 1
Christian
  • 303
  • 2
  • 15

2 Answers2

2

Your code is fine, The problem is with layout file: document_item.xml:

    <LinearLayout
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/_photo"
            android:layout_width="wrap_content"  -->mistake
            android:layout_height="wrap_content"
            android:layout_weight="0.25" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" -->mistake
            android:gravity="center_horizontal"
            android:orientation="vertical"
            ...

The second LinearLayout gets its height from parent (first LinearLayout), and the first one gets its height from its only child: ImageView (so without no drawable set, the second layout's height will be zero! ), if you set a drawable for ImageView (in onBindViewHolder method or in layout xml file) it probably crops some of TextView items, Also when you set android:layout_width to "wrap_content", actually you're ignoring layout_weight.

So edit layout file like this:

          ...
          <ImageView
            android:id="@+id/_photo"
            android:layout_width="0dp"
            android:layout_gravity="center_vertical"
            android:layout_height="wrap_content"
            android:layout_weight="0.25" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ...
Farshad
  • 3,074
  • 2
  • 30
  • 44
0

You are using RecyclerView with vertical orientation (in Java code)

In the Activity XML you are setting the layout height to Wrap_Content

The card layout also has an height of match_parent

They will not work like that, I lost three days on this

Try to give fix height to your card layout, test also other alteration of other layout, it will work for you

Good luck

Firas Shrourou
  • 625
  • 8
  • 19