Helllo Guys.
I'm having issues when I want to get strings from my ContactView into my MainActivity.
I tried to create an Intent in my ContactView and retrieve the data from this intent in my MainAcitivty.
The Problem I have is that it opens the MainActivity when I do anything in my ContactView:
What I did is this:
ContactView.java
//this is where the Activity gets opend right?
Intent getContact = new Intent(this, MainActivity.class);
getContact.putExtra("Contact",phoneNo);
startActivity(getContact);
and this is what i do to retrieve it:
Bundle extras = getIntent().getExtras();
if (extras != null) {
//this is where i should get the contact?
String readContact= extras.getString("Contact")
}
I have a contactPicked method in my ContactView that gets me the contactname and phonenumber and thats actually what i want to pass to my MainActivity because there i have a button that should do an sendsms with text and contact from ContactView.
This is my contactPicked method:
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();
}
Do I need to put the intent into my method? Eventhough it will open me the Activity after I've selected a contact.
Am I missing something that I can't find?
Can anyone help me please?
###EDIT
This is my onClick for the button to pick contacts:
public void onClick(View v) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
}
###EDIT 2
The Orders of my Activites are MainActivity(MapView) that ContactView(where I pick contacts and display them)
I want still to pick Contacts and display them in my ContactView but when I am in my MainActivity again I have a button that should get the strings from ContactView and do something with them like sending a sms.
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();
}
}