title: Data not displaying on recyclerview after button press
body: <p>My data doesn't add to Recylerview after pressing the add button until I either restart the app or press the back button that's built in the action bar. My activity doesn't close after pressing the button so I usually use the back button on my device. Nothing gets added to my Recyclerview until the app restarts or when I add a new set of data and press the back button from the action bar, then 2 sets of data will be added.</p>
  <p>I have also tried to finish the activity right after the button press but then that also doesn't add data into Recyclerview.</p>
  <p>I want to know how I can make my activity finish after button press and display data in the Recyclerview or make it so that pressing the back button from my device still makes it display.</p>
  <p><strong>MainActivity Code:</strong></p>
  <pre><code>
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.recyclerview.widget.LinearLayoutManager;
  import androidx.recyclerview.widget.RecyclerView;
  
  import android.content.Intent;
  import android.database.Cursor;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.View;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.google.android.material.floatingactionbutton.FloatingActionButton;
  
  import java.util.ArrayList;
  
  public class MainActivity extends AppCompatActivity {
  
      RecyclerView recyclerView;
      FloatingActionButton newTaskBtn;
      myDatabase myDB;
      ArrayList&lt;String&gt; task_id, task_subject, task_description, task_due_date;
      CustomAdapter customAdapter;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          recyclerView = findViewById(R.id.recyclerTasks);
          newTaskBtn = findViewById(R.id.floatingBtn);
          newTaskBtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Intent intent = new Intent(MainActivity.this, AddActivity.class);
                  startActivity(intent);
              }
          });
          myDB = new myDatabase(MainActivity.this);
          task_id = new ArrayList&lt;&gt;();
          task_subject = new ArrayList&lt;&gt;();
          task_description = new ArrayList&lt;&gt;();
          task_due_date = new ArrayList&lt;&gt;();
  
          storeDataArray();
  
          customAdapter = new CustomAdapter(MainActivity.this, task_id, task_subject, task_description, task_due_date);
          recyclerView.setAdapter(customAdapter);
          recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
  
      }
  
      void storeDataArray() {
          Cursor cursor = myDB.readAllData();
          if (cursor.getCount() == 0) {
              Toast.makeText(this, &quot;No data&quot;, Toast.LENGTH_SHORT).show();
  
          } else {
              while (cursor.moveToNext()) {
                  task_id.add(cursor.getString(0));
                  task_subject.add(cursor.getString(1));
                  task_description.add(cursor.getString(2));
                  task_due_date.add(cursor.getString(3));
              }
          }
      }
  }
  </code></pre>
  <p><strong>Add Data Activity Code:</strong></p>
  <pre><code>
  import androidx.appcompat.app.AppCompatActivity;
  
  import android.os.Bundle;
  import android.widget.Button;
  import android.widget.EditText;
  
  public class AddActivity extends AppCompatActivity {
  
      EditText subjectEntry, descEntry, dateEntry;
      Button addBtn;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_add);
  
          subjectEntry = findViewById(R.id.subjectEntry);
          descEntry = findViewById(R.id.descEntry);
          dateEntry =  findViewById(R.id.dateEntry);
          addBtn = findViewById(R.id.addbtn);
          addBtn.setOnClickListener(view -&gt; {
              myDatabase myDB = new myDatabase(AddActivity.this);
              myDB.addTask(subjectEntry.getText().toString().trim(),
                      descEntry.getText().toString().trim(),
                      Integer.valueOf(dateEntry.getText().toString().trim()));
  
  
          });
      }
  }
  </code></pre>
  <p><strong>Adapter Code</strong></p>
  <pre><code>
  import android.content.Context;
  import android.content.Intent;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.LinearLayout;
  import android.widget.TextView;
  
  import androidx.annotation.NonNull;
  import androidx.constraintlayout.widget.ConstraintLayout;
  import androidx.recyclerview.widget.RecyclerView;
  
  import java.util.ArrayList;
  
  public class CustomAdapter extends RecyclerView.Adapter&lt;CustomAdapter.myViewHolder&gt; {
  
      private Context context;
      private ArrayList task_id, task_subject, task_description, task_due_date;
      private int position;
  
      CustomAdapter(Context context, ArrayList task_id, ArrayList task_subject, ArrayList task_description, ArrayList task_due_date){
          this.context = context;
          this.task_id = task_id;
          this.task_description = task_description;
          this.task_subject = task_subject;
          this.task_due_date = task_due_date;
      }
  
      @NonNull
      @Override
      public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
          LayoutInflater inflater = LayoutInflater.from (context);
          View view = inflater.inflate(R.layout.to_do_list, parent, false);
          return new myViewHolder(view);
      }
  
      @Override
      public void onBindViewHolder(@NonNull final myViewHolder holder, final int position) {
          this.position = position;
  
          holder.taskid_txt.setText(String.valueOf(task_id.get(position)));
          holder.taskSubject_txt.setText(String.valueOf(task_subject.get(position)));
          holder.taskDescription_txt.setText(String.valueOf(task_description.get(position)));
          holder.taskDate_txt.setText(String.valueOf(task_due_date.get(position)));
          holder.mainLayout.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Intent intent = new Intent(context, UpdateActivity.class);
                  intent.putExtra(&quot;id&quot;, String.valueOf(task_id.get(position)));
                  intent.putExtra(&quot;Description&quot;, String.valueOf(task_description.get(position)));
                  intent.putExtra(&quot;Subject&quot;, String.valueOf(task_subject.get(position)));
                  intent.putExtra(&quot;Due Date&quot;, String.valueOf(task_due_date.get(position)));
                  context.startActivity(intent);
              }
          });
      }
  
      @Override
      public int getItemCount() {
          return task_id.size();
      }
  
      public class myViewHolder extends RecyclerView.ViewHolder {
  
          TextView taskid_txt, taskSubject_txt, taskDescription_txt, taskDate_txt;
          LinearLayout mainLayout;
  
          public myViewHolder(@NonNull View itemView) {
              super(itemView);
              taskid_txt = itemView.findViewById(R.id.taskid_txt);
              taskSubject_txt = itemView.findViewById(R.id.taskSubject_txt);
              taskDescription_txt = itemView.findViewById(R.id.taskDescription_txt);
              taskDate_txt = itemView.findViewById(R.id.taskDate_txt);
              mainLayout = itemView.findViewById(R.id.mainLayout);
  
          }
  
      }
  
      ;
  }
  </code></pre>