如何比较两个微调项目?

时间:2022-04-11 14:02:38

i have created a ticket reservation app in which following data will be saved about passenger. 1) passenger name 2) passenger phone number 3) city from which passenger will go 4) city to which passenger will go

我创建了一个票务预订应用程序,其中将保存关于乘客的以下数据。 1)乘客姓名2)乘客电话号码3)乘客将前往的城市4)乘客将前往的城市

my code is as follows

我的代码如下

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;

now the problem is that if the FROM city and TO city are same, it should display a toast that CITY NAMES ARE SAME. i have tried following code

现在的问题是,如果FROM城市和TO城市相同,它应该显示CITY NAMES相同的Toast。我试过以下代码

if(spFrom.getSelectedItem().equals(spTo.getSelectedItem()))

{
      toast: "city names are same"

}

but it does not work for me

但它对我不起作用

1 个解决方案

#1


Use this

if(spFrom.getSelectedItem().toString().equals(spTo.getSelectedItem().toString())){
  Toast.makeText(this,"city names are same",Toast.LENGTH_LONG).show();
}

This will work

这会奏效

#1


Use this

if(spFrom.getSelectedItem().toString().equals(spTo.getSelectedItem().toString())){
  Toast.makeText(this,"city names are same",Toast.LENGTH_LONG).show();
}

This will work

这会奏效