我有3个微调器A,B,C,我希望当用户从A中选择元素时,B将获得他的项目。它的工作很好:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> aParentView, View aView, int aPosition, long anId) {
String selectedCompany2=String.valueOf(spinner.getSelectedItem());
String selectedCompany = getResources().getStringArray(R.array.songsArray)[spinner.getSelectedItemPosition()]; //the company name
ArrayList<String> results = new ArrayList<String>();
x="'"+selectedCompany2+"'";
Cursor c = customersDB.rawQuery("SELECT Name, Street, Block, City FROM " +
TABLE_NAME +
" where Name == "+ x , null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String name = c.getString(c.getColumnIndex("Name"));
String street = c.getString(c.getColumnIndex("Street"));
String city = c.getString(c.getColumnIndex("City"));
if(!results.contains(street))
results.add(street);
}while (c.moveToNext());
}
}
String [] countries = results.toArray(new String[results.size()]);
moduleOfSpecificCompany = (Spinner) findViewById(R.id.degemSpinner1);
ArrayAdapter<String> spinnerArrayAdapter_degem = new ArrayAdapter<String>(DelekActivity.this, android.R.layout.simple_spinner_item, countries);
spinnerArrayAdapter_degem.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down vieww
moduleOfSpecificCompany.setAdapter(spinnerArrayAdapter_degem);
}
public void onNothingSelected(AdapterView<?> aParentView) {
// your code here
}
});
但是,我也希望当用户在B中选择某些内容时,会出现c的数据。我写了这个,我收到了一个错误:
moduleOfSpecificCompany.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> aParentView, View aView, int aPosition, long anId) {
String selectedCompany3=String.valueOf(moduleOfSpecificCompany.getSelectedItem());
ArrayList<String> results2 = new ArrayList<String>();
x="'"+selectedCompany3+"'";
Cursor c = customersDB.rawQuery("SELECT Name, Street, Block, City FROM " +
TABLE_NAME +
" where Name == "+ x , null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String name = c.getString(c.getColumnIndex("Name"));
String street = c.getString(c.getColumnIndex("Street"));
String Block = c.getString(c.getColumnIndex("Block"));
String city = c.getString(c.getColumnIndex("City"));
if(!results2.contains(Block))
results2.add(Block);
}while (c.moveToNext());
}
}
String [] countries2 = results2.toArray(new String[results2.size()]);
/*moduleOfSpecificCompany = (Spinner) findViewById(R.id.manoaSpinner1);
ArrayAdapter<String> spinnerArrayAdapter_manoa = new ArrayAdapter<String>(DelekActivity.this, android.R.layout.simple_spinner_item, countries2);
spinnerArrayAdapter_manoa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down vieww
moduleOfSpecificCompany.setAdapter(spinnerArrayAdapter_manoa);
if (customersDB != null)
customersDB.execSQL("DELETE FROM " + TABLE_NAME);
customersDB.close();
**/
}
public void onNothingSelected(AdapterView<?> aParentView) {
// your code here
}
});
代码不太相关。关键是当我使用setOnItemSelectedListener 2次时出现错误。感谢ypi,对不起我的英语。
日志:
10-07 01:15:30.907: D/dalvikvm(2588): GC_EXTERNAL_ALLOC freed 748 objects / 55680 bytes in 242ms
10-07 01:15:45.305: D/dalvikvm(2588): GC_FOR_MALLOC freed 3176 objects / 154984 bytes in 116ms
10-07 01:16:10.644: D/dalvikvm(2588): GC_EXTERNAL_ALLOC freed 186 objects / 8784 bytes in 83ms
10-07 01:16:12.674: D/AndroidRuntime(2588): Shutting down VM
10-07 01:16:12.674: W/dalvikvm(2588): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-07 01:16:12.684: E/AndroidRuntime(2588): FATAL EXCEPTION: main
10-07 01:16:12.684: E/AndroidRuntime(2588): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.save_money/com.example.save_money.DelekActivity}: java.lang.NullPointerException
10-07 01:16:12.684: E/AndroidRuntime(2588): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-07 01:16:12.684: E/AndroidRuntime(2588): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-07 01:16:12.684: E/AndroidRuntime(2588): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-07 01:16:12.684: E/AndroidRuntime(2588): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-07 01:16:12.684: E/AndroidRuntime(2588): at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 01:16:12.684: E/AndroidRuntime(2588): at android.os.Looper.loop(Looper.java:123)
10-07 01:16:12.684: E/AndroidRuntime(2588): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-07 01:16:12.684: E/AndroidRuntime(2588): at java.lang.reflect.Method.invokeNative(Native Method)
10-07 01:16:12.684: E/AndroidRuntime(2588): at java.lang.reflect.Method.invoke(Method.java:521)
10-07 01:16:12.684: E/AndroidRuntime(2588): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-07 01:16:12.684: E/AndroidRuntime(2588): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-07 01:16:12.684: E/AndroidRuntime(2588): at dalvik.system.NativeStart.main(Native Method)
10-07 01:16:12.684: E/AndroidRuntime(2588): Caused by: java.lang.NullPointerException
10-07 01:16:12.684: E/AndroidRuntime(2588): at com.example.save_money.DelekActivity.onCreate(DelekActivity.java:3057)
10-07 01:16:12.684: E/AndroidRuntime(2588): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-07 01:16:12.684: E/AndroidRuntime(2588): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-07 01:16:12.684: E/AndroidRuntime(2588): ... 11 more
答案 0 :(得分:0)
如果您想要查询中多个名称的数据,则需要使用
之类的查询SELECT Name, Street, Block, City FROM TableName WHERE Name = '1stName'
然后,如果用户选择C,您可以附加OR Name = '2ndName'
然后追加';'