0

I am having some trouble getting the data from my edit text into an share intent extra.For some reason , it is returning an empty string even when the EditText field has been filled. I Know there are several similar threads about people having trouble getting data out of an edit text field but I cant seem to adapt those answers to my code.

I have attached the relevant bits of code .Do let me know if you need to see any more of the code.(the bits of code I am having trouble with is in the private Intent createShareIntent().It is the third method in the attached code below)

   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mDbHelper = new NotesDbAdapter(this);
    mDbHelper.open();

    setContentView(R.layout.note_edit);//Set the layout file 
    setTitle(R.string.edit_note);//Set the title to be displayed in the action bar


    mTitleText = (EditText) findViewById(R.id.title);
    mBodyText = (EditText) findViewById(R.id.body);

   //get the row id of the note in the database form the intent .
    mRowId = (savedInstanceState == null) ? null :
        (Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID);
    if (mRowId == null) {
        Bundle extras = getIntent().getExtras();
        mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
                                : null;
    }

    populateFields();

}
//THis method is used to create the menu in the Action bar
@Override//This works perfectly fine
public boolean onCreateOptionsMenu(Menu menu) {

    getSupportMenuInflater().inflate(R.menu.main, menu);//inflating the menu layout
    MenuItem actionItem = menu.findItem(R.id.menu_share);//The share button in handled here
    //The share action provider  
    ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
    actionProvider.setShareHistoryFileName(null);
    actionProvider.setShareIntent(createShareIntent());//creating a share intent
    return true;//return true if the menu creation has been successful 
}

//Handles the share intent
private Intent createShareIntent() {//this is where the trouble is
    saveState();
    String body = mBodyText.getText().toString();//this returnes an empty string 
    String title = mTitleText.getText().toString();// this returnes an empty string
    Intent shareIntent = new Intent(Intent.ACTION_SEND);//Setting the action to ACTION_SEND
    shareIntent.setType("text/plain");//setting the text type to plain text
    shareIntent.putExtra(Intent.EXTRA_TEXT,body);//this sends an empty string
    shareIntent.putExtra(Intent.EXTRA_SUBJECT,title);//this sends an empty string
    return shareIntent;
}

My questions are: 1. Why am I getting an empty string from the edittext even when it is filled? 2.How do I fix it ?

Thanks for taking the time to read this and for any help that you can give.

4

1 回答 1

0

它返回空文本,因为putExtra在内部onCreateOptionsMenu和开始时(创建选项菜单时)调用你EditText是空的。您可以添加TextChangedListener到您的编辑文本并在更改时更新您的共享意图EditText

et.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // Update your intent here.
    }
});
于 2014-10-18T18:31:41.270 回答