RecycleView only showing newly added item after adding another one

0

I have a recycle view with 2 types of items - category and product. Everything works fine until the number of items get bigger than the screen and scrolling is involved. At that point, if I add an item, it is not shown. If I add a second item, the first one is shown and so on. enter image description here

public class ProdListAdapter extends RecyclerView.Adapter<ProdListItemViewHolder> implements Filterable {

    public List<ProductListItem> prodListItems = null;
    public List<ProductListItem> displayedProdListItems = null;
    private int currListItemId;
    private RecyclerView recyclerView;
    private ProductsListFragment fragment;
    private MainActivity activity;

    public ProdListAdapter(ProductsListFragment fragment, MainActivity activity) {
        System.out.println("LIST ADAPTER CONSTRUCTOR");
        this.prodListItems = new ArrayList<>();
        for (int i = 0; i < activity.products.size(); i++) {
            Product currProd = activity.products.get(i);
            System.out.println(currProd.getName() + " " + i);
            //add category separator
            if (i > 0 && !currProd.getCategory().equals(activity.products.get(i - 1).getCategory())) {
                prodListItems.add(new ProductListCategory(currProd));
            } else if (i == 0) {
                prodListItems.add(new ProductListCategory(currProd));
            }
            prodListItems.add(new ProductListItem(currProd));
        }



        this.activity = activity;
        this.displayedProdListItems = prodListItems;
        this.currListItemId = 0;
        this.fragment = fragment;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public ProdListItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //System.out.println("CREATING: " + currListItemId);

        int idToUse = currListItemId % prodListItems.size();



        boolean isCatSeparator = false;
        ProductListItem currProdListItem =  prodListItems.get(idToUse);
        ConstraintLayout listItemView = (ConstraintLayout) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.product_list_item, parent, false);
        if (currProdListItem.isCategorySeparator()) {
            isCatSeparator = true;
        }


        int currProdId = -1;
        if (!isCatSeparator) {
            Product currProd = currProdListItem.getProduct();
            if (currProd != null) {
                currProdId = activity.products.indexOf(currProd);
            }
        }


        System.out.println("idToUse: " + idToUse + " " +  currProdListItem.getItemText());
        System.out.println("PROD ID: " + currProdId);

