我创建了一个机票预订应用程序,其中将保存关于乘客的以下数据。 1)乘客姓名 2)乘客电话号码 3)乘客将前往的城市 4)乘客将前往的城市
我的代码如下
public class TicketReservationView extends Activity implements OnClickListener {
private static Database theDatabase;
private EditText etName,etContact;
private Spinner spFrom,spTo;
private Button bSave,bShowSms;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ticket_reservation_view);
etName = (EditText)findViewById(R.id.etPassengerName);
etContact = (EditText)findViewById(R.id.etContactNo);
spFrom = (Spinner)findViewById(R.id.spFrom);
spTo = (Spinner)findViewById(R.id.spTo);
bSave = (Button)findViewById(R.id.bSave);
bSave.setOnClickListener(this);
bShowSms = (Button)findViewById(R.id.bShowSms);
bShowSms.setOnClickListener(this);
db = theDatabase.getWritableDatabase();
theDatabase.insert_city_data(db);
db.close();
loadData();
private void loadData()
{
db=theDatabase.getReadableDatabase();
String[] cities=theDatabase.getCitiesData(db);
ArrayAdapter<String> fromAdapter = new ArrayAdapter<String>(TicketReservationView.this,android.R.layout.simple_spinner_item,cities);
fromAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spFrom.setAdapter(fromAdapter);
spTo.setAdapter(fromAdapter);
db.close();
}
private String[] fillData()
{
String[] data= new String[4];
data[0] = etName.getText().toString();
data[1] = etContact.getText().toString();
data[2] = (String)spFrom.getSelectedItem();
data[3] = (String)spTo.getSelectedItem();
return data;
}
switch(view.getId())
{
case R.id.bSave:
db = theDatabase.getReadableDatabase();
boolean isPassengerExist = theDatabase.getPassengerExistance(db,etContact.getText().toString());
db.close();
if(!isPassengerExist)
{
String[] data = fillData();
db = theDatabase.getWritableDatabase();
theDatabase.insert_ticket_data(data,db);
showToast("Data saved Successfully");
db.close();
}
else
{
Toast.makeText(getBaseContext(), "Already Exist!", Toast.LENGTH_LONG).show();
}
break;
现在的问题是,如果FROM城市和TO城市相同,它应该显示CITY NAMES相同的Toast。 我试过以下代码
if(spFrom.getSelectedItem().equals(spTo.getSelectedItem()))
{
toast: "city names are same"
}
但它对我不起作用
答案 0 :(得分:0)
使用此
if(spFrom.getSelectedItem().toString().equals(spTo.getSelectedItem().toString())){
Toast.makeText(this,"city names are same",Toast.LENGTH_LONG).show();
}
这将有效