title: ArrayList<HashMap<String, String>> to SQLite database android
body: <p>I have a database that has a table with 4 columns : ID, Contact(name), Phone, Message. </p>
  
  <p>ID is auto incremented. 
  Contact name and number comes from an  ArrayList> 
  Message comes from an EditText.</p>
  
  <p>I am have problems trying to create the part to bring in the  ArrayList> to the Database. </p>
  
  <p>I have the part for the message but later I will need to either put that message in every slot? Or maybe not save it in the database but somewhere else. </p>
  
  <p>Also, should I keep the contact name? or just the number since the number is what will be used to send the SMS message? </p>
  
  <p>Thanks in advance! 
  Below is my code. </p>
  
  <p>Contacts.java - gets the contacts from the contact list and saves them in an ArrayList></p>
  
  <pre><code>import android.app.Activity;
  import android.content.ContentResolver;
  import android.content.Intent;
  import android.database.Cursor;
  import android.net.Uri;
  import android.provider.ContactsContract;
  import android.support.v7.app.ActionBarActivity;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.example.deptofpharmacology.test.R;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  
  
  public class Contacts extends ActionBarActivity {
  private static final int PICK_CONTACT = 1;
  private static final String TAG = "The Goods: ";
  DatabHelper myDb;
  
  private static ArrayList&lt;HashMap&lt;String, String&gt;&gt; getContacts = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
  private static ArrayList&lt;HashMap&lt;String, String&gt;&gt; data1 = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
  
  
  private static HashMap&lt;String, String&gt; contacts = new HashMap&lt;String,String&gt;();
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_contacts);
      myDb = new DatabHelper(this);
     // ArrayList&lt;HashMap&lt;String, String&gt;&gt; getD =  myDb.insertRecord();
  
  }
  
  
  /**
  this is what's going to be used to transfer the Arraylist&lt;HashMap&gt; to the database
  public void AddConPhon(){
      boolean isInserted = myDb.insertData(Contacts.this, );
  }
  **/
  public void btnAddContacts_Click(View view) {
      Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
      startActivityForResult(intent, PICK_CONTACT);
  }
  
  public void btnDone_Click(View view){
      Intent i = new Intent(Contacts.this, Message.class);
      startActivity(i);
  }
  
  
  
  @Override
  public void onActivityResult(int reqCode, int resultCode, Intent data) {
      super.onActivityResult(reqCode, resultCode, data);
  
      switch (reqCode) {
          case (PICK_CONTACT):
              if (resultCode == Activity.RESULT_OK) {
                  Uri contactData = data.getData();
                  Cursor c = managedQuery(contactData, null, null, null, null);
                  if (c.moveToFirst()) {
                      String id =
                              c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
  
                      String hasPhone =
                              c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
  
                      if (hasPhone.equalsIgnoreCase("1")) {
                          Cursor phones = getContentResolver().query(
                                  ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                                  ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id,
                                  null, null);
                          phones.moveToFirst();
                          String phn_no = phones.getString(phones.getColumnIndex("data1"));
                          String name = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.DISPLAY_NAME));
  
                              contacts.put(name, phn_no);
  
                              HashMap&lt;String, String&gt; h = new HashMap&lt;String, String&gt;();
                              h.put("name", name);
                              h.put("phone", phn_no);
                              data1.add(h);
  
                          Toast.makeText(this, "contact info : " + phn_no + "\n" + name, Toast.LENGTH_LONG).show();
                          Log.d(TAG," " + data1.size());
                          Log.d(TAG, data1.toString());
                      }
                  }
              }
      }
  
  }
  </code></pre>
  
  <p>Contacts XML </p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"         android:layout_width="match_parent"
  android:layout_height="match_parent"     android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:paddingBottom="@dimen/activity_vertical_margin"
  tools:context="com.example.deptofpharmacology.test.Contacts"&gt;
  
  
  &lt;Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Contact"
      android:id="@+id/contact1"
      android:layout_alignParentTop="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_marginTop="82dp"
      android:clickable="true"
      android:onClick="btnAddContacts_Click" /&gt;
  
  
  &lt;Button
      style="?android:attr/buttonStyleSmall"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Contact"
      android:id="@+id/contact2"
      android:layout_below="@+id/contact1"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_marginTop="40dp"
      android:clickable="true"
      android:onClick="btnAddContacts_Click" /&gt;
  
  &lt;Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Contact"
      android:id="@+id/contact3"
      android:layout_below="@+id/contact2"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_marginTop="49dp"
      android:clickable="true"
      android:onClick="btnAddContacts_Click" /&gt;
  
  &lt;Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Contact"
      android:id="@+id/contact4"
      android:layout_below="@+id/contact3"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_marginTop="52dp"
      android:clickable="true"
      android:onClick="btnAddContacts_Click" /&gt;
  
  &lt;Button
      style="?android:attr/buttonStyleSmall"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Next"
      android:id="@+id/Next1"
      android:layout_alignParentBottom="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:clickable="true"
      android:onClick="btnDone_Click" /&gt;
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>the Database </p>
  
  <pre><code>import android.content.ContentValues;
  import android.content.Context;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  
  import java.util.HashMap;
  
  public class DatabHelper extends SQLiteOpenHelper {
  
  public static final String DATABASE_NAME = "PostalE.db";
  public static final String TABLE_NAME = "emergency_table";
  public static final String COL_1= "ID";
  public static final String COL_2 = "CONTACTS";
  public static final String COL_3 = "NUMBERS";
  public static final String COL_4 = "MESSAGE";
  
  public DatabHelper(Context context) {
      super(context, DATABASE_NAME, null, 1);
  
  }
  
  @Override
  public void onCreate(SQLiteDatabase db) {
  db.execSQL("create table " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, CONTACTS TEXT ,NUMBERS INTEGER, MESSAGE TEXT)");
  }
  
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
  onCreate(db);
  }
  
  public boolean insertRecord(HashMap&lt;String, String&gt; queryValues) {
      SQLiteDatabase database = this.getWritableDatabase();
      ContentValues values = new ContentValues();
      values.put("name", queryValues.get("name"));
      values.put("phone", queryValues.get("phone"));
      database.insert(COL_2, null, values);
      database.insert(COL_3, null, values);
      long result = database.insert(TABLE_NAME,null,values);
      if(result == -1){
          return false;
      }else{
          return true;
      }
  }
  
  /**
  public boolean insertData(String name, String phone){
      SQLiteDatabase db = this.getWritableDatabase();
      ContentValues contentValues = new ContentValues();
      contentValues.put(COL_2,name);
      contentValues.put(COL_3,phone);
      long result = db.insert(TABLE_NAME,null,contentValues);
      if(result == -1){
          return false;
      }else{
          return true;
      }
  }
  
  **/
  public boolean insertData(String message){
      SQLiteDatabase db = this.getWritableDatabase();
      ContentValues contentValues = new ContentValues();
      contentValues.put(COL_4,message);
      long result = db.insert(TABLE_NAME,null,contentValues);
      if(result == -1){
          return false;
      }else{
          return true;
      }
  }
  }
  </code></pre>