I have a spinner
item and the value has been inserted to SQLite
.Now I want to retrieve the item out to another spinner
which is in Update.java
.
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);
}
Of course this method doesn't work and the setText
cannot be resolved. I've been searching the solution for a long time but it seems like not much information can be found . Any idea or suggestions would be greatly appreciated.
Edited
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();
Error
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int android.database.Cursor.getColumnIndexOrThrow(java.lang.String)' on a null object reference