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());
}
任何幫助,非常感謝!
您可以將數據保存在SQLite –