我有一个带有两个用于数据输入的微调器的活动。
我还有一个带有代码的按钮,用于将所选项目值插入数据库。
我还添加了代码,以便在数据成功添加到数据库后从微调器中删除该项。
问题在于,这是第一次成功。
如果我选择其他项目,填写其他所需数据并单击要添加的按钮,我仍然会获得成功添加的上一项的ID。我已经添加了逻辑来防止这种情况发生,从而失败了。
关闭活动并重新启动后,它再次起作用... 以下是代码。
public void Add(View v)
{
// get tenant id
// check whether appartment occupied ...and still active
// check tenants..if same tenant? ask whether want to renew and end previous tenacy
// test overlapping tenancy
int pos= spinner.getSelectedItemPosition();
TenantList tenant= (TenantList)t.get(pos);
// get tenant id
int tenant_id= tenant.get_id();
int pos1= spinner1.getSelectedItemPosition();
Apartments a = (Apartments)b.get(pos1);
//get apartment id
int apartment_id= a.get_id();
// tenant has a valid tenancy
if(db.HasValidTenancy(tenant_id)==true)
{
// inform user and exit
// make a toast
Context context = this.getApplicationContext();
CharSequence msg ="This Tenant has a vaild running teanancy"+ "\n"+
"If the tenancy has expired first make it expired"+ "\n"+
"Before creating a new tenancy";
int duration =Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, msg, duration);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return;
}
// apartment free?
if(db.ApartmentNotFree(apartment_id)==true)
{
// inform user and exit
// make a toast
Context context = this.getApplicationContext();
CharSequence msg ="This apartment not free"+ "\n"+
"If the tenant left, first teminate the Tenancy";
int duration =Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, msg, duration);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return;
}
// is same tenant?
Date sdate=null;
Date edate=null;
try {
sdate = fm.parse(txtviewstartdate.getText().toString());
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
edate =fm.parse( txtvieweenddate.getText().toString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String startdate = fm.format(sdate);
String enddate =fm.format(edate);
EditText rental= (EditText)
findViewById(R.id.editTextRental);
// test rent ented
if(rental.getText().toString().length()==0)
{
// inform user and exit
// make a toast
Context context = this.getApplicationContext();
CharSequence msg ="You must enter the Monthly Rental amount";
int duration =Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, msg, duration);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return;
}
double rent = Double.parseDouble(rental.getText().toString());
db.AddTenancy(tenant_id, apartment_id, startdate, enddate, rent, 0);
// can u refresh data on spinners?
// remove apartment
// remove tenant
tenantlist.remove(pos);
apartments.remove(pos1);
dataAdapter.notifyDataSetChanged();
dataAdapter1.notifyDataSetChanged();
}
有什么想法吗?或者我需要在上面的代码中重新启动Activity?
罗纳德
答案 0 :(得分:0)
您将从名为t和b的列表中获取租户和公寓:
TenantList tenant= (TenantList)t.get(pos);
Apartments a = (Apartments)b.get(pos1);
但是你要将它们从名为tenanlist和apartments的不同列表中删除:
tenantlist.remove(pos);
apartments.remove(pos1);
如果必须
,您应该使用相同的列表,或从两者中删除它