使用Intents将数据结果返回到父活动

我能够成功地将我的第一个Activity中的ListView中的字符串传输到第二个Activity中的EditText 。 我现在想要编辑文本并将其发回以在我的第一个Activity中更新我的ListView 。 我基本上希望将编辑作为弹出窗口发送回第一个活动,以帮助我测试哪个字符串被传回

我不确定在onActivityResult()放入什么Intent:

 protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) { String name = data.getExtras().getString("name"); Toast.makeText(this, name, Toast.LENGTH_SHORT).show(); } } 

这是我的第一个活动:

  public class ToDoActivity extends Activity { private ArrayList todoItems; private ArrayAdapter todoAdapter; // declare array adapter which will translate the piece of data to teh view private ListView lvItems; // attach to list view private EditText etNewItem; private final int REQUEST_CODE = 20; //private Intent i; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_to_do); etNewItem = (EditText) findViewById(R.id.etNewItem); lvItems = (ListView) findViewById(R.id.lvItems); // now we have access to ListView //populateArrayItems(); // call function readItems(); // read items from file todoAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, todoItems); //create adapter lvItems.setAdapter(todoAdapter); // populate listview using the adapter //todoAdapter.add("item 4"); setupListViewListener(); setupEditItemListener(); onActivityResult(REQUEST_CODE, RESULT_OK, /** Intent variable **/); } private void launchEditItem(String item) { Intent i = new Intent(this, EditItemActivity.class); i.putExtra("itemOnList", item); // list item into edit text //startActivityForResult(i, REQUEST_CODE); startActivity(i); } private void setupEditItemListener() { // on click, run this function to display edit page lvItems.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView adapter, View item, int pos, long id) { String text = (String) lvItems.getItemAtPosition(pos); launchEditItem(text); } }); } private void setupListViewListener() { lvItems.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView adapter, View item, int pos, long id) { todoItems.remove(pos); todoAdapter.notifyDataSetChanged(); // has adapter look back at the array list and refresh it's data and repopulate the view writeItems(); return true; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.to_do, menu); return true; } public void onAddedItem(View v) { String itemText = etNewItem.getText().toString(); todoAdapter.add(itemText); // add to adapter etNewItem.setText(""); //clear edit text writeItems(); //each time to add item, you want to write to file to memorize } private void readItems() { File filesDir = getFilesDir(); //return path where files can be created for android File todoFile = new File(filesDir, "todo.txt"); try { todoItems = new ArrayList(FileUtils.readLines(todoFile)); //populate with read }catch (IOException e) { // if files doesn't exist todoItems = new ArrayList(); } } private void writeItems() { File filesDir = getFilesDir(); //return path where files can be created for android File todoFile = new File(filesDir, "todo.txt"); try { FileUtils.writeLines(todoFile, todoItems); // pass todoItems to todoFile } catch (IOException e) { e.printStackTrace(); } } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) { String name = data.getExtras().getString("name"); Toast.makeText(this, name, Toast.LENGTH_SHORT).show(); } } } 

我想过从第二个活动中使用Intent ,但我不知道该怎么做。

这是我的第二个活动。

 public class EditItemActivity extends Activity { private EditText etEditItem; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_edit_item); Intent i = getIntent(); String ItemToEdit = i.getStringExtra("itemOnList"); etEditItem = (EditText)findViewById(R.id.etEditItem); etEditItem.setText(ItemToEdit); onSubmit(etEditItem); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.edit_item, menu); return true; } public void DoneEdit(View v) { this.finish(); } public void onSubmit(View v) { EditText etName = (EditText) findViewById(R.id.etEditItem); Intent data = new Intent(); data.putExtra("EditedItem", etName.getText().toString()); setResult(RESULT_OK, data); finish(); } } 

要获得结果形成活动(孩子),您需要执行以下操作:

在父活动中

 startActivityForResult(myIntent, 1); 

父活动的全局变量

 boolean backFromChild = false; String aString; 

然后仍然在父活动中

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1) { if (resultCode == RESULT_OK) { // code for result aString = getIntent().getExtras().getString("aString"); backFromChild = true; } if (resultCode == RESULT_CANCELED) { // Write your code on no result return } } } 

在你的孩子中,你可以做某些类似的事情

 Intent returnIntent = new Intent(); //example of sending back a string to the parent. returnIntent.putExtra("aString", aString); setResult(RESULT_OK, returnIntent); finish(); 

问题是,从您的孩子返回时,您的父活动的onResume将被调用。 在那里你必须执行更新,在你的情况下,它是更新编辑文本的信息:

 @Override public void onResume(){ super.onResume(); if (backFromChild){ backFromChild = false; //do something with aString here Toast.makeText(this, aString, Toast.LENGTH_SHORT).show(); } } 

基本上,在onActivityResult我从孩子的意图中获取信息。 然后在onResume我使用这个信息。

为了您的顾虑,您可以使用SharedPreferences

例如:在第二个活动中将数据放入SP中

 SharedPreferences spppp = getSharedPreferences("tab", 0); SharedPreferences.Editor editors = spppp.edit(); editors.putString("for", "0"); editors.commit(); 

并在第一个活动中获取列表视图的数据,如下所示

 SharedPreferences spppp = getSharedPreferences("tab", 0); String your_list_view_value = spppp.getString("for", "");