我有一個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
對'spinner2'做同樣的事情,除了只給'list'列表'添加'Weather''字符串。 –
Titus
你的意思是ArrayAdapter適配器? –
Hoo
是的,創建一個適配器,並將'Weather'添加到您傳遞給適配器構造函數的列表中。 – Titus