2015-05-11 87 views
0

我已經創建了一個機票預訂應用程序,其中將保存關於乘客的以下數據。 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 ARE SAME的敬酒。 我曾嘗試下面的代碼

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

{ 
     toast: "city names are same" 

}

,但它不爲我工作

+0

您可以驗證'字符串#equals'被調用並且這兩個選擇確實相​​等? – Smutje

回答

0

使用此

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

這將工作