2017-08-04 50 views
1

我在我的Unterkunft Activity中實現了一個微調框,它由來自sqlite數據庫的數據填充。 在這個活動中,我有幾個edit texts和這個spinner。我希望插入的值和來自微調器的選定值保存在數據庫中。編輯文本值沒有問題。但是,如何將微調器值保存在數據庫中?如何保存來自sqlite數據庫中的微調框的值

我加入了Unterkunft acitvity

public class activity_unterkunft extends AppCompatActivity { 

    DatabaseHelper myDb; 
    Button btn_save; 
    Spinner ChooseProject; 
    EditText Entfernung,Price,MWST; 


    private BottomNavigationView bottomNavigationView; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_unterkunft); 
     myDb = new DatabaseHelper(this); 

     ChooseProject = (Spinner) findViewById(R.id.ChooseProject); 
     Entfernung = (EditText) findViewById(R.id.Entfernung); 
     Price = (EditText) findViewById(R.id.Preis); 
     MWST = (EditText) findViewById(R.id.MwSt); 
     btn_save=(Button) findViewById(R.id.btn_save); 
     loadSpinnerData(); 
     SaveData(); 


     //++++++++++++BOTTOM NAVIGATION BAR++++++++++++// 
     bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView); 

     bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener(){ 
      @Override 
      public boolean onNavigationItemSelected(@NonNull MenuItem item){ 
       if (item.getItemId()==R.id.menu_start){ 
        startActivity(new Intent(activity_unterkunft.this, MainActivity.class)); 
       } else if(item.getItemId()==R.id.menu_allgemein){ 
        startActivity(new Intent(activity_unterkunft.this, activity_allgemein.class)); 
       } else if(item.getItemId()==R.id.menu_transport){ 
        startActivity(new Intent(activity_unterkunft.this, activity_transport.class)); 
       } else if(item.getItemId()==R.id.menu_rechnung){ 
        startActivity(new Intent(activity_unterkunft.this, activity_rechnung.class)); 
       } else if(item.getItemId()==R.id.menu_unterkunft){ 
        startActivity(new Intent(activity_unterkunft.this, activity_unterkunft.class)); 
       } 
       return true; 
      } 
     }); 

     bottomNavigationView.setSelectedItemId(R.id.menu_unterkunft); 
    } 

    /** 
    * Function to load the spinner data from SQLite database 
    * */ 
    private void loadSpinnerData() { 
     // database handler 
     DatabaseHelper db = new DatabaseHelper (getApplicationContext()); 

     // Spinner Drop down elements 
     List<String> projects = db.getAllProjects(); 

     // Creating adapter for spinner 
     ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_spinner_item, projects); 

     // Drop down layout style - list view with radio button 
     dataAdapter 
       .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

     ChooseProject.setPrompt("Projekt auswählen"); 


     // attaching data adapter to spinner 
     ChooseProject.setAdapter(dataAdapter); 
    } 


    //++++++++++++Save Data++++++// 
    public void SaveData(){ 
     btn_save.setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         boolean isInserted = myDb.createUnterkunft(
           Integer.valueOf(Price.getText().toString()), 
           Integer.valueOf(MWST.getText().toString()), 
           Integer.valueOf(Entfernung.getText().toString()) 
         ); 
         if(isInserted=true) 
          Toast.makeText(activity_unterkunft.this, "Daten gespeichert", Toast.LENGTH_LONG).show(); 
         else 
          Toast.makeText(activity_unterkunft.this, "Daten nicht gespeichert", Toast.LENGTH_LONG).show(); 

        } 
       } 
     ); 
    } 
} 
+0

請參閱:[如何避免濫用標籤?](https://meta.stackoverflow.com/questions/354427/how-do-i-avoid-misusing-tags) – EJoshuaS

回答

1

使用setOnItemSelectedListener得到微調值

String item; 

    ChooseProject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
       public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 
        item =String.ValueOf(parent.getItemAtPosition(pos)); 
       } 
       public void onNothingSelected(AdapterView<?> parent) { 
       } 
      }); 

然後您可以將item保存到數據庫。

+0

我實現了你的想法並改變了它一點點。 相反 '項目=將String.valueOf(parent.getItemAtPosition(POS));' 我用 'selectedspinner = parent.getSelectedItem()的toString();' 但我還有問題總是第一個值保存在數據庫中。儘管我選擇了第二個例子。你知道我怎麼能解決這個問題嗎? – Stelios

+1

我解決了它!我現在使用'selectedspinner =(String)ChooseProject.getSelectedItem();' 謝謝! – Stelios

+0

恭喜男人! –

相關問題