返回的数据结果使用意图父活动 [英] Returning Data Result to Parent Activity using Intents

查看:185
本文介绍了返回的数据结果使用意图父活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够成功地在我的第一个活动在我第二次转让串在我的的ListView 的EditText 活动。我现在要编辑的文本,并发送回在我的第一个活动来更新我的的ListView 。基本上我想有编辑发回的第一个活动作为一个弹出来帮我测试一下这串被传递回

我不知道要放什么东西在意向我的的onActivityResult()

 保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    如果(结果code == RESULT_OK&放大器;&安培;请求code == REQUEST_ code){
        字符串名称= data.getExtras()的getString(名称)。
        Toast.makeText(这一点,姓名,Toast.LENGTH_SHORT).show();
    }
}

下面是我的第一个活动:

 公共类ToDoActivity延伸活动{
        私人的ArrayList<串GT; todoItems;
        私人ArrayAdapter<串GT; todoAdapter; //声明阵列适配器将一块数据转换为德视图
        私人的ListView lvItems; //附加到列表视图
        私人的EditText etNewItem;
        私人最终诠释REQUEST_ code = 20;
        //私人意图我;        @覆盖
        保护无效的onCreate(捆绑savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.activity_to_do);
            etNewItem =(EditText上)findViewById(R.id.etNewItem);
            lvItems =(ListView控件)findViewById(R.id.lvItems); //现在我们已经获得的ListView
            // populateArrayItems(); //通话功能
            readItems(); //读取文件中的项目
            todoAdapter =新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,todoItems); //创建适配器
            lvItems.setAdapter(todoAdapter); //使用适配器填充列表视图
            //todoAdapter.add(\"item 4);
            setupListViewListener();
            setupEditItemListener();
            的onActivityResult(REQUEST_ code,RESULT_OK,/ **意图变量** /);
        }
    私人无效launchEditItem(字符串项){
        意图I =新意图(这一点,EditItemActivity.class);
        i.putExtra(itemOnList项目); //列表项进入编辑文本
        // startActivityForResult(I,REQUEST_ code);
        startActivity(ⅰ);
    }    私人无效setupEditItemListener(){//上点击,运行该功能来显示编辑页面
        lvItems.setOnItemClickListener(新OnItemClickListener(){            公共无效onItemClick(适配器视图<>适配器,查看项目,诠释POS,长I​​D){
                字符串文本=(字符串)lvItems.getItemAtPosition(POS)
                launchEditItem(文本);
            }        });
    }    私人无效setupListViewListener(){
        lvItems.setOnItemLongClickListener(新OnItemLongClickListener(){
            @覆盖
            公共布尔onItemLongClick(适配器视图<>适配器,查看项目,诠释POS,长I​​D){
                todoItems.remove(POS)
                todoAdapter.notifyDataSetChanged(); //有适配器回头看看数组列表并刷新它的数据和重新填充视图
                writeItems();
                返回true;
            }
        });
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.to_do,菜单);
        返回true;
    }    公共无效onAddedItem(视图v){
        。字符串itemText = etNewItem.getText()的toString();
        todoAdapter.add(itemText); //添加到适配器
        etNewItem.setText(); //明确编辑文本
        writeItems(); //每次添加项目,要写入文件记忆
    }    私人无效readItems(){
        文件FILESDIR = getFilesDir();其中,文件可以为Android创建//返回路径
        文件todoFile =新的文件(FILESDIRtodo.txt);
        尝试{
            todoItems =新的ArrayList<串GT;(FileUtils.readLines(todoFile)); //读取与填充
        }赶上(IOException异常五){//如果文件不存在
            todoItems =新的ArrayList<串GT;();
        }
    }    私人无效writeItems(){
        文件FILESDIR = getFilesDir();其中,文件可以为Android创建//返回路径
        文件todoFile =新的文件(FILESDIRtodo.txt);
        尝试{
            FileUtils.writeLines(todoFile,todoItems); //传递给todoItems todoFile
        }赶上(IOException异常五){
            e.printStackTrace();
        }
    }    保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
        如果(结果code == RESULT_OK&放大器;&安培;请求code == REQUEST_ code){
            字符串名称= data.getExtras()的getString(名称)。
            Toast.makeText(这一点,姓名,Toast.LENGTH_SHORT).show();
        }
    }
}

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

下面是我第二次活动。

 公共类EditItemActivity延伸活动{
    私人的EditText etEditItem;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_edit_item);
        意向I = getIntent();
        串ItemToEdit = i.getStringExtra(itemOnList);
        etEditItem =(EditText上)findViewById(R.id.etEditItem);
        etEditItem.setText(ItemToEdit);
        的onsubmit(etEditItem);
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.edit_item,菜单);
        返回true;
    }    公共无效DoneEdit(视图v){
        this.finish();
    }    公共无效的onsubmit(视图v){
        的EditText etName =(EditText上)findViewById(R.id.etEditItem);
        意图数据=新的Intent();
        data.putExtra(EditedItem,etName.getText()的toString());
        的setResult(RESULT_OK,数据);
        完();
    }
}


解决方案

要得到的结果形成一个活动(孩子)你做如下:

在父活动

  startActivityForResult(myIntent,1);

你的父母活动的全局变量

 布尔backFromChild = FALSE;
串ASTRING;

然后还在父活动

  @覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    如果(要求code == 1){
        如果(结果code == RESULT_OK){
            //结果为code
            ASTRING = getIntent()getExtras()的getString(ASTRING)。。
            backFromChild = TRUE;
        }
        如果(结果code == RESULT_CANCELED){
            //写您的code上没有回报的结果
        }
    }
}

在你的孩子你做的地方类似的东西。

 意图returnIntent =新意图();
//例如发送回一个字符串的父。
returnIntent.putExtra(ASTRING,ASTRING);
的setResult(RESULT_OK,returnIntent);
完();

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

  @覆盖
公共无效onResume(){
    super.onResume();
    如果(backFromChild){
         backFromChild = FALSE;
         //做一些与此ASTRING
         Toast.makeText(这一点,ASTRING,Toast.LENGTH_SHORT).show();
    }
}

基本上,在的onActivityResult 我得到的信息从孩子的意图了。然后在 onResume 我用这个信息。

I am able to successfully transfer the string in my ListView in my first Activity to the EditText in my second Activity. I now want to edit the text and send it back to update my ListView 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

I'm not sure what Intent to put in my 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();
    }
}

Here is my first Activity:

    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();
        }
    }
}

I thought about using the Intent from the second activity but I'm not sure how to do so.

Here is my second Activity.

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();
    }
}

解决方案

To get result form an activity (child) you do as follow :

In the parent activity

startActivityForResult(myIntent, 1);

global vars of your parent activity

boolean backFromChild = false;
String aString;

then still in the parent activity

@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
        }
    }
}

in your child you do somewhere something like that

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

The thing is that onResume of your parent activity will be called when returning from your child. In there you have to perform the update, in your case it is to update the information of the edited text :

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

Basically, in the onActivityResult I get the info back from the intent of the child. Then in onResume I use this info.

这篇关于返回的数据结果使用意图父活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