RecycleView没有显示任何值

时间:2019-05-26 08:34:40

标签: android android-recyclerview

我的RecyclerView在警报对话框中未显示任何值。我很困惑,不知道我应该在代码中哪里寻找问题。我希望你们中的一些人遇到同样的问题。

这是我调用RecycleView和AlertDialog的活动。

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pondeljak = (Button) findViewById(R.id.buttonPonedeljak);
        utorak = (Button) findViewById(R.id.buttonUtorak);
        sreda = (Button) findViewById(R.id.buttonSreda);
        cetvrtak = (Button) findViewById(R.id.buttonCetvrtak);
        petak = (Button) findViewById(R.id.buttonPetak);



        db = new DataDays(this);

        View view  = getLayoutInflater().inflate(R.layout.casovi,null);

        alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setView(view);


        alertDialog = alertDialogBuilder.create();

        recyclerView = (RecyclerView) view.findViewById(R.id.recycleCasoviID);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        casoviList = new ArrayList<>();
        casoviItems = new ArrayList<>();

      //  for (Casovi c: casoviList){

            Casovi casovi = new Casovi();

            //casovi.setId(0);
            casovi.setDan("asas");
            casovi.setRedniBrCasa("ssss");
            casovi.setNzaivCasa("ssddff");

            casoviItems.add(casovi);

        recycleViewAdapter = new RecycleViewAdapter(this, casoviItems);
        recyclerView.setAdapter(recycleViewAdapter);
        recycleViewAdapter.notifyDataSetChanged();


        pondeljak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (db.casoviCountPondeljak() == 0){
                    Intent intent = new Intent(MainActivity.this, Unos_casova.class);
                    intent.putExtra("Dan", "Pondeljak");
                    startActivity(intent);

                }else {
                    alertDialog.show();

                }

这是RecycleView类。我不确定,也许这有问题吗?

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder> {

    private Context context;
    private List<Casovi> casoviItems;

    public RecycleViewAdapter(Context context, List<Casovi> casoviItems) {
        this.context = context;
        this.casoviItems = casoviItems;
    }


    @Override
    public RecycleViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.prikaz_casova, parent,false);

        return new ViewHolder(view, context);
    }

    @Override
    public void onBindViewHolder( RecycleViewAdapter.ViewHolder holder, int position) {

        Casovi casovi = new Casovi();


        holder.redniBrCasPrikaz.setText(casovi.getRedniBrCasa());
        holder.nazivCasPrikaz.setText(casovi.getRedniBrCasa());

    }

    @Override
    public int getItemCount() {

        return casoviItems.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView redniBrCasPrikaz;
        public TextView nazivCasPrikaz;
        public Button delteBtn;
        public Button editBtn;
        public CardView cardView;


        public ViewHolder(View view, Context ctx) {
            super(view);

            context = ctx;


            redniBrCasPrikaz = (TextView) view.findViewById(R.id.rednibrojCasaID);
            nazivCasPrikaz = (TextView) view.findViewById(R.id.nazivcasaID);
            delteBtn = (Button) view.findViewById(R.id.obrisiCasBtnID);
            editBtn = (Button) view.findViewById(R.id.izmeniCasBtnID);
            cardView = (CardView) view.findViewById(R.id.carViewID);

        }
    }

布局casovi。应该附加关于女巫prikaz casova的RecycleView。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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="wrap_content">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycleCasoviID"
        android:layout_width="match_parent"
        android:layout_height="574dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.283" />
</android.support.constraint.ConstraintLayout>

布局prikaz_casova。此布局应显示db中的值。在我发布的这个示例中,有哈希值可以显示。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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.support.v7.widget.CardView
        android:id="@+id/carViewID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="133dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="470dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.471">

        <RelativeLayout
            android:id="@+id/relativniID"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/rednibrojCasaID"
                android:layout_width="126dp"
                android:layout_height="wrap_content"
                android:text="redni broj casa"
                android:textSize="14dp"
                android:textStyle="italic"

                />

            <TextView
                android:id="@+id/nazivcasaID"
                android:layout_width="338dp"
                android:layout_height="105dp"
                android:layout_below="@+id/rednibrojCasaID"
                android:layout_marginTop="1dp"
                android:text="Naziv casa"
                android:textSize="24dp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/obrisiCasBtnID"
                android:layout_width="40dp"
                android:layout_height="37dp"
                android:layout_below="@+id/rednibrojCasaID"
                android:layout_toRightOf="@+id/nazivcasaID"
                android:background="@drawable/delete" />

            <Button
                android:id="@+id/izmeniCasBtnID"
                android:layout_width="40dp"
                android:layout_height="37dp"
                android:layout_below="@+id/obrisiCasBtnID"
                android:layout_marginLeft="3dp"
                android:layout_marginTop="15dp"
                android:layout_toRightOf="@+id/nazivcasaID"
                android:background="@drawable/edit" />


        </RelativeLayout>
    </android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

这是您在RecyclerView Adapter类中犯的错误。

 @Override
public void onBindViewHolder( RecycleViewAdapter.ViewHolder holder, int position) {
  

Casovi casovi =新的Casovi();

应该是

Casovi casovi = casoviItems.get(position);

    holder.redniBrCasPrikaz.setText(casovi.getRedniBrCasa());
    holder.nazivCasPrikaz.setText(casovi.getRedniBrCasa());

}

您都做完了