        ProdListItemViewHolder  viewHolder = new ProdListItemViewHolder(listItemView, currProdId, isCatSeparator, fragment, currProdListItem);
        currListItemId++;
        return viewHolder;
    }

    // (invoked by the layout manager)
    // Replace the contents of a view by the item at index: itemIndex
    @Override
    public void onBindViewHolder(ProdListItemViewHolder  viewHolder, int itemIndex) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element

        //update product item
        ProductListItem newListViewItem = displayedProdListItems.get(itemIndex);

        boolean newItemIsCatSeparator = newListViewItem.isCategorySeparator();
        boolean oldItemIsCatSeparator = viewHolder.getProductListItem().isCategorySeparator();


        if (!newItemIsCatSeparator) {
            Product currProd = newListViewItem.getProduct();
            if (currProd != null) {
                viewHolder.setProdId(activity.products.indexOf(currProd));
            }
        }

        System.out.println();
        System.out.println(viewHolder.getProductListItem().getItemText() + " -> " + newListViewItem.getItemText());
        System.out.println(viewHolder.getProductListItem().isCategorySeparator() + " TO " + newListViewItem.isCategorySeparator());
        System.out.println("checked " + viewHolder.getProductListItem().isChecked() + " to " + newListViewItem.isChecked());
        System.out.println();

        if (newItemIsCatSeparator && oldItemIsCatSeparator) {
            //System.out.println("1");
            viewHolder.changeCategory(newListViewItem.getProduct());
        } else if (!newItemIsCatSeparator && !oldItemIsCatSeparator) {
            //System.out.println("2");
            viewHolder.changeProduct(newListViewItem);
        } else if (newItemIsCatSeparator && !oldItemIsCatSeparator) {
            //System.out.println("3");
            viewHolder.toCatSeparator(newListViewItem.getProduct());
        } else { // !newListViewItem.isCategorySeparator() && viewHolder.getProductListItem().isCategorySeparator()
            //System.out.println("4");
            viewHolder.toProdItem(newListViewItem);
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/prodListLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <AutoCompleteTextView
        android:id="@+id/searchTxtView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Search"
        android:drawableLeft="@drawable/ic_search"
        android:drawablePadding="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/prod_list_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="2dp"
        android:scrollbars="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/searchTxtView" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:layout_marginEnd="28dp"
        android:layout_marginRight="28dp"
        android:layout_marginBottom="28dp"
        android:src="@drawable/ic_fab_plus_36dp"
        app:backgroundTint="#aa00ff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

The way I add the item is from another fragment I add it in the ArrayList activity.products and when I navigate to my product list fragment, the constructor builds the list of RecycleView items. The weirdest thing is the according to my logs, all the items are connected and bounded correctly.

Logs showing the creation of the two items and their binding. Only "new item 1" is shown but only after adding "new item 2". I would be really grateful if you could help me.


11/13 10:08:58: Launching 'app' on Xiaomi MI 8.
$ adb shell am start -n "com.example.producttracker/com.example.producttracker.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Waiting for process to come online...
Connected to process 7960 on device 'xiaomi-mi_8-cd84f0e3'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/Perf: Connecting to perf service.
W/FirebaseApp: Default FirebaseApp failed to initialize because no default options were found. This usually means that com.google.gms:google-services was not applied to your gradle project.
I/FirebaseInitProvider: FirebaseApp initialization unsuccessful
D/Stitch: Initialized android SDK
W/.producttracke: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
W/.producttracke: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
I/System.out: LIST ADAPTER CONSTRUCTOR
    name0 0
    name1 1
    name2 2
    name3 3
    name3 4
    name3 5
    name3 6
    name3 7
    name3 8
    name3 9
    name3 10
    name3 11
    name3 12
    name3 13
    name3 14
    ATTACHED 0
W/Looper: Slow Looper: Activity com.example.producttracker/.MainActivity is 394ms late (wall=319ms running=0ms ClientTransaction{ callbacks=[android.app.servertransaction.LaunchActivityItem] lifecycleRequest=android.app.servertransaction.ResumeActivityItem }) because of 1 msg, msg 1 took 463ms (h=android.app.ActivityThread$H w=110)
I/System.out: idToUse: 0 Uncategorized
    PROD ID: -1
I/System.out: Uncategorized -> Uncategorized
    true TO true
    checked false to false
W/.producttracke: Accessing hidden field Landroid/view/View;->mAccessibilityDelegate:Landroid/view/View$AccessibilityDelegate; (light greylist, reflection)
I/System.out: idToUse: 1 name0
    PROD ID: 0
    name0 -> name0
    false TO false
    checked false to false
I/System.out: idToUse: 2 category1
    PROD ID: -1
I/System.out: category1 -> category1
    true TO true
    checked false to false
I/System.out: idToUse: 3 name1
    PROD ID: 1
    name1 -> name1
I/System.out: false TO false
    checked false to false
I/System.out: idToUse: 4 name2
I/System.out: PROD ID: 2
    name2 -> name2
    false TO false
    checked false to false
I/System.out: idToUse: 5 category2
    PROD ID: -1
    category2 -> category2
I/System.out: true TO true
    checked false to false
I/System.out: idToUse: 6 name3
    PROD ID: 3
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 7 name3
    PROD ID: 4
I/System.out: name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 8 name3
    PROD ID: 5
    name3 -> name3
    false TO false
    checked false to false
I/.producttracke: Compiler allocated 4MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
I/System.out: idToUse: 9 name3
    PROD ID: 6
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 10 name3
    PROD ID: 7
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 11 name3
I/System.out: PROD ID: 8
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 12 name3
    PROD ID: 9
I/System.out: name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 13 name3
    PROD ID: 10
I/System.out: name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 14 name3
    PROD ID: 11
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 15 name3
    PROD ID: 12
    name3 -> name3
    false TO false
    checked false to false
D/libEGL: eglInitialize: enter
I/Adreno: QUALCOMM build                   : 791494e, Id7006ec082
    Build Date                       : 12/09/18
    OpenGL ES Shader Compiler Version: EV031.24.02.00
    Local Branch                     : 
    Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.UM.7.3.R1.09.00.00.423.045
    Remote Branch                    : NONE
    Reconstruct Branch               : NOTHING
    Build Config                     : S P 6.0.7 AArch64
W/RenderThread: type=1400 audit(0.0:66164): avc: denied { search } for name="proc" dev="debugfs" ino=14915 scontext=u:r:untrusted_app:s0:c191,c256,c512,c768 tcontext=u:object_r:qti_debugfs:s0 tclass=dir permissive=0
I/Adreno: PFP: 0x016ee180, ME: 0x00000000
W/RenderThread: type=1400 audit(0.0:66165): avc: denied { search } for name="ctx" dev="debugfs" ino=14944 scontext=u:r:untrusted_app:s0:c191,c256,c512,c768 tcontext=u:object_r:qti_debugfs:s0 tclass=dir permissive=0
I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
    android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
D/libEGL: eglInitialize: exit(res=1)
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 2
W/RenderThread: type=1400 audit(0.0:66166): avc: denied { search } for name="ctx" dev="debugfs" ino=14944 scontext=u:r:untrusted_app:s0:c191,c256,c512,c768 tcontext=u:object_r:qti_debugfs:s0 tclass=dir permissive=0
E/LB: fail to open file: No such file or directory
I/.producttracke: ProcessProfilingInfo new_methods=2355 is saved saved_to_disk=1 resolve_classes_delay=8000
D/OpenGLRenderer: endAllActiveAnimators on 0x75cf371200 (DropDownListView) with handle 0x75cf378a60
I/System.out: SAVING PROD ID: -1
I/chatty: uid=10191(com.example.producttracker) identical 1 line
I/System.out: SAVING PROD ID: -1
I/System.out: LIST ADAPTER CONSTRUCTOR
    name0 0
    name1 1
    name2 2
    name3 3
    name3 4
    name3 5
    name3 6
    name3 7
    name3 8
    name3 9
    name3 10
    name3 11
    name3 12
    name3 13
    name3 14
    new item 1 15
    ATTACHED 0
I/System.out: idToUse: 0 Uncategorized
I/System.out: PROD ID: -1
I/System.out: Uncategorized -> Uncategorized
    true TO true
    checked false to false
I/System.out: idToUse: 1 name0
    PROD ID: 0
    name0 -> name0
    false TO false
    checked false to false
I/System.out: idToUse: 2 category1
    PROD ID: -1
    category1 -> category1
I/System.out: true TO true
    checked false to false
I/System.out: idToUse: 3 name1
    PROD ID: 1
    name1 -> name1
    false TO false
    checked false to false
I/System.out: idToUse: 4 name2
    PROD ID: 2
    name2 -> name2
    false TO false
    checked false to false
I/System.out: idToUse: 5 category2
    PROD ID: -1
    category2 -> category2
    true TO true
    checked false to false
I/System.out: idToUse: 6 name3
    PROD ID: 3
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 7 name3
    PROD ID: 4
I/System.out: name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 8 name3
    PROD ID: 5
I/System.out: name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 9 name3
    PROD ID: 6
I/System.out: name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 10 name3
    PROD ID: 7
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 11 name3
I/System.out: PROD ID: 8
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 12 name3
    PROD ID: 9
    name3 -> name3
I/System.out: false TO false
    checked false to false
I/System.out: idToUse: 13 name3
    PROD ID: 10
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 14 name3
    PROD ID: 11
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 15 name3
    PROD ID: 12
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 16 name3
I/System.out: PROD ID: 13
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 17 name3
    PROD ID: 14
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 18 new item 1
    PROD ID: 15
    new item 1 -> new item 1
    false TO false
    checked false to false
D/OpenGLRenderer: endAllActiveAnimators on 0x75ce491300 (DropDownListView) with handle 0x75ce768ac0
I/System.out: SAVING PROD ID: -1
I/chatty: uid=10191(com.example.producttracker) identical 1 line
I/System.out: SAVING PROD ID: -1
I/System.out: LIST ADAPTER CONSTRUCTOR
    name0 0
    name1 1
    name2 2
    name3 3
    name3 4
    name3 5
I/System.out: name3 6
    name3 7
    name3 8
    name3 9
    name3 10
    name3 11
    name3 12
    name3 13
    name3 14
    new item 1 15
    new item 2 16
    ATTACHED 0
I/System.out: idToUse: 0 Uncategorized
    PROD ID: -1
I/System.out: Uncategorized -> Uncategorized
    true TO true
    checked false to false
I/System.out: idToUse: 1 name0
I/System.out: PROD ID: 0
    name0 -> name0
    false TO false
    checked false to false
I/System.out: idToUse: 2 category1
    PROD ID: -1
    category1 -> category1
    true TO true
    checked false to false
I/System.out: idToUse: 3 name1
    PROD ID: 1
    name1 -> name1
    false TO false
    checked false to false
I/System.out: idToUse: 4 name2
    PROD ID: 2
    name2 -> name2
    false TO false
    checked false to false
I/System.out: idToUse: 5 category2
    PROD ID: -1
I/System.out: category2 -> category2
    true TO true
    checked false to false
I/System.out: idToUse: 6 name3
    PROD ID: 3
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 7 name3
    PROD ID: 4
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 8 name3
    PROD ID: 5
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 9 name3
    PROD ID: 6
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 10 name3
    PROD ID: 7
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 11 name3
    PROD ID: 8
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 12 name3
    PROD ID: 9
    name3 -> name3
    false TO false
I/System.out: checked false to false
I/System.out: idToUse: 13 name3
    PROD ID: 10
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 14 name3
    PROD ID: 11
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 15 name3
    PROD ID: 12
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 16 name3
I/System.out: PROD ID: 13
    name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 17 name3
    PROD ID: 14
I/System.out: name3 -> name3
    false TO false
    checked false to false
I/System.out: idToUse: 18 new item 1
    PROD ID: 15
    new item 1 -> new item 1
    false TO false
    checked false to false
I/System.out: Uncategorized -> new item 2
    true TO false
    checked false to false

EDIT

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;
    public ArrayList<Product> products;
    public ArrayList<Task> tasks;
    public ArrayList<String> categories;
    public ArrayList<ShoppingItem> shoppingItems;
    private NotificationManagerCompat mNotificationManagerCompat;
    public static NotificationManagerCompat notificationManager;
    final public Context context = this;
    private boolean isLastQueryForShopping;
    private ProdListAdapter prodListAdapter;

    public boolean isLastQueryForShopping() {
        return isLastQueryForShopping;
    }

    public void setLastQueryForShopping(boolean lastQueryForShopping) {
        isLastQueryForShopping = lastQueryForShopping;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        createNotificationChannel();

        notificationManager = NotificationManagerCompat.from(this);
        mNotificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());

        //create a dummy product list  //ORDER BY CATEGORY ALPHANUMERICALLY AND THEN ALPHANUMERICALLY WITHIN EACH CATEGORY
        products = new ArrayList<Product>();
        Product prod0 = new Product("name0", "Uncategorized", "notes", 0.00, 1.0, "09/11/19", 0, 0);
        Product prod1 = new Product("name1", "category1", "notes", 0.00, 1.0, "09/11/19", 1, 0);
        Product prod2 = new Product("name2", "category1", "notes", 0.00, 1.0, "09/11/19", 0, 0);
        Product prod3 = new Product("name3", "category2", "notes", 0.00, 1.0, "09/11/19", 0, 0);
        products.add(prod0);
        products.add(prod1);
        products.add(prod2);
        products.add(prod3);
        products.add(new Product("name3", "category2", "notes", 0.00, 1.0, "09/11/19", 0, 0));
        products.add(new Product("name3", "category2", "notes", 0.00, 1.0, "09/11/19", 0, 0));
        products.add(new Product("name3", "category2", "notes", 0.00, 1.0, "09/11/19", 0, 0));
        products.add(new Product("name3", "category2", "notes", 0.00, 1.0, "09/11/19", 0, 0));
        products.add(new Product("name3", "category2", "notes", 0.00, 1.0, "09/11/19", 0, 0));
        products.add(new Product("name3", "category2", "notes", 0.00, 1.0, "09/11/19", 0, 0));
        products.add(new Product("name3", "category2", "notes", 0.00, 1.0, "09/11/19", 0, 0));
        products.add(new Product("name3", "category2", "notes", 0.00, 1.0, "09/11/19", 0, 0));
        products.add(new Product("name3", "category2", "notes", 0.00, 1.0, "09/11/19", 0, 0));
        products.add(new Product("name3", "category2", "notes", 0.00, 1.0, "09/11/19", 0, 0));
        products.add(new Product("name3", "category2", "notes", 0.00, 1.0, "09/11/19", 0, 0));


        categories = new ArrayList<>();
        categories.add("Uncategorized");
        categories.add("category1");
        categories.add("category2");

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(R.id.nav_shopping_list,
                R.id.nav_products_list, R.id.nav_to_do_list, R.id.nav_settings)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
        // generateNotification();
    }


    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration) || super.onSupportNavigateUp();
    }


    public void saveShoppingItem(ShoppingItem shoppingItem, int index){
        if (index == -1) {
            this.shoppingItems.add(shoppingItem);
        } else {
            this.shoppingItems.set(index, shoppingItem);
        }
    }

    public void saveProduct(Product product, int index){
        if (index == -1) {
            // new item
            this.products.add(product);
        } else {
            // updated old item
            this.products.set(index, product);
        }
    }

    public void setProdListAdapter(ProdListAdapter prodListAdapter){
        this.prodListAdapter = prodListAdapter;
    }

    public void saveTask(Task task, int index){
        if (index == -1) {
            this.tasks.add(task);
        } else {
            this.tasks.set(index, task);
        }
    }



}

