2016-05-29 86 views
0

抱歉模糊的標題。但我的問題是這樣的:在返回到父活動時保存對對象的更改

我使用意圖將位置從我的父活動的列表視圖傳遞到DetailActivity我創建對象。我用這些信息填充一些edittext。當我返回到父活動時,我想要將所做的任何更改保存到對象中。我該如何處理這個問題?

MainActivity

public class MainActivity extends AppCompatActivity { 
    public static final String POSITION = "POSITION"; 

    private static final int REQUEST_CODE = 1111; 

    private ListView lv_contacts; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ContactListAdapter adapter = new ContactListAdapter(this, R.layout.list_item, ContactList.getInstance().getContacts()); 
     lv_contacts = (ListView) findViewById(R.id.lv_contacts); 
     lv_contacts.setAdapter(adapter); 
     lv_contacts.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Intent intent = new Intent(MainActivity.this, DetailActivity.class); 
       intent.putExtra(POSITION, position); 
       startActivityForResult(intent, REQUEST_CODE); 
      } 
     }); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == REQUEST_CODE) { 
      if (resultCode == RESULT_OK) { 

      } 
     } 
    } 
} 

DetailActivity

public class DetailActivity extends AppCompatActivity { 
    private TextView first_name, last_name; 
    private EditText phone_number, location; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_detail); 

     int position = getIntent().getIntExtra(MainActivity.POSITION, -1); 
     Contact contact = ContactList.getInstance().getContact(position); 

     first_name = (TextView) findViewById(R.id.tv_first_name); 
     last_name = (TextView) findViewById(R.id.tv_last_name); 
     phone_number = (EditText) findViewById(R.id.et_phone_number); 
     location = (EditText) findViewById(R.id.et_location); 

     first_name.setText(contact.getFirst_name()); 
     last_name.setText(contact.getLast_name()); 
     phone_number.setHint(contact.getPhonenumber()); 
     location.setHint(contact.getLocation()); 
    } 

任何幫助,非常感謝!

+0

您可以將數據保存在SQLite –

回答

0

你已經有了一個ContactList作爲一個單獨的對象,應該使這個變得簡單。如果您正在使用ListView適配器的ContactList來使用您所要做的所有事情,那麼請確定上一個活動如何結束,例如修改了任何聯繫人。如果是這樣,只需在活動的onActivityResult上調用listview適配器的notifydatasetchanged。

相關問題