Hi everybocy,

I'm starting to learn how to develop for android.
I writed an activity as follow using some samples from the web, but I
have a problem with mEmailText which is not updated after picking a
contact:

public class NoteEdit extends Activity {

    private EditText mWhoText;
    private EditText mPhoneNumberText;
    private EditText mEmailText;
    private EditText mHowMuchText;
    private EditText mWhatText;
    private EditText mDateText;
    private EditText mCommentText;
    private Long mRowId;

    private PaymentDbAdapter mDbHelper;
    private static final int CONTACT_PICKER_RESULT = 1001;
    private static final String DEBUG_TAG = "NoteEditActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDbHelper = new PaymentDbAdapter(this);
        mDbHelper.open();

        setContentView(R.layout.note_edit);
        setTitle(R.string.edit_note);

        mWhoText = (EditText) findViewById(R.id.who);
        mPhoneNumberText = (EditText) findViewById(R.id.phoneNumber);
        mEmailText = (EditText) findViewById(R.id.email);
        mHowMuchText = (EditText) findViewById(R.id.howMuch);
        mWhatText = (EditText) findViewById(R.id.what);
        mDateText = (EditText) findViewById(R.id.date);
        mCommentText = (EditText) findViewById(R.id.comment);

        Button confirmButton = (Button) findViewById(R.id.confirm);
        Button contactPicker = (Button)
findViewById(R.id.pickContact);
        contactPicker.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                setResult(RESULT_OK);
                doLaunchContactPicker(view);
            }
        });

        mRowId = (savedInstanceState == null) ? null : (Long)
savedInstanceState.getSerializable(PaymentDbAdapter.KEY_ROWID);
        if (mRowId == null) {
            Bundle extras = getIntent().getExtras();
            mRowId = extras != null ?
extras.getLong(PaymentDbAdapter.KEY_ROWID) : null;
        }

        populateFields();

        confirmButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                setResult(RESULT_OK);
                finish();
            }
        });
    }

    public void doLaunchContactPicker(View view) {
        Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
Contacts.CONTENT_URI);
        startActivityForResult(contactPickerIntent,
CONTACT_PICKER_RESULT);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
            case CONTACT_PICKER_RESULT:
                Cursor cursor = null;
                String email = "";
                String phone = "";
                try {
                    Bundle extras = data.getExtras();
                    Set<String> keys = extras.keySet();
                    Iterator<String> iterate = keys.iterator();
                    while (iterate.hasNext()) {
                        String key = iterate.next();
                        Log.v(DEBUG_TAG, key + "[" + extras.get(key) +
"]");
                    }

                    Uri result = data.getData();
                    Log.v(DEBUG_TAG, "Got a contact result: "
                            + result.toString());

                    // get the contact id from the Uri
                    String id = result.getLastPathSegment();

                    // query for everything email
                    cursor =
getContentResolver().query(Email.CONTENT_URI,
                            null, Email.CONTACT_ID + "=?", new
String[] { id },
                            null);

                    int emailIdx = cursor.getColumnIndex(Email.DATA);
                    int phoneIdx = cursor.getColumnIndex(Phone.DATA);
                    if (cursor.moveToFirst()) {
                        email = cursor.getString(emailIdx);
                        phone = cursor.getString(phoneIdx);
                        Log.v(DEBUG_TAG, "Got email: " + email);

                    } else {
                        Log.w(DEBUG_TAG, "No results");
                    }
                } catch (Exception e) {
                    Log.e(DEBUG_TAG, "Failed to get email data", e);
                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }
                    mEmailText.setText(email);
                    if (email.length() == 0) {
                        Toast.makeText(this, "No email found for
contact.",
                                Toast.LENGTH_LONG).show();
                    }

                }

                break;
            }
        } else {
            Log.w(DEBUG_TAG, "Warning: activity result not ok");
        }
    }
    private void populateFields() {
        if (mRowId != null) {
            Cursor note = mDbHelper.fetchNote(mRowId);
            startManagingCursor(note);
 
mWhoText.setText(note.getString(note.getColumnIndexOrThrow(PaymentDbAdapter.KEY_WHO)));
 
mPhoneNumberText.setText(note.getString(note.getColumnIndexOrThrow(PaymentDbAdapter.KEY_PHONENUMBER)));
 
mEmailText.setText(note.getString(note.getColumnIndexOrThrow(PaymentDbAdapter.KEY_EMAIL)));
 
mHowMuchText.setText(note.getString(note.getColumnIndexOrThrow(PaymentDbAdapter.KEY_HOWMUCH)));
 
mWhatText.setText(note.getString(note.getColumnIndexOrThrow(PaymentDbAdapter.KEY_WHAT)));
 
mDateText.setText(note.getString(note.getColumnIndexOrThrow(PaymentDbAdapter.KEY_DATE)));
 
mCommentText.setText(note.getString(note.getColumnIndexOrThrow(PaymentDbAdapter.KEY_COMMENT)));
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        saveState();
        outState.putSerializable(PaymentDbAdapter.KEY_ROWID, mRowId);
    }

    @Override
    protected void onPause() {
        super.onPause();
        saveState();
    }

    @Override
    protected void onResume() {
        super.onResume();
        populateFields();
    }

    private void saveState() {
        String who = mWhoText.getText().toString();
        String phoneNumber = mPhoneNumberText.getText().toString();
        String email = mEmailText.getText().toString();
        String howMuch = mHowMuchText.getText().toString();
        String what = mWhatText.getText().toString();
        String date = mDateText.getText().toString();
        String comment = mCommentText.getText().toString();

        if (mRowId == null) {
            long id = mDbHelper.createNote(who, phoneNumber, email,
howMuch, what, date, comment);
            if (id > 0) {
                mRowId = id;
            }
        } else {
            mDbHelper.updateNote(mRowId, who, phoneNumber, email,
howMuch, what, date, comment);
        }
    }

}


so the line "mEmailText.setText(email);" doesn't seem to work since
the email string is not written inside the text field although the
email string is correctly find in the contact list according to log
file.
I am struggling with this for two days now, so if anybody could help
me, that would be very appreciated.


-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to