'Cardview Items in Recyclerview temporarily duplicating previous entries when adding new data

sing the dialogfragment to get the user input, while I am capable of updating the mainactivity using the ((MainActivity)getActivity()).storeDataInArrays(); command (This is in order to refresh the recyclerview to display the newly added contact).

However now there seems to be this new issue cropping up where only temporarily, instead of only showing the newly updated contact, it shows adds all of the previous contacts as well.Until I exit and enter the Mainactivity. This is a demonstration of what's happening.

Recycler view duplicating previous entires

This is the Mainactivity

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
//widgets
public ImageButton eAddContact;
RecyclerView recyclerView;

DatabaseHelper myDB;
ArrayList<String> Contact_id, Contact_Name, Contact_Number;
CustomAdapter customAdapter;
Button btnEnterContact;

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

    eAddContact = findViewById(R.id.btnAddContact);
    eAddContact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d(TAG, "onClick:opening dialog");
            Dialog_AddContact dialog = new Dialog_AddContact();
            dialog.show(getSupportFragmentManager(), "Add Contact Dialog");
        }
    });
    //Toast.makeText(getApplicationContext(),"@#",Toast.LENGTH_SHORT).show();
    myDB = new DatabaseHelper(MainActivity.this);
    Contact_id = new ArrayList<>();
    Contact_Name = new ArrayList<>();
    Contact_Number = new ArrayList<>();
    recyclerView = findViewById(R.id.RecyclerView);
    customAdapter = new CustomAdapter(MainActivity.this, Contact_id, Contact_Name, Contact_Number);
    storeDataInArrays();
    recyclerView.setAdapter(customAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
}

void storeDataInArrays() {
    Cursor cursor = myDB.getEveryone();
    if (cursor.getCount() == 0) {
        //Add blank page
    } else {
        while (cursor.moveToNext()) {
            Contact_id.add(cursor.getString(0));
            Contact_Name.add(cursor.getString(1));
            Contact_Number.add(cursor.getString(2));
        }
    }
    customAdapter.notifyDataSetChanged();
}
}

and This is the dialogfragment's enter contact button where I call the Mainactivity fuction

        btnEnterContact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ContactInfo ContactInfo;
            try {
                Log.d(TAG, "onClick: Entering Contact");
                ContactInfo = new ContactInfo(-1,etName.getText().toString(),etPhonenumber.getText().toString());
                Toast.makeText(getContext(),ContactInfo.toString(),Toast.LENGTH_SHORT).show();
            }
            catch (Exception e) {
                Toast.makeText(getContext(),"Error Creating Contact",Toast.LENGTH_SHORT).show();
                ContactInfo = new ContactInfo(-1,"error","0");
            }
            DatabaseHelper databaseHelper = new DatabaseHelper(getContext());
            boolean success = databaseHelper.addOne(ContactInfo);
            Toast.makeText(getContext(),"Success="+success,Toast.LENGTH_SHORT).show();
            ((MainActivity)getActivity()).storeDataInArrays();
            getDialog().dismiss();
        }
    });

I would really appreciate any help in figuring out how to solve this.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source