views:

1543

answers:

1

How to get EditText value from Sub-Activity? With a condition that in Sub-Activity I click back icon on the phone there's no error? This is my Sub-Activity code

public class SBooksSearch extends Activity {
private EditText mTextSearch; 
@Override
protected void onCreate(Bundle savedInstanceState) {
 // TODO Auto-generated method stub
 super.onCreate(savedInstanceState);  
 setContentView(R.layout.sbooks_search); 

 mTextSearch = (EditText)findViewById(R.id.edit_search);  
 Button searchButton = (Button)findViewById(R.id.btn_search);  

 searchButton.setOnClickListener(new View.OnClickListener(){
  public void onClick(View v){    
   Intent data = new Intent();    
   data.putExtra(SBooksDbAdapter.KEY_TITLE_RAW, mTextSearch.getText().toString());   
   setResult(RESULT_OK, data);
   finish();
  }
 });
} 

@Override
protected void onSaveInstanceState(Bundle outState){
 super.onSaveInstanceState(outState);  
}
@Override
protected void onPause(){
 super.onPause();

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

}

This is my Activity-Result

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
 super.onActivityResult(requestCode, resultCode, intent);  
 switch(requestCode){
 case ACTIVITY_SEARCH:
  Bundle extras = getIntent().getExtras();
        mTitleRaw = extras != null ? extras.getString(SBooksDbAdapter.KEY_TITLE_RAW) : null;         
        if(mTitleRaw!=null){
         Cursor cursor = mDbHelper.searchData(mTitleRaw);

   String[] from = new String[]{ SBooksDbAdapter.KEY_ROWID,
     SBooksDbAdapter.KEY_TITLE, SBooksDbAdapter.KEY_LYRICS };
   int[] to = new int[]{ R.id.id, R.id.title, R.id.lyrics };
   SimpleCursorAdapter adapter = 
    new SimpleCursorAdapter(this, R.layout.sbooks_row, cursor, from, to );
   setListAdapter(adapter);
        }  

  break;
 }

}
A: 

First of all, you shouldn't be attempting any sort of action if the user hits the "back" button. It's a global button that means "get me out of here now", and it's usually understood that the user wants nothing other than going back one screen to occur as a result of that.

So what you need to do is in your searchButton.setOnClickListener, in the onClick, create an empty Intent like so:

Intent data = new Intent();

Then you need to be adding the value of your EditText as an extra value, like so:

data.putExtra(SBooksDbAdapter.KEY_TITLE_RAW, mTextSearch.getText().toString());

Finally, include this intent in your setResult call:

setResult(RESULT_OK, data);

In your onActivityResult pull the value out of the intent like you're already doing and you should be fine.

MattC
Thanks MattC , but I try with your code but I don't know why there's no thing on my ListView after Click "Search" button? I think there's no data pull back, do you have any idea about this?
Dennie
If you debug the code and set a breakpoint, is the value returned by extras.getString(SBooksDbAdapter.KEY_TITLE_RAW) the value that was in the EditText box?
MattC
After debugging, the value return is "null". I don't know why?
Dennie
There's one point, When I change the code Bundle extras = getIntent().getExtras(); With Bundle extras = intent.getExtras();The result is OK, but when Click Back "button" on the phone it throw an error. On the contrary, the value is null.
Dennie
When you debug, what does mTextSearch.getText() return? I do this in a lot of places in a few of my apps and it works like a charm so I'm not really sure what's going on with yours :(
MattC
Hi MattC, Your code is ok, and I can get the value back to my Main-Acivity. But what happen when you are in the Sub-Activity and you don't put the value and you click Back "button" on the phone? You see it will throw an exception. So, to make sure no error exception appear, I must check the the value in the Intent before I do something with it. Finally, I can sovle the problem! (Maybe you want to see it here http://stackoverflow.com/questions/1265095/control-the-back-button-in-android )Thanks for your answer.
Dennie