我正在使用虚拟数据创建一个联系人管理器,我目前有一个包含联系人ArrayList的公共字段,即public static ArrayList contactList = new ArrayList();
点击特定联系人后(从查看联系人屏幕),我将进入编辑联系人屏幕,其中显示该特定联系人的所有字段。即名称,手机,地址,电子邮件等。列表中的第一个联系人在单击时会正确显示其详细信息。但是,当我返回到视图联系人屏幕时,它仍然显示第一个联系人的字段。
在我的MainActivity.java类中:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parentView, View clickedView, int clickedViewPosition, long id){
TextView firstName = (TextView) findViewById(R.id.firstName);
String fName = firstName.getText().toString();
Intent theIntent = new Intent(getApplication(), EditContact.class);
theIntent.putExtra("firstName", fName);
startActivity(theIntent);
}
});
我将联系人的firstName传递给putExtra(),这将我带到EditContact类,我在其中运行ArrayList并获取具有相同firstName的联系人,然后获取其所有字段(存储在相应的文本中)框)。
@Override
public void onCreate(Bundle savedInstanceState) {
// Get saved data if there is any
super.onCreate(savedInstanceState);
// Designate that add_new_contact.xml is the interface used
setContentView(R.layout.edit_contact);
// Initialize the EditText objects
firstName = (EditText) findViewById(R.id.firstName);
lastName = (EditText) findViewById(R.id.lastName);
dateOfBirth = (EditText) findViewById(R.id.dateOfBirth);
mobileNumber = (EditText) findViewById(R.id.mobileNumber);
homeNumber = (EditText) findViewById(R.id.homeNumber);
workNumber = (EditText) findViewById(R.id.workNumber);
homeEmail = (EditText) findViewById(R.id.emailAddressHome);
workEmail = (EditText) findViewById(R.id.emailAddressWork);
homeAddress = (EditText) findViewById(R.id.homeAddress);
workAddress = (EditText) findViewById(R.id.workAddress);
Intent theIntent = getIntent();
String thefirstName = theIntent.getStringExtra("firstName");
for (Contact con: MainActivity.contactList){
if (con.getFirstName().equals(thefirstName)){
firstName.setText(con.getFirstName());
lastName.setText(con.getLastName());
dateOfBirth.setText(con.getDateOfBirth());
mobileNumber.setText(con.getMobileNumber());
homeNumber.setText(con.getHomeNumber());
workNumber.setText(con.getWorkNumber());
homeEmail.setText(con.getHomeEmail());
workEmail.setText(con.getWorkEmail());
homeAddress.setText(con.getHomeAddress());
workAddress.setText(con.getWorkAddress());
}
}
}
当我点击其他联系人时,有谁知道为什么putExtra()中传递的firstName不会改变?