RecyclerViewAdapter仅显示最后一项

时间:2016-06-12 16:37:01

标签: android android-fragments android-recyclerview

我使用带有3个片段的标签式布局。起初我使用ListView,然后移动到RecyclerView以获取其中一个片段,以测试它是否正常工作。当它似乎正在工作时,我将第一个片段的适配器复制到另一个2并重命名它们等。但现在看起来它们只是多次显示最后一个项目。我认为它与适配器有关,所以我将发布适配器代码beneeth。

第1个适配器的代码(CijfersRecyclerViewAdapter.java):

package com.bettersomtoday.arnedebeer.somtoday;

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

import java.util.ArrayList;
import java.util.HashMap;

public class CijfersRecyclerViewAdapter extends RecyclerView.Adapter<CijfersRecyclerViewAdapter.ListItemViewHolder> {

    ArrayList<HashMap<String, String>> list = new ArrayList<>();

    //private static final String TAG_DATA = "data";
    private static final String TAG_VAK = "vak";
    private static final String TAG_BESCHRIJVING = "beschrijving";
    //private static final String TAG_WEGING = "weging";
    private static final String TAG_CIJFER = "resultaat";
    //private static final String TAG_DATUM = "datum";
    //private static final String TAG_VOLDOENDE = "voldoende";
    private static final String TAG_GEEN_CIJFERS = "geencijfers";

    private Context mContext;


    public CijfersRecyclerViewAdapter(Context context, ArrayList<HashMap<String, String>> list) {
        this.list = list;
        this.mContext = context;
    }

    @Override
    public ListItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cijfer_row_item, viewGroup, false);

        ListItemViewHolder holder = new ListItemViewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(ListItemViewHolder viewHolder, int i) {
        HashMap<String, String> e = list.get(i);

        viewHolder.cijfer.setText(e.get(TAG_CIJFER));
        viewHolder.beschrijving.setText(e.get(TAG_BESCHRIJVING));
        viewHolder.vak.setText(e.get(TAG_VAK));
        viewHolder.geenCijfers.setText(e.get(TAG_GEEN_CIJFERS));
    }

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

    class ListItemViewHolder extends RecyclerView.ViewHolder {
        TextView cijfer;
        TextView beschrijving;
        TextView vak;
        TextView geenCijfers;

        public ListItemViewHolder(View itemView) {
            super(itemView);
            cijfer = (TextView) itemView.findViewById(R.id.cijfer);
            beschrijving = (TextView) itemView.findViewById(R.id.cijferBeschrijving);
            vak = (TextView) itemView.findViewById(R.id.vak);
            geenCijfers = (TextView) itemView.findViewById(R.id.geenCijfersText);
        }
    }
}

布局(cijfer_row_item.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip" >

    <TextView
        android:id="@+id/cijfer"
        android:layout_width="55sp"
        android:layout_height="55sp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="6dip"
        android:gravity="center"
        android:singleLine="true"
        android:text="0.0"
        android:textSize="26sp" />

    <TextView
        android:id="@+id/cijferBeschrijving"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@id/cijfer"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="Beschrijving"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/vak"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/cijferBeschrijving"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_toEndOf="@id/cijfer"
        android:gravity="center_vertical"
        android:text="Vak"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/geenCijfersText"
        android:layout_width="match_parent"
        android:layout_height="55sp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="6dip"
        android:gravity="center"
        android:singleLine="true"
        android:text=""
        android:textSize="26sp" />

</RelativeLayout>

片段布局:(fragment_cijfers.xml)

<LinearLayout 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"
    android:background="#ffffff"
    android:orientation="vertical"
    tools:context=".Cijfers">

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

</LinearLayout>

Screenshow of the problem

我希望有人知道这个解决方案

谢谢!

Arne de Beer

1 个答案:

答案 0 :(得分:0)

显然我的适配器很好,问题出在我填充ArrayList的地方。