I want to create a app like to do list. So created a DatabaseHelper
Class. It contains DataAdd
and DisplayData
function . There is recyclerView object in MainActivity for view all stored data. My functions are working but when I saved new data and I pressed back button , its saving but doesn't display on recyclerView . When I saved new data again its displaying. I think I can refresh recylerview when I pressed back in onBackPressed
. How can I do it ?
...
package com.tcoding.hangidersdenkacsorucozdum;
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.View;
import android.widget.Toast;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView rv ;
private FloatingActionButton addButton ;
DatabaseHelper db ;
ArrayList<String> lessonId , lessonName , lessonSubject , solvedProblemCount ;
CustomAdapter adapter ;
private void init() {
rv = (RecyclerView) findViewById(R.id.rv);
addButton = (FloatingActionButton) findViewById(R.id.addButton);
db = new DatabaseHelper(this);
lessonId = new ArrayList<>();
lessonName = new ArrayList<>();
lessonSubject = new ArrayList<>();
solvedProblemCount = new ArrayList<>();
rv = findViewById(R.id.rv);
adapter = new CustomAdapter(this , lessonId , lessonName , lessonSubject , solvedProblemCount);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setEnabled(true);
rv.setAdapter(adapter);
}
@Override
public void onBackPressed() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,AddActivity.class));
}
});
DisplayData();
}
void DisplayData(){
Cursor c = db.showData();
if(c.getCount() == 0){
Toast.makeText(this, "Gösterilecek Veri Bulunamadı...", Toast.LENGTH_SHORT).show();
} else {
while(c.moveToNext()) {
lessonId.add(c.getString(0));
lessonName.add(c.getString(1));
lessonSubject.add(c.getString(2));
solvedProblemCount.add(c.getString(3));
}
}
}
}
You need to grab data again inside onResume()
, as onCreate()
won't be called when you return back from AddActivity
So, to add the entire data you do something like
@Override
protected void onResume() {
super.onResume();
DisplayData();
adapter.notifyDataSetChanged();
}
Or, from performance wise, it is better to notify of the last inserted item to the database.
@Override
protected void onResume() {
super.onResume();
Cursor c = db.showData();
if(c.getCount() == 0){
Toast.makeText(this, "Gösterilecek Veri Bulunamadı...", Toast.LENGTH_SHORT).show();
} else {
c.cursor.moveToLast())
lessonId.add(c.getString(0));
lessonName.add(c.getString(1));
lessonSubject.add(c.getString(2));
solvedProblemCount.add(c.getString(3));
}
adapter.notifyItemInserted(lessonId.getSize()-1);
}