0

我有 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
4

1 回答 1

0

如果您想要查询中来自多个名称的数据,您需要使用类似的查询

SELECT Name, Street, Block, City FROM TableName WHERE Name = '1stName'

然后如果用户选择C,你可以追加OR Name = '2ndName'

然后附加一个';'

于 2012-10-07T01:17:11.453 回答