无法通过不同的按钮保存多个联系人

时间:2016-09-28 06:47:48

标签: android android-studio

我想从我的电话簿中提取联系人并保存联系人的姓名和号码。我想保存3个联系人。为此,有3个EditTexts和各自的按钮。在按钮上单击用户将定向到用户可以选择所需联系人的联系人。但是,我无法为所有3个编辑文本实现Contact-fetch功能。 当我只用一个联系人实现它时,代码工作正常。但是,如何在多个联系人的情况下实现这一点。 Contact.java

# Tells Apache to identify which site by name
NameVirtualHost *:80
# Tells Apache to serve the default WAMP Server page to "localhost"
<VirtualHost 127.0.0.1>
ServerName localhost
DocumentRoot "C:/wamp/www/"
</VirtualHost> 
# Tells Apache to serve Client 1's pages to "client1.localhost"
# Duplicate and modify this block to add another client
<VirtualHost 127.0.0.1>
# The name to respond to
ServerName localhost
# Folder where the files live
DocumentRoot "C:/wamp/www/mobile/"
# A few helpful settings...
<Directory "C:/wamp/www/mobile/">
Allow from all
Order Allow,Deny
# Enables .htaccess files for this site
AllowOverride All
</Directory>
# Apache will look for these two files, in this order, if no file is specified in the URL
DirectoryIndex index.html index.php
</VirtualHost> 
<VirtualHost 192.168.56.1>
# The name to respond to
ServerName localhost
# Folder where the files live
DocumentRoot "C:/wamp/www/"
# A few helpful settings...
<Directory "C:/wamp/www/">
Allow from all
Order Allow,Deny
# Enables .htaccess files for this site
AllowOverride All
</Directory>
# Apache will look for these two files, in this order, if no file is specified in the URL
DirectoryIndex index.html index.php
</VirtualHost> 
<VirtualHost 192.168.106.1>
# The name to respond to
ServerName m.localhost
# Folder where the files live
DocumentRoot "C:/wamp/www/mobile/"
# A few helpful settings...
<Directory "C:/wamp/www/mobile/">
Allow from all
Order Allow,Deny
# Enables .htaccess files for this site
AllowOverride All
</Directory>
# Apache will look for these two files, in this order, if no file is specified in the URL
DirectoryIndex index.html index.php
</VirtualHost> 

1 个答案:

答案 0 :(得分:0)

对于所有三次出现的startActivityForResult,您已使用PICK_CONTACT = 1

startActivityForResult(intent, PICK_CONTACT);

为startActivityForResult的每次出现赋予不同的值,如下所示:

con1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.putExtra("extra_text", "1");
        intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
        if (intent.resolveActivity(Contact.this.getPackageManager()) != null) {
            startActivityForResult(intent, 1);
        }
    }
});

con2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.putExtra("extra_text", "2");
        intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
        if (intent.resolveActivity(Contact.this.getPackageManager()) != null) {
            startActivityForResult(intent, 2);
        }
    }
});

con3.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.putExtra("extra_text", "3");
        intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
        if (intent.resolveActivity(Contact.this.getPackageManager()) != null) {
            startActivityForResult(intent, 3);
        }
    }
});

现在在你的onActivityResult中更改以下代码:

if (requestCode == PICK_CONTACT) {
    if (resultCode == this.RESULT_OK) {
        contactPicked(data);
    }
}

到此:

if (requestCode == 1 || requestCode == 2 || requestCode == 3) {
    if (resultCode == this.RESULT_OK) {
        contactPicked(data);
    }
}

现在要在edittext框中打印电话号码,将以下代码添加到contactPicked(意图数据):

String text = data.getStringExtra("extra_text").toString();
if (text.equals("1")) {
           editText2.setText(phone);
}           
else if (text == "2") {
          editText3.setText(phone);
}            
else (text == "3") {
         editText4.setText(phone);
}