0

I have a database, and I am trying to show the columns on cardview on my app. Nothing wrong database side I guess. I have checked it, for example I can add items to the database. I can login-register. But when I add my items, I should be seeing them on the cardview after addition process. When I add item on the database, it goes back to the recyclerview layout but shows nothing. Only an empty page.

No errors on the debug process until now.

Here is my cardview layout:

<?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();
    }
}
workingonit
  • 23
  • 1
  • 5
  • 1
    Where did you called `#setAdapter()`? – ADM Dec 29 '18 at 18:43
  • I've tried to use this one at recycler java but it didn't work and gave an error. adapter = new itemAdapter(this,mItemsList); mRecyclerView.setAdapter(adapter); – workingonit Dec 29 '18 at 19:03
  • What do you mean by anywhere ? If you do not set Adapter how will `RecyclerView` will show data . – ADM Dec 29 '18 at 19:03
  • Somewhere in your `Activity` (after you have declared your `RecyclerView` and associated objects) you need to fill your `List` "List" with data. Then add that data to your `Adapter` "itemAdapter" (ouch--that hurt just a little). Then use `setAdapter(adapter)` on your `RecyclerView`. There you go... :: I hate to sound harsh, but there is so much wrong with this code... eg. There is no need to pass the `RecyclerView` as a parameter to the `Adapter`! You have totally unnecessary code: `Intent intent =getIntent();`? never used! Mixed naming conventions makes me wonder if this is your code. – Barns Dec 29 '18 at 20:00
  • @Barns my app has finished.And intent does not belong to these codes, I have other activities on my app.(may be activity names are directing wrong, but i will check that again) By the way I am just a beginner and not sorry for asking questions. Of course, you do not sound harsh, I need every help I can get about these, thanks again. – workingonit Dec 30 '18 at 16:29
  • We are ALL still learning! I learn something new every day and I have been coding for almost 30 years (okay, I lied 34 years). Keep coding! – Barns Dec 30 '18 at 16:45

0 Answers0