RecyclerView中的项目未显示

时间:2018-12-30 12:17:16

标签: android android-recyclerview

List_items没有显示在RecyclerView中。

尝试更改布局高度和其他答案。

list_items应该包含 -项目名称 -item_quantity 日期

因此,基本上,在AlertDialog中输入相应的数据集并单击保存按钮之后,将显示ListActivity。但是出现了一个空白屏幕,其中不包含CardView中的list_items。

这是ListActivity

package com.example.akash.distributedapp.DatabaseApp.Activities;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.example.akash.distributedapp.DatabaseApp.Data.DatabaseHandler;
import com.example.akash.distributedapp.DatabaseApp.Model.Grocery;
import com.example.akash.distributedapp.DatabaseApp.UI.RecyclerViewAdapter;
import com.example.akash.distributedapp.R;

import java.util.ArrayList;
import java.util.List;

public class ListActivity extends AppCompatActivity {
    private DatabaseHandler db;
    private RecyclerView recyclerView;
    private List<Grocery> groceryList;
    private List<Grocery> listItems;
    private RecyclerViewAdapter recyclerViewAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = 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();
            }
        });

        db = new DatabaseHandler(this);
        recyclerView = findViewById(R.id.recyclerViewID);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        groceryList = new ArrayList<>();
        listItems = new ArrayList<>();

        //get items from database
        groceryList = db.getAllGroceries();

        for(Grocery c : groceryList){
            Grocery grocery = new Grocery();
            grocery.setName(c.getName());
            grocery.setQuantity("Qty:" + c.getQuantity());
            grocery.setId(c.getId());
            grocery.setDateItemAdded("Added on: " + c.getDateItemAdded());

            listItems.add(grocery);
        }

        recyclerViewAdapter = new RecyclerViewAdapter(this, listItems);
        recyclerView.setAdapter(recyclerViewAdapter);
        recyclerViewAdapter.notifyDataSetChanged();

    }

}

这是适配器类

package com.example.akash.distributedapp.DatabaseApp.UI;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.akash.distributedapp.DatabaseApp.Model.Grocery;
import com.example.akash.distributedapp.R;

import java.util.List;

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

    private Context context;
    private List<Grocery> groceryItems;

    public RecyclerViewAdapter(Context context, List<Grocery> groceryItems) {
        this.context = context;
        this.groceryItems = groceryItems;
    }

    @NonNull
    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_row, parent, false);

        return new ViewHolder(view, context);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder holder, int position) {
        Grocery grocery = groceryItems.get(position);

        holder.groceryItemName.setText(grocery.getName());
        holder.quantity.setText(grocery.getQuantity());
        holder.quantity.setText(grocery.getDateItemAdded());


    }

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

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView groceryItemName;
        public TextView quantity;
        public TextView dateAdded;
        public Button editBtn;
        public Button deleteBtn;
        public int id;

        public ViewHolder(View itemView, Context ctx) {
            super(itemView);
            context = ctx;
            groceryItemName = itemView.findViewById(R.id.tv_item);
            quantity = itemView.findViewById(R.id.tv_qty);
            dateAdded = itemView.findViewById(R.id.tv_date);
            editBtn = itemView.findViewById(R.id.btn_edit);
            deleteBtn = itemView.findViewById(R.id.btn_delete);

            editBtn.setOnClickListener(this);
            deleteBtn.setOnClickListener(this);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //opens new activity
                }
            });

        }


        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_edit:

                    break;

                case R.id.btn_delete:

                    break;
            }
    }
    }
}

这是我的XML文件:

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

    <android.support.v7.widget.CardView
        android:id="@+id/cardView"
        android:background="@color/colorAccent"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <RelativeLayout
            android:padding="15dp"
            android:background="@color/colorAccent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/tv_item"
                android:text="Grocery name"
                android:textSize="18sp"
                android:textStyle="bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/tv_qty"
                android:layout_below="@+id/tv_item"
                android:text="Quantity"
                android:paddingTop="5dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/tv_date"
                android:paddingTop="5dp"
                android:text="Date"
                android:layout_below="@+id/tv_qty"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TableRow
                android:layout_alignParentRight="true"
                android:layout_marginTop="15dp"
                android:orientation="horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                >
                <Button
                    android:id="@+id/btn_edit"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_marginRight="15dp"
                    android:background="@android:drawable/ic_menu_edit"/>

                <Button
                    android:id="@+id/btn_delete"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:background="@android:drawable/ic_delete"/>


            </TableRow>

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

0 个答案:

没有答案