java-使用意图将数据结果返回到父活动

java-使用意图将数据结果返回到父活动,第1张

概述我能够将第一个Activity中的ListView中的字符串成功传输到第二个Activity中的EditText中.现在,我想编辑文本并将其发送回以更新我的第一个Activity中的ListView.我基本上希望将编辑作为d出窗口发送回第一个活动,以帮助我测试将哪个字符串传回我不确定要在onActivityResult()中放

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

我不确定要在onActivityResult()中放入什么意图

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<String> todoItems;                private ArrayAdapter<String> 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<String>(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<String>(fileUtils.readlines(todofile));   //populate with read        }catch (IOException e) {    // if files doesn't exist              todoItems = new ArrayList<String>();        }    }    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;

然后仍然在父活动中

@OverrIDeprotected 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.在这种情况下,您必须执行更新,以更新已编辑文本的信息:

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

基本上,在onActivityResult中,我从孩子的意图中获取了信息.然后在onResume中,我使用此信息.

总结

以上是内存溢出为你收集整理的java-使用意图将数据结果返回到父活动全部内容,希望文章能够帮你解决java-使用意图将数据结果返回到父活动所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1087364.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存