mercredi 29 avril 2015

I have a database that has a table with 4 columns : ID, Contact(name), Phone, Message.

ID is auto incremented. Contact name and number comes from an ArrayList> Message comes from an EditText.

I am have problems trying to create the part to bring in the ArrayList> to the Database.

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.

Also, should I keep the contact name? or just the number since the number is what will be used to send the SMS message?

Thanks in advance! Below is my code.

Contacts.java - gets the contacts from the contact list and saves them in an ArrayList>

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<HashMap<String, String>> getContacts = new ArrayList<HashMap<String, String>>();

private static ArrayList<HashMap<String, String>> data1 = new ArrayList<HashMap<String, String>>();



private static HashMap<String, String> contacts = new HashMap<String,String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contacts);
    myDb = new DatabHelper(this);
   // ArrayList<HashMap<String, String>> getD =  myDb.insertRecord();

}


/**
this is what's going to be used to transfer the Arraylist<HashMap> 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<String, String> h = new HashMap<String, String>();
                            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());
                    }
                }
            }
    }

}

Contacts XML

<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"         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">


<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" />


<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" />

<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" />

<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" />

<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" />


</RelativeLayout>

the Database

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<String, String> 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;
    }
}
}

Aucun commentaire:

Enregistrer un commentaire