所以,仍在处理此代码的联系人部分。我觉得是时候展示我的档案了。我试图从联系人列表中选择三个联系人,并在屏幕上显示所述联系人的姓名和号码。我设法让其中一个工作,但是当我选择第二个联系人时,第一个联系人的姓名和号码将更改为第二个联系人的名称和号码,而第二个联系人的信息保持空白。如何让所有这些都出现在他们需要的地方。这是代码:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class contacts extends Activity {
TextView num1, num2, num3, name1, name2, name3;
Button saveNums, pick1Btn, pick2Btn, pick3Btn;
Context c = this;
private final static int REQUEST_CONTACTPICKER = 1;
public static String filename = "sharedString";
SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contactsettings_layout);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
uiBinder();
btnManger();
}
public void uiBinder(){
name1 = (TextView) findViewById(R.id.iceName1);
name2 = (TextView) findViewById(R.id.iceName2);
name3 = (TextView) findViewById(R.id.iceName3);
num1 = (TextView) findViewById(R.id.iceNum1);
num2 = (TextView) findViewById(R.id.iceNum2);
num3 = (TextView) findViewById(R.id.iceNum3);
saveNums = (Button) findViewById(R.id.saveICEBtn);
pick1Btn = (Button) findViewById(R.id.contactPickBtn);
pick2Btn = (Button) findViewById(R.id.contact2PickBtn);
pick3Btn = (Button) findViewById(R.id.contact3PickBtn);
}
private void selectContact()
{
// This intent will fire up the contact picker dialog
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent, REQUEST_CONTACTPICKER);
}
public void btnManger(){
pick1Btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectContact();
}
});
pick2Btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectContact();
}
});
pick3Btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectContact();
}
});
}
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (1) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
cursor.moveToFirst();
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
name1.setText("Contact name: " + name);
num1.setText("Contact number: " + number);
}
break;
case (2):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
cursor.moveToFirst();
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
name2.setText("Contact name: " + name);
num2.setText("Contact number: " + number);
}
break;
case (3):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
cursor.moveToFirst();
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
name3.setText("Contact name: " + name);
num3.setText("Contact number: " + number);
}
break;
}
}
}
再次感谢, - Alx
答案 0 :(得分:1)
这是你的代码,除了我将onclick更改为1 onclick处理程序而不是3调用相同的方法,它使用btn id发送请求代码,我们在你的交换机案例中检查
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class contacts extends Activity {
TextView num1, num2, num3, name1, name2, name3;
Button saveNums, pick1Btn, pick2Btn, pick3Btn;
Context c = this;
public static String filename = "sharedString";
SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contactsettings_layout);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
uiBinder();
btnManger();
}
public void uiBinder(){
name1 = (TextView) findViewById(R.id.iceName1);
name2 = (TextView) findViewById(R.id.iceName2);
name3 = (TextView) findViewById(R.id.iceName3);
num1 = (TextView) findViewById(R.id.iceNum1);
num2 = (TextView) findViewById(R.id.iceNum2);
num3 = (TextView) findViewById(R.id.iceNum3);
saveNums = (Button) findViewById(R.id.saveICEBtn);
pick1Btn = (Button) findViewById(R.id.contactPickBtn);
pick2Btn = (Button) findViewById(R.id.contact2PickBtn);
pick3Btn = (Button) findViewById(R.id.contact3PickBtn);
}
public void btnManger(){
View.OnClickListener click = new View.OnClickListener() {
@Override
public void onClick(View view) {
// This intent will fire up the contact picker dialog
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent, view.getId());
}
};
pick1Btn.setOnClickListener(click);
pick2Btn.setOnClickListener(click);
pick3Btn.setOnClickListener(click);
}
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (R.id.contactPickBtn) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
cursor.moveToFirst();
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
name1.setText("Contact name: " + name);
num1.setText("Contact number: " + number);
}
break;
case (R.id.contactPick2Btn):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
cursor.moveToFirst();
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
name2.setText("Contact name: " + name);
num2.setText("Contact number: " + number);
}
break;
case (R.id.contactPick3Btn):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
cursor.moveToFirst();
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
name3.setText("Contact name: " + name);
num3.setText("Contact number: " + number);
}
break;
}
}
}
P.S。我在浏览器中写了这个,你应该从中理解它可能不比较,但应该足以让你理解