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

Java 使用意图将数据结果返回到父活动,java,android,android-intent,Java,Android,Android Intent,我能够成功地将第一个活动中的ListView中的字符串传输到第二个活动中的EditText。现在我想编辑文本并将其发送回我的第一个活动中更新我的ListView。我基本上希望将编辑作为弹出窗口发送回第一个活动,以帮助我测试返回的字符串 我不确定我的onActivityResult()的目的是什么: 这是我的第一个活动: public class ToDoActivity extends Activity { private ArrayList<String>

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

我不确定我的
onActivityResult()
的目的是什么:

这是我的第一个活动:

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

出于您的考虑,您可以利用SharedReferences

例如:在第二个活动中将数据放入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", "");
并在第一个活动中获取列表视图的数据,如下所示

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", "");

要从活动(子项)中获取结果,请执行以下操作:

在父活动中

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
        }
    }
}
父活动的全局变量

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
        }
    }
}
然后仍然在父活动中

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
中,我使用了这些信息。

现在我必须承认我没有仔细阅读您的代码,但这不是简单的A-send text->B(您已经管理过了)然后启动一个新的A活动,如对话框B-send text->A吗?否则,如果您希望为有限的文本集执行此操作,那么我建议使用SharedReferences来存储和检索它,在我看来,这使情况更加简单。然后保存一次,并将其放在任何有上下文的地方已注释掉。。有什么原因吗?嗯,我想是的。我仍在学习android开发,所以我不确定从B开始新的
Intent
是否是一条路,但肯定是的。步骤1 A-发送文本->步骤2在EditText中编辑文本。步骤3将编辑的文本从B发送回A@AmulyaKhare是的,我正在测试以前的功能是否有效。我不确定在我的
onActivityForResult()
中放入什么要显示从第一个活动返回的内容,请使用
startActivityForResult
而不是
startActivity
,其余的代码都可以,它应该可以工作。我不确定你所说的onresume是什么意思。你是指onActivityResult吗?我对Java也有点陌生。我想知道为什么onResume会自动调用,因为我没有在任何地方调用该函数。我所做的就是创造这些function@Liondancer当您@override时,您的意思是“替换”一个已经存在的函数。请注意,对于您的术语,函数是什么?其他语言在java中称为
方法。对于onCreate来说,它也已经存在,但是当你想将你的布局链接到活动时,你必须覆盖它来链接布局和所有的东西,这就是为什么覆盖它是一种义务。但是它与
main()
不完全相同。我很高兴它有帮助。当心。再见