地狱的家伙。
当我想从ContactView中获取字符串到MainActivity时遇到问题。
我试图在ContactView中创建一个Intent,并在MainAcitivty中从该Intent中检索数据。
我的问题是,当我在ContactView中执行任何操作时,它会打开MainActivity:
我所做的是:
ContactView.java
//this is where the Activity gets opend right?
Intent getContact = new Intent(this, MainActivity.class);
getContact.putExtra("Contact",phoneNo);
startActivity(getContact);
这就是我要做的事情:
Bundle extras = getIntent().getExtras();
if (extras != null) {
//this is where i should get the contact?
String readContact= extras.getString("Contact")
}
我的ContactView中有一个contactPicked方法,可以获取我的联系人姓名和电话号码,这实际上是我想要传递给MainActivity的内容,因为我有一个按钮,可以对来自ContactView的文本和联系人进行sendms。
这是我的contactPicked方法:
private void contactPicked(Intent data) {
Cursor cursor = null;
try {
String phoneNo = null;
String name = null;
// getData() method will have the Content Uri of the selected contact
Uri uri = data.getData();
//Query the content uri
cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
// column index of the phone number
int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
// column index of the contact name
int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
phoneNo = cursor.getString(phoneIndex);
name = cursor.getString(nameIndex);
// Set the value to the textviews
textView1.setText(name);
textView2.setText(phoneNo);
} catch (Exception e) {
e.printStackTrace();
}
我需要将意图放入方法中吗?即使选择了某个联系人后,它将为我打开“活动”。
我错过了找不到的东西吗?
谁能帮我吗?
###编辑
这是我点击按钮以选择联系人的方法:
public void onClick(View v) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
}
### EDIT 2
我的活动顺序是ContactView的MainActivity(MapView)(我在其中选择并显示联系人)
我仍然想选择联系人并在ContactView中显示它们,但是当我再次处于MainActivity时,我有一个按钮,该按钮应该从ContactView中获取字符串并对其进行处理,例如发送短信。
ContactView.java
public class ContactView extends AppCompatActivity {
private static final int RESULT_PICK_CONTACT = 85;
private TextView textView1;
private TextView textView2;
private TextView message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_view);
textView1 = (TextView) findViewById(R.id.TxtName);
textView2 = (TextView) findViewById(R.id.TxtNumber);
message = (TextView) findViewById(R.id.message);
//speicher den aktuellen status der activity
SharedPreferences settings = getSharedPreferences("SelectedContact", MODE_PRIVATE);
String name = settings.getString("contactName", "");
//the second parameter set a default data if “contactName” is empty
if (!name.isEmpty()){
textView1.setText(name);
}
String phoneNo = settings.getString("contactPhone", "");//the second parameter set a default data if “contactName” is empty
if (!phoneNo.isEmpty()){
textView2.setText(phoneNo);
}
}
public void onClick(View v) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// check whether the result is ok
if (resultCode == RESULT_OK) {
// Check for the request code, we might be usign multiple startActivityForReslut
switch (requestCode) {
case RESULT_PICK_CONTACT:
contactPicked(data);
break;
}
} else {
Log.e("ContactView", "Failed to pick contact");
}
}
/**
* Query the Uri and read contact details. Handle the picked contact data.
*
* @param data
*/
private void contactPicked(Intent data) {
Cursor cursor = null;
try {
String phoneNo = null;
String name = null;
String msg=message.getText().toString();
//String ResqMessage = null;
// getData() method will have the Content Uri of the selected contact
Uri uri = data.getData();
//Query the content uri
cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
// column index of the phone number
int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
// column index of the contact name
int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
phoneNo = cursor.getString(phoneIndex);
name = cursor.getString(nameIndex);
// Set the value to the textviews
textView1.setText(name);
textView2.setText(phoneNo);
SharedPreferences settings = getSharedPreferences("SelectedContact", MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("contactName", name);
editor.putString("contactPhone", phoneNo);
editor.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
最佳答案
实际上,您收到错误的方式。查看您的代码,您没有通过MainActivity
传递任何捆绑软件,因此在获得通过的联系之前,无需调用getExtras()
。您可以从MainActivity
进行以下操作:
//start main
Intent getContact = new Intent(this, MainActivity.class);
getContact.putExtra("Contact",phoneNo);
startActivity(getContact);
// In MainActivity onCreate
String contact = getIntent().getStringExtra("Contact");