2015-10-17 141 views
0

我有一個spinner項目,並且該值已被插入到SQLite。現在我想將項目檢索到位於Update.java中的另一個spinner將微調項目讀取到另一個微調項目

Information.java

public void addItemsOnSpinner2() { 
     spinner2 = (Spinner) findViewById(R.id.spinner2); 
     List<String> list = new ArrayList<String>(); 
     list.add("Sunny"); 
     list.add("Cloudy"); 
     list.add("Rainy"); 
     list.add("Thunderstorm"); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(Information.this, android.R.layout.simple_spinner_dropdown_item, list); 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinner2.setAdapter(adapter); 

Update.java

public void addWeather() { 
    weather3 = (Spinner) findViewById(R.id.spinner5); 
    List<String> list = new ArrayList<String>(); 
    list.add("Sunny"); 
    list.add("Cloudy"); 
    list.add("Rainy"); 
    list.add("Thunderstorm"); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(UpdatePage.this, android.R.layout.simple_spinner_dropdown_item, list); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    weather3.setAdapter(adapter); 
    String Weather = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Weather)); 
    weather3.setText(Weather); 
} 

當然,這種方法行不通和setText不能得到解決。我一直在尋找解決方案很長一段時間,但似乎沒有太多的信息可以找到。任何想法或建議將不勝感激。

編輯

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     dbHelper = new MyDatabaseHelper(this); 
     setContentView(R.layout.updatepage); 
     final String name = getIntent().getExtras().getString("name1"); 
     final String date=getIntent().getExtras().getString("date1"); 
     final String ID = getIntent().getExtras().getString("ID"); 
     addWeather(); 
     RetrievePage(name,date,ID); 
    } 

    public void addWeather() { 
      weather3 = (Spinner) findViewById(R.id.spinner5); 
      String[] arr = new String[]{"Sunny","Cloudy","Rainy","Thunderstorm"}; 
      List<String> list = new ArrayList<String>(); 
      String weather = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Weather)); 
      list.add(weather); 
      for(String s:arr){ 
       if(!list.contains(s)){ 
        list.add(s); 
       } 
      } 
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(UpdatePage.this, android.R.layout.simple_spinner_dropdown_item, list); 
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
      weather3.setAdapter(adapter); 
     } 

public void RetrievePage(String name,String date, String id) { 
     final String name2 = name; 
     final String date2=date; 
     final String id2 = id; 
     final EditText name3 = (EditText) findViewById(R.id.editText9); 
     final EditText date3 = (EditText) findViewById(R.id.editText12); 
     name3.setText(name2); 
     date3.setText(date2); 

     final EditText subC3 = (EditText) findViewById(R.id.editText17); 
     final EditText noP = (EditText) findViewById(R.id.editText18); 
     final EditText noH = (EditText) findViewById(R.id.editText19); 
     final Spinner poject3 = (Spinner) findViewById(R.id.spinner8); 


     database = dbHelper.getWritableDatabase(); 
     c = database.rawQuery("SELECT i.Weather, i.Status,w.Subcontractors, w.NumberOfPerson, w.NumberOfHours FROM Information i LEFT JOIN WorkForce w ON w.TInfo_id = i._id WHERE i.Name = ? AND i._id= ? ", 
       new String[]{String.valueOf(name2),String.valueOf(id2)}, null); 
     if (c != null) { 
      while (c.moveToNext()) { 
       Info I = new Info(); 
       Details WD = new Details(); 
       Force WF = new Force(); 
       String Weather = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Weather)); 
       String Status = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Status)); 
       String SubC = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Subcontractors)); 
       String NoP = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.NumberOfPerson)); 
       String NoH = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.NumberOfHours)); 
       I.setWeather(Weather); 
       I.setStatus(Status); 
       WF.setSubcontractors(SubC); 
       WF.setNoOfPerson(NoP); 
       WF.setNoOfHours(NoH); 
       subC3.setText(SubC); 
       noP.setText(NoP); 
       noH.setText(NoH); 

      } 

     } 
     c.close(); 

錯誤

Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int android.database.Cursor.getColumnIndexOrThrow(java.lang.String)' on a null object reference 
+0

對'spinner2'做同樣的事情,除了只給'list'列表'添加'Weather''字符串。 – Titus

+0

你的意思是ArrayAdapter 適配器? – Hoo

+0

是的,創建一個適配器,並將'Weather'添加到您傳遞給適配器構造函數的列表中。 – Titus

回答

0

我不知道我理解你想要達到什麼,但如果你想顯示在spinner2選擇的項目在weather3微調,你可以做這樣的事情:

public void addWeather() { 
     weather3 = (Spinner) findViewById(R.id.spinner5); 
     String[] arr = new String[]{"Sunny","Cloudy","Rainy","Thunderstorm"}; 
     List<String> list = new ArrayList<String>(); 
     String weather = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Weather)); 
     list.add(weather); 
     for(String s:arr){ 
      if(!list.contains(s)){ 
       list.add(s); 
      } 
     } 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(UpdatePage.this, android.R.layout.simple_spinner_dropdown_item, list); 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     weather3.setAdapter(adapter); 
    } 

這個想法是添加您想要顯示的項目作爲ArrayList中的第一項。

+0

這似乎是我尋找的答案。然而,我得到Cursor.getColumnIndexOrThrow(java.lang.String)'空對象引用.... – Hoo

+0

@Hoo看來'c'是'null'。 – Titus

+0

需要將字符串天氣移動到retrievePage()?請參閱編輯後的文章 – Hoo

相關問題