0

Edited >> Write whole activity code I have an EditText in my activity and a button below this. When user clicks on the button, the contact picker intent is lunched, and user can pick one contact. In 'onActivityResult' event of that activity, I get the selected id of contact, and by the API I have defined, I get his/her name, as follow:

package com.iBirthDayNotofication;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.iBirthdayNotification.Data.BirthdayContact;
import com.iBirthdayNotification.Data.MyOwnContact;

public class BirthdayEditor extends Activity {

     EditText et_Name;
     EditText et_Birthday;
    Button et_Btn;


    private Long mRowId;

    BirthdayContact db;

    String editor_tag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.birthday_editor);

        db=new BirthdayContact(this);

        et_Name=(EditText)findViewById(R.id.editor_contactname_Input);
        et_Birthday=(EditText)findViewById(R.id.editor_birthdday_input);
        et_Btn=(Button)findViewById(R.id.editor_btn_add);

        et_Name.setText("Ahmagh");


        mRowId = (savedInstanceState==null) ? null : 
            (Long)savedInstanceState.getSerializable("_id");

        //Get row id from calling activity
        if (mRowId == null) {

            Bundle extras = getIntent().getExtras();
            mRowId = extras != null ? extras.getLong("_id")
                    : null;

        }

        populateFields();

        et_Btn.setOnClickListener(new View.OnClickListener() 
        {

            @Override
            public void onClick(View arg0) { 
                // TODO Auto-generated method stub

                //Log.v("click message","hey");

                //Open Contact Intent to pick contact ID
                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                startActivityForResult(intent,1001);
            }
        });

    }

    void startContactActivity(Intent intent)
    {
        startActivityForResult(intent,101);
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        //super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK) 
        {
            String id = data.getData().getLastPathSegment();


            mRowId=Long.parseLong(id);

            String tst=MyOwnContact.GetMyOwnContact(mRowId).GetName();

            et_Name.setText(tst);


            BirthdayContact bc=db.GetBirthDay(mRowId);


            editor_tag="insert";

            if(bc!=null)
            {

                SimpleDateFormat sp=new SimpleDateFormat("yyyy-MM-dd");


                et_Birthday.setText(bc.GetBirthDay().toString());
                editor_tag="edit";
            }

        }

    }

    void populateFields()
    {
        if(mRowId!=null)
        {
            MyOwnContact mw=MyOwnContact.GetMyOwnContact(mRowId);

            BirthdayContact bc=db.GetBirthDay(mRowId);

            //populate widgtes
            et_Name.setText(mw.GetName());
            et_Name.setText(bc.GetBirthDay().toString());


        }
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        saveState();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        populateFields();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);

        saveState();

        outState.putSerializable(("_id"), mRowId);
    }

    private void saveState()
    {
        SimpleDateFormat sp=new SimpleDateFormat("yyyy-MM-dd");

        Date birthDate=new Date();
        try
        {
            if(et_Birthday.getText()!=null)
            {
                birthDate = sp.parse(et_Birthday.getText().toString());
            }
        }
        catch(Exception exp)
        {
            //
        }


        if (editor_tag == "insert") {
            long id = db.AddNewBirthdayContact(mRowId, birthDate);
            if (id > 0) {
                mRowId = id;
            }
        } 
        else if(editor_tag =="edit")
        {
            db.UpdateBirthDateContact(mRowId, birthDate);
        }
    }


}

Wheter I set et_Name text ("!!!!!!") or wheter I set it with tst, it only shows this:

Wed Aug 29 00:00:00 Asia/Tehran 1990

Even when I comment the

et_Name.setText("!!!!!!");

Is shows that date.

Note: I have a table which save a contact`s birthday and his id, and I have defined two classes to deal with ContactAPI and that table.

Can you help me please ?!

Edited:

Xml file

 <EditText
            android:id="@+id/editor_contactname_Input"
            android:layout_weight="1"
            android:hint="@string/editor_hint_contact"
            android:layout_width="0dp"
            android:layout_height="wrap_content"

            >

Edited >>

The outcome :

enter image description here

Babak Fakhriloo
  • 2,076
  • 4
  • 44
  • 80

3 Answers3

1

You don't close de EditText tag.

try:

<EditText
        android:id="@+id/editor_contactname_Input"
        android:layout_weight="1"
        android:hint="@string/editor_hint_contact"
        android:layout_width="0dp"
        android:layout_height="wrap_content"

        />
pb2q
  • 58,613
  • 19
  • 146
  • 147
Carlos
  • 11
  • 1
0

try it without

super.onActivityResult(requestCode, resultCode, data);

I have not used contact intent but have worked a lot with camera intent and I have not had to call super.

Orlymee
  • 2,349
  • 1
  • 22
  • 24
  • i am not sure as to why you are getting that. are u sure you are not setting it else where.read this post it shows how to read contacts http://stackoverflow.com/questions/1721279/how-to-read-contacts-on-android-2-0 – Orlymee Feb 28 '12 at 14:52
  • The easiest way will be to DeBug and see where and how this value is being set. Here is a link on how to do that http://androidacademy.com/1-tutorials/43-hands-on/76-debugging-android-with-eclipse?showall=1 – Orlymee Feb 28 '12 at 14:55
  • check that you are not assigning it a value in your layout. Also what is the value of your string that your are using for hint? – Orlymee Feb 28 '12 at 15:02
  • I set the value in two places, and I put break point in both of them, but according to app logic, only the on I noted in my above code is being run. Also I gave the hint value by reading from string resource : Click to pick contact – Babak Fakhriloo Feb 28 '12 at 17:20
  • Another thing to check will be to make sure that your calls to update the textfield are being called on the UI Thread. – Orlymee Feb 28 '12 at 17:43
  • how can I check whether the call has been from UI thread or not? – Babak Fakhriloo Feb 28 '12 at 18:15
  • Since you are saying that you are never able to update the textview, in the main activity oncreate, retrieve the textview and try to set it value to something else. Since you always get the same value back it will be a good check to see if it gets overwritten again. Sorry forgot to mention here is a link on info on UI Thread. http://stackoverflow.com/questions/1845678/android-ui-thread – Orlymee Feb 28 '12 at 18:26
  • if you still cannot figure out, it is better to post the code (if you can) will save time for everyone. – Orlymee Feb 28 '12 at 18:28
0

I could found the problem.In the onresume event, I call populateFields() method with out considering that the operation mode. The opertion mode could be insert or edit. And also I made a mistake and set the et_name twice.

Thanks guys.

Babak Fakhriloo
  • 2,076
  • 4
  • 44
  • 80