Note that there are some explanatory texts on larger screens.

plurals
  1. POReturning Data Result to Parent Activity using Intents
    primarykey
    data
    text
    <p>I am able to successfully transfer the string in my <code>ListView</code> in my first Activity to the <code>EditText</code> in my second Activity. I now want to edit the text and send it back to update my <code>ListView</code> in my first Activity. I basically want to have the edits be sent back to the first activity as a popup to help me test which string is being passed back</p> <p>I'm not sure what Intent to put in my <code>onActivityResult()</code>:</p> <pre><code>protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK &amp;&amp; requestCode == REQUEST_CODE) { String name = data.getExtras().getString("name"); Toast.makeText(this, name, Toast.LENGTH_SHORT).show(); } } </code></pre> <p>Here is my first Activity:</p> <pre><code> public class ToDoActivity extends Activity { private ArrayList&lt;String&gt; todoItems; private ArrayAdapter&lt;String&gt; 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&lt;String&gt;(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&lt;?&gt; 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&lt;?&gt; 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&lt;String&gt;(FileUtils.readLines(todoFile)); //populate with read }catch (IOException e) { // if files doesn't exist todoItems = new ArrayList&lt;String&gt;(); } } 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 &amp;&amp; requestCode == REQUEST_CODE) { String name = data.getExtras().getString("name"); Toast.makeText(this, name, Toast.LENGTH_SHORT).show(); } } } </code></pre> <p>I thought about using the <code>Intent</code> from the second activity but I'm not sure how to do so.</p> <p>Here is my second Activity.</p> <pre><code>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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload