私は 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