2013-12-10 23 views
3

我能够将我的第一个活动中的我的ListView中的字符串成功传送到第二个活动中的EditText。我现在要编辑文本并将其发送回我的第一个活动中更新我的ListView。我基本上想拥有的编辑发回的第一个活动作为一个弹出来帮我测试该字符串被传递回使用Intents将数据结果返回给父活动

我不知道要放什么东西在意向我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(); 
    } 
} 
+1

现在我必须承认我没有仔细阅读你的代码,但这不是简单的A - 发送文本 - > B(你已经管理过了),然后开始一个新的A活动作为对话框B - 发送文本 - > A?否则,如果它是你想要做的一组有限的文本,那么我会建议使用SharedPreferences来存储和检索它,这使得我的观点更简单。然后你将它保存一次,并在任何具有上下文的地方使用。 – cYrixmorten

+0

你需要'startActivityForResult(i,REQUEST_CODE);'这被注释掉了..有什么理由呢? –

+0

嗯,我认为是。我仍然在学习Android开发,所以我不确定是否要从B开始一个新的“Intent”是一条路,但是是的。步骤1 A - 发送文本 - > B.步骤2在EditText中编辑文本。第3步将编辑后的文本从B发回A – Liondancer

回答

3

要获得结果形式的活动(孩子),你做如下:

在父活动的父活动的

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我使用这个信息。

+0

I' m不知道你的意思是由onResume – Liondancer

+0

你的意思是onActivityResult? – Liondancer

+0

我也有点新Java。我想知道为什么onResume被自动调用,因为我没有在任何地方调用函数。我所做的只是快速创建一个函数 – Liondancer

1

你的关心,你可以利用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", ""); 
相关问题