package com.example.producttracker.ui.product_input;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.Navigation;

import com.example.producttracker.MainActivity;
import com.example.producttracker.Product;
import com.example.producttracker.R;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.zxing.integration.android.IntentIntegrator;

import java.util.ArrayList;
import java.util.Locale;


public class ProductInputFragment extends Fragment {

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_product_input, container, false);

        final MainActivity activity = (MainActivity) getActivity();

        ImageButton plusAmount = (ImageButton) root.findViewById(R.id.plusAmount);
        plusAmount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int amount = Integer.parseInt(amountEditText.getText().toString());
                amount +=1;
                amountEditText.setText(new Integer(amount).toString());
            }
        });

        ImageButton minusAmount = (ImageButton) root.findViewById(R.id.minusAmount);
        minusAmount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int amount = Integer.parseInt(amountEditText.getText().toString());
                if (amount > 0) {
                    amount -=1;
                    amountEditText.setText(new Integer(amount).toString());
                }
            }
        });



        final int prodId = ProductInputFragmentArgs.fromBundle(getArguments()).getProdId();

        Button saveButton = (Button) root.findViewById(R.id.saveButton);
        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String prodName = prodNameTxtView.getText().toString().trim();
                if (!prodName.equals("")) {
                    String date = activity.reformatDate(expiryDateEditText.getText().toString().trim());


                    if (!date.equals("invalidDate")) {
                        double amount = Double.parseDouble(amountEditText.getText().toString().trim());
                        double price = Double.parseDouble(priceEditText.getText().toString().trim());
                        String notes = notesTextView.getText().toString().trim();
                        String category = categorySpinner.getSelectedItem().toString().trim();
                        int measurementIndex = measureInSpinner.getSelectedItemPosition();
                        int categoryIndex = categorySpinner.getSelectedItemPosition();


                        Product newProd = new Product(prodName, category, notes, price, amount, date, measurementIndex, categoryIndex);
                        System.out.println("SAVING PROD ID: " + prodId);
                        System.out.println("SAVING PROD ID: " + prodId);
                        System.out.println("SAVING PROD ID: " + prodId);
                        activity.saveProduct(newProd, prodId);

                        Navigation.findNavController(v).navigate(R.id.nav_products_list);
                    } else {
                        Toast.makeText(v.getContext(), "Invalid date! Please follow format: dd/mm/yy", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(v.getContext(), "Product must have a name!", Toast.LENGTH_SHORT).show();
                }
            }
        });




        return root;
    }

}
java
android
android-recyclerview
recycler-adapter
asked on Stack Overflow Nov 13, 2019 by Parankush • edited Nov 13, 2019 by Parankush

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0