我有一个spinner
项,并且该值已插入SQLite
。现在,我想将该项检索到spinner
中的另一个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);
}
当然这种方法不起作用,无法解析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 :(得分:0)
我不确定我是否完全了解您要实现的目标,但如果您想在onNewIntent()
微调器中显示spinner2
中选择的项目,您可以执行以下操作:< / p>
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);
}
中的第一项。