How to perform CRUD operations agains realm and a listview.
In this tutorial,we shall explore how to work with Realm database.The component of choice is ListView of course.Realm is a fast alternative database to SQLite written in C++.We shall use it to store our data locally. in our device.
First we write or save data to Realm from a material edittext.We the retrieve this data and fill to an array list which we in turn bind to a ListView component.
First we shall need to add the dependency for our Realm database in our build.gradle.You can check the latest version of Realm and add in your project of course.
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 'io.realm:realm-android:0.82.1'
}
Then we have our Spacecraft class here.This is our POJO class.We specify the Spacecraft properties.Each property shall correspond to a Column in the database. Of course you can see we derive from RealmObject.This class therefore shall correspond to our Realm database table.
package com.tutorials.hp.listviewrealm.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;
}
}
We'll need a class to help us in performing our CRUD activities.Lets create the RealHelper class to help us in saving,retrieving data to and from our Realm database.
package com.tutorials.hp.listviewrealm.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;
}
}
Android Save To Realm Database[/caption]
Finally we have our MainActivity.Here we shall first create and display our Input Dialog with editexts and buttons.The inputted data shall get passed to our RealHelper class to be saved to database.Moreover we shall bind data to our GridView via an adapter right here.
package com.tutorials.hp.listviewrealm;
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.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Space;
import android.widget.Toast;
import com.tutorials.hp.listviewrealm.m_Realm.RealmHelper;
import com.tutorials.hp.listviewrealm.m_Realm.Spacecraft;
import java.util.ArrayList;
import io.realm.Realm;
import io.realm.RealmConfiguration;
public class MainActivity extends AppCompatActivity {
Realm realm;
ArrayList<String> spacecrafts;
ArrayAdapter adapter;
ListView lv;
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);
lv= (ListView) findViewById(R.id.lv);
//SETUP REALM
RealmConfiguration config=new RealmConfiguration.Builder(this).build();
realm=Realm.getInstance(config);
//RETRIEVE
RealmHelper helper=new RealmHelper(realm);
spacecrafts=helper.retrieve();
//BIND
adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,spacecrafts);
lv.setAdapter(adapter);
//ITEM CLICKS
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,spacecrafts.get(position),Toast.LENGTH_SHORT).show();
}
});
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
displayInputDialog();
}
});
}
//DISPLAY INPUT 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("");
//RETRIEVE
spacecrafts=helper.retrieve();
adapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,spacecrafts);
lv.setAdapter(adapter);
}
});
d.show();
}
}
We have seen how to save data,retrieve data from Realm database and bind the data to a ListView.Thats our Realm ListView tutorial.You can find the complete source code to check for stuff like layouts above.
Just download it,extract and import to your android studio. Moreover,the youtube version of this tutorial for more explanations and demo is below.Subscribe over there as we do post videos regularly. https://www.youtube.com/watch?v=m4MA-Kd_h0E
No. | Location | Link |
---|---|---|
1. | GitHub | Direct Download) |
2. | GitHub | Browse |
3. | YouTube | Video Tutorial |
4. | YouTube | Our YouTube Channel |