Android Realm RecyclerView Tutorials and Examples.
Today we look at how to work with Realm database alongside a RecyclerView component with CardView.The fisrt thing we do is save/write data to Realm database in our android application.We do this from an edittext.We display to the user a material edittexts inside an input dialog.
He enters the data,clicks save and then we save the data. We also retrieve data from the Realm database and fill an arraylist.This arraylist we then pass t our RecyclerView adapter and bind it as our dataset to the RecyclerView.The RecyclerView consists of CardViews.
The first thing is to add our Realm dependency via our build.gradle,app level.You may want to make sure you have a later version than I used here .
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.android.support:cardview-v7:23.3.0'
compile 'io.realm:realm-android:0.82.1'
}
Then we can define our data object,our model class as below :
package com.tutorials.hp.recyclerviewrealm.m_Realm;
import io.realm.RealmObject;
public class Spacecraft extends RealmObject {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Now you can see that we derive from RealmObject.This model class shall correspond to our database table.
Lets see our RealmHelper,the class that shall be responsible for all CRUD operations in our project.Here we save to Realm database,retrieve data and fill an arraylist.
package com.tutorials.hp.recyclerviewrealm.m_Realm;
import java.util.ArrayList;
import io.realm.Realm;
import io.realm.RealmResults;
public class RealmHelper {
Realm realm;
public RealmHelper(Realm realm) {
this.realm = realm;
}
//WRITE
public void save(final Spacecraft spacecraft)
{
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Spacecraft s=realm.copyToRealm(spacecraft);
}
});
}
//READ
public ArrayList<String> retrieve()
{
ArrayList<String> spacecraftNames=new ArrayList<>();
RealmResults<Spacecraft> spacecrafts=realm.where(Spacecraft.class).findAll();
for(Spacecraft s:spacecrafts)
{
spacecraftNames.add(s.getName());
}
return spacecraftNames;
}
}
A
Next we are using a RecyclerView so we need an adapter :
package com.tutorials.hp.recyclerviewrealm.m_UI;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.tutorials.hp.recyclerviewrealm.R;
import java.util.ArrayList;
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
Context c;
ArrayList<String> spacecrafts;
public MyAdapter(Context c, ArrayList<String> spacecrafts) {
this.c = c;
this.spacecrafts = spacecrafts;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(c).inflate(R.layout.model,parent,false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.nameTxt.setText(spacecrafts.get(position));
}
@Override
public int getItemCount() {
return spacecrafts.size();
}
}
The purpose of our Adapter class is to receive a List of data and bind it to our RecyclerView.We have a recyclerView.ViewHolder subclass that you can view in the full source code reference download at the top of this page.
Lets have a look at our MainActivity class.First we are going to show our input dialog that has the edittexts to input data and button to save.
We shall pass the typed data to our RealmHelper class for it to be saved to database. We are also going to bind our data to our RecyclerView via an adapter.
package com.tutorials.hp.recyclerviewrealm;
import android.app.Dialog;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import com.tutorials.hp.recyclerviewrealm.m_Realm.RealmHelper;
import com.tutorials.hp.recyclerviewrealm.m_Realm.Spacecraft;
import com.tutorials.hp.recyclerviewrealm.m_UI.MyAdapter;
import java.util.ArrayList;
import io.realm.Realm;
import io.realm.RealmConfiguration;
public class MainActivity extends AppCompatActivity {
Realm realm;
ArrayList<String> spacecrafts;
MyAdapter adapter;
RecyclerView rv;
EditText nameEditTxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//SETUP RECYCLERVIEW
rv= (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this));
//SETUP REEALM
RealmConfiguration config=new RealmConfiguration.Builder(this).build();
realm=Realm.getInstance(config);
//RETRIEVE
RealmHelper helper=new RealmHelper(realm);
spacecrafts=helper.retrieve();
//BIND
adapter=new MyAdapter(this,spacecrafts);
rv.setAdapter(adapter);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
displayInputDialog();
}
});
}
//SHOW DIALOG
private void displayInputDialog()
{
Dialog d=new Dialog(this);
d.setTitle("Save To Realm");
d.setContentView(R.layout.input_dialog);
nameEditTxt= (EditText) d.findViewById(R.id.nameEditText);
Button saveBtn= (Button) d.findViewById(R.id.saveBtn);
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//GET DATA
Spacecraft s=new Spacecraft();
s.setName(nameEditTxt.getText().toString());
//SAVE
RealmHelper helper=new RealmHelper(realm);
helper.save(s);
nameEditTxt.setText("");
//REFRESH
spacecrafts=helper.retrieve();
adapter=new MyAdapter(MainActivity.this,spacecrafts);
rv.setAdapter(adapter);
}
});
d.show();
}
}
Look we've looked at how to save data to Realm database,read that data and bind to a recyclerView.That's it for today.The full source code can be downloaded above.Download it,extract it and import to your android studio. Cheers.
If you prefer more step by step explanations then you can check the video tutorial below.Or watch the demo here.
No. | Location | Link |
---|---|---|
1. | GitHub | Direct Download) |
2. | GitHub | Browse |
3. | YouTube | Video Tutorial |
4. | YouTube | Our YouTube Channel |