Java Arraylist hashmap android

Java Arraylist hashmap android,java,android,sqlite,arraylist,hashmap,Java,Android,Sqlite,Arraylist,Hashmap,我想将联系人及其号码的HashMap保存到ArrayList中,但我一直有这样的错误“缺少方法体或声明摘要” 私有静态ArrayList>getContacts() 我可以把联系人和他们的号码拿出来,用toast显示出来,但是我需要保存它,然后将它移动到SQLite数据库中 import android.app.Activity; import android.content.ContentResolver; import android.content.Intent; import andro

我想将联系人及其号码的HashMap保存到ArrayList中,但我一直有这样的错误“缺少方法体或声明摘要” 私有静态ArrayList>getContacts()

我可以把联系人和他们的号码拿出来,用toast显示出来,但是我需要保存它,然后将它移动到SQLite数据库中

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

                    }
                }
            }
    }

}


}
导入android.app.Activity;
导入android.content.ContentResolver;
导入android.content.Intent;
导入android.database.Cursor;
导入android.net.Uri;
导入android.provider.contacts合同;
导入android.support.v7.app.ActionBarActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.TextView;
导入android.widget.Toast;
导入java.util.ArrayList;
导入java.util.HashMap;
公共类联系人扩展了ActionBarActivity{
专用静态最终int PICK_触点=1;
//新代码
私有静态ArrayList getContacts();
private ArrayList data=new ArrayList();
private HashMap contacts=new HashMap();
//停止新代码
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
}
公共无效btnAddContacts_单击(视图){
意向意向=新意向(Intent.ACTION\u PICK,Contacts contract.Contacts.CONTENT\u URI);
startActivityForResult(意向、选择联系人);
}
公共无效b取消单击(查看){
意向i=新意向(Contacts.this、Message.class);
星触觉(i);
}
@凌驾
ActivityResult上的公共无效(int-reqCode、int-resultCode、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
开关(reqCode){
案例(选择联系人):
if(resultCode==Activity.RESULT\u确定){
Uri contactData=data.getData();
游标c=managedQuery(contactData,null,null,null,null);
if(c.moveToFirst()){
字符串id=
c、 getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
串式电话=
c、 getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if(hasPhone.equalsIgnoreCase(“1”)){
Cursor=getContentResolver().query(
ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,null,
contacts contract.commonDataTypes.Phone.CONTACT_ID+“=”+ID,
空,空);
moveToFirst();
String phn_no=phones.getString(phones.getColumnIndex(“data1”));
String name=c.getString(c.getColumnIndex(ContactsContract.CommonDataTypes.StructuredPostal.DISPLAY_name));
//下面添加了所有新代码
联系人。put(姓名、phn_编号);
while(c.moveToNext()){
String id1=c.getString(c.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
字符串名称1=contacts.get(id1);
String phone=c.getString(c.getColumnIndex(ContactsContract.CommonDataTypes.phone.DATA));
HashMap h=新的HashMap();
h、 放置(“名称”,名称1);
h、 放置(“电话”,电话);
数据。添加(h);
}
//祝酒词符合旧的规则,很好用。
Toast.makeText(这是“联系人信息:+phn\u no+”\n“+姓名,Toast.LENGTH\u LONG).show();
}
}
}
}
}
}
更改此代码:

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

现在尝试编译。

当我去掉“()”时,“data.add(h)”;“still gives a error”无法解析方法“add(java.util.HashMap)”,当我使用您建议的第二个方法时,它也会给出相同的结果error@isabella这是因为
data
是传递给
onActivityResult(..)的
Intent
对象
method,将其名称更改为唯一的名称。谢谢你,提图斯!成功了!我将其更改为data1,错误消失了。现在我将尝试打印内容。谢谢你的回答,但是“data.add(h);“仍然给出一个错误”无法解析方法“add(java.util.HashMap)”,我不确定您的代码发生了什么。您在哪里尝试使用getContacts()创建getter?我不知道为什么要抱怨ArrayList中的add方法,因为它是一个已经初始化的静态变量。我知道了。另一位用户建议我将“数据”更改为一些独特且有效的内容。现在,我只是想确保联系人姓名和电话号码已保存在ArrayList/HashMapI中,但它不起作用
private static ArrayList<HashMap<String, String>> getContacts = new ArrayList<HashMap<String, String>>();
//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>();
private static ArrayList<HashMap<String, String>> getContacts = new ArrayList<HashMap<String, String>>();

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

private static HashMap<String, String> contacts = new HashMap<String,String>();
public ArrayList<HashMap<String, String>> getContacts(){
     //put all the code here and return contacts
}