mardi 28 avril 2015

Arraylist hashmap android

I would like to have a HashMap of contacts and their numbers saved into an ArrayList but I keep having this error " missing method body or declare abstract " for private static ArrayList> getContacts();

I was able to get the contacts and their numbers out and show with toast but I need to save it and then move it into the SQLite database.

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;
//new code
private static ArrayList<HashMap<String, String>> getContacts();

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

private HashMap<String, String> contacts = new HashMap<String,String>();
//stop new code 
@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));

//all new code added below
                        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);
                            data.add(h);
                        }

//the toast goes with the old code. it works fine. 
                        Toast.makeText(this, "contact info : " + phn_no + "\n" + name, Toast.LENGTH_LONG).show();

                    }
                }
            }
    }

}


}

Aucun commentaire:

Enregistrer un commentaire