Android RecyclerView在屏幕上不显示cardview

时间:2018-12-29 18:40:15

标签: android android-recyclerview android-cardview

我有一个数据库,我正在尝试在我的应用程序的cardview上显示列。 我猜数据库方面没有错。我已经检查过了,例如可以将项目添加到数据库中。我可以登录注册。但是,当我添加项目时,添加过程完成后,我应该会在Cardview中看到它们。 当我在数据库上添加项目时,它返回到recyclerview布局,但什么也不显示。只有一个空白页。

到目前为止,调试过程中没有错误。

这是我的Cardview布局:

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

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="5dp">

                <TextView
                    android:id="@+id/itemName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Stock Name"
                    android:textColor="@color/colorPrimary" />

                <TextView

                    android:id="@+id/barcode"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Stock Code" />

                <TextView
                    android:id="@+id/tvQuantity"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Quantity" />

                <TextView
                    android:id="@+id/tvPrice"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Price" />
                <TextView
                    android:id="@+id/tvCost"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Cost" />
            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

recyclerlayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal"
   >

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

</LinearLayout>

RECYCLER.JAVA

public class recycler extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private RecyclerView.LayoutManager mLayoutManager;
    private DatabaseHelper myDb;
    private itemAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);
        myDb = new DatabaseHelper(this);

        Intent intent =getIntent();


        mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView);
        mRecyclerView.setHasFixedSize(true);
        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.home_menu, menu);

        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.addMenu:
                goToAddActivity();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    private void goToAddActivity(){
        Intent intent = new Intent(recycler.this, add.class);
        startActivity(intent);
    }
}

itemAdapter.java

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

    private List<list> mItemsList;
    private Context mContext; //to inflate list layout
    private RecyclerView mRecyclerV;

    public class ViewHolder extends RecyclerView.ViewHolder{

        public TextView nameTxt;
        public TextView quantityTxt;
        public TextView priceTxt;
        public TextView costTxt;
        public TextView barcodeTxt;

        public View layout;
        public ViewHolder( View v) {
            super(v);

            layout = v;

            nameTxt = (TextView) v.findViewById(R.id.itemName);
            quantityTxt = (TextView) v.findViewById(R.id.tvQuantity);
            priceTxt = (TextView) v.findViewById(R.id.tvPrice);
            costTxt = (TextView) v.findViewById(R.id.tvCost);
            barcodeTxt = (TextView) v.findViewById(R.id.barcode);
        }
    }
    public void add(int position, list list) {
        mItemsList.add(position, list);
        notifyItemInserted(position);
    }

    public void remove(int position) {
        mItemsList.remove(position);
        notifyItemRemoved(position);
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public itemAdapter(List<list> myDataset, Context context, RecyclerView recyclerView) {
        mItemsList = myDataset;
        mContext = context;
        mRecyclerV = recyclerView;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public itemAdapter.ViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
        //create a new view
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v =inflater.inflate(R.layout.activity_list, parent, false);
        // set the view's size, margins, paddings and layout parameters
        return new ViewHolder(v);

    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element

        final list list = mItemsList.get(position);
        holder.nameTxt.setText("Stock Name: " + list.getName());
        holder.barcodeTxt.setText("Barcode: " + list.getBarcode());
        holder.quantityTxt.setText("Quantity: " + list.getQuantity());
        holder.priceTxt.setText("Price: " + list.getPrice());
        holder.costTxt.setText("Cost: " + list.getCost());

        holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setTitle("Choose option");
                builder.setMessage("Update or delete stock?");
                builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        //go to update activity
                        goToUpdateActivity(list.getId());

                    }
                });
                builder.setNeutralButton("Delete", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DatabaseHelper dbHelper = new DatabaseHelper(mContext);
                        dbHelper.deleteRecord(list.getId(), mContext);

                        mItemsList.remove(position);
                        mRecyclerV.removeViewAt(position);
                        notifyItemRemoved(position);
                        notifyItemRangeChanged(position, mItemsList.size());
                        notifyDataSetChanged();
                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();
            }
        });
    }
    private void goToUpdateActivity(long listID){
        Intent goToUpdate = new Intent(mContext, update.class);
        goToUpdate.putExtra("listID", listID);
        mContext.startActivity(goToUpdate);
    }
    @Override
    public int getItemCount() {
        return mItemsList.size();
    }
}

0 个答案:

没有答案