step 1:
Class name;Contact
public class Contact {
//private variables int _id; String _name; String _phone_number; // Empty constructor public Contact(){ } // constructor public Contact(int id, String name, String _phone_number){ this._id = id; this._name = name; this._phone_number = _phone_number; } // constructor public Contact(String name, String _phone_number){ this._name = name; this._phone_number = _phone_number; } // getting ID public int getID(){ return this._id; } // setting id public void setID(int id){ this._id = id; } // getting name public String getName(){ return this._name; } // setting name public void setName(String name){ this._name = name; } // getting phone number public String getPhoneNumber(){ return this._phone_number; } // setting phone number public void setPhoneNumber(String phone_number){ this._phone_number = phone_number; }}step 2:Class name: DB Helperpublic class Contact { //private variables int _id; String _name; String _phone_number; // Empty constructor public Contact(){ } // constructor public Contact(int id, String name, String _phone_number){ this._id = id; this._name = name; this._phone_number = _phone_number; } // constructor public Contact(String name, String _phone_number){ this._name = name; this._phone_number = _phone_number; } // getting ID public int getID(){ return this._id; } // setting id public void setID(int id){ this._id = id; } // getting name public String getName(){ return this._name; } // setting name public void setName(String name){ this._name = name; } // getting phone number public String getPhoneNumber(){ return this._phone_number; } // setting phone number public void setPhoneNumber(String phone_number){ this._phone_number = phone_number; } }step 3:Class name:MainActivityimport android.content.Intent; import android.support.design.widget.FloatingActionButton; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.GridView; import android.widget.Toast; import java.util.List; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_main); DBHelper db=new DBHelper(getApplicationContext()); /** * CRUD Operations * */// Inserting ContactsLog.d("Insert: ", "Inserting .."); db.addContact(new Contact("Ravi", "9100000000")); db.addContact(new Contact("Srinivas", "9199999999")); db.addContact(new Contact("Tommy", "9522222222")); db.addContact(new Contact("Karthik", "9533333333")); // Reading all contacts Log.d("Reading: ", "Reading all contacts.."); List<Contact> contacts = db.getAllContacts(); for (Contact cn : contacts) { String log = "Id: "+cn.getID()+" ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber(); //Writing Contacts to logLog.d("Name: ", log); } } }
No comments:
Post a Comment