mardi 28 avril 2015

I have an ArrayList HashMap that holds Contacts -names and phone numbers-. I would like to check and make sure that these items are in the ArrayList HashMap before I send it to the SQL database for saving. All of the code below works, I just do not have a clue as of how to print all the contents in the ArrayList. The things I have tried have not worked and I do not know how to use Log...I am rather new to this.

Thanks

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 java.util.ArrayList;
import java.util.HashMap;


public class Contacts extends ActionBarActivity {
private static final int PICK_CONTACT = 1;

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);

}

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);


                        while (c.moveToNext()) {
                            String id1 = c.getString(c.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));

                            String name1 = contacts.get(id1);
                            String phone = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));

                            HashMap<String, String> h = new HashMap<String, String>();
                            h.put("name", name1);
                            h.put("phone", phone);
                            data1.add(h);
                        }



                        Toast.makeText(this, "contact info : " + phn_no + "\n" + name, Toast.LENGTH_LONG).show();

                    }
                }
            }
    }

}


}

the 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="akh.seniorproj.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>

Aucun commentaire:

Enregistrer un commentaire