android sqlite Spinner没有显示SimplCursorAdapter

时间:2015-01-16 12:28:10

标签: android sqlite simplecursoradapter

所以这是我在Stackoverflow上的第一个问题。如果我能够正确地提出我的问题,那就让我们看看:

我有一个SQLite DB(lights_db)和两个Spinners(spinnerLightType和spinnerLightSelector)。取决于spinnerLightType中的选择(" HMI"," Tungsten"," Fluorescent"或" LED"),我喜欢通过使用查询中的选择将不同的light_db项加载到spinnerLightSelector中。查询很好,Cursor有数据,问题是它只是没有在微调器中显示它,我无法弄清楚原因。

以下是MainActivity代码的一部分:

public class MainActivity extends ActionBarActivity {

double Transmission = 1.0;
double Z = 1.0;

Light_SQL light_db = new Light_SQL(this);

Spinner spinnerLightType, spinnerLightSelector;

public Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // add lights to light_db
    createLightDB();
    mContext = this;
    spinnerLightType = (Spinner) findViewById(R.id.spinnerLightType);
    spinnerLightType.setSelection(1);
    spinnerLightType.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
            System.out.println("Technology set to : " + parent.getItemAtPosition(pos).toString());
             Cursor light_cc = light_db.getWritableDatabase().query("lights", // a. table
                    new String[]{"id AS _id", "technology", "name"}, // b. column names
                   "technology=?", // c. selections
                   new String[]{parent.getItemAtPosition(pos).toString()}, // d. selections args
                   null, // e. group by
                   null, // f. having
                   null, // g. order by
                   null); // h. limit
            if (light_cc.moveToFirst()) {
                do {
                    System.out.println("Item : " + light_cc.getString(0) + "Technology : " + light_cc.getString(1) + " name : " + light_cc.getString(2));
                } while (light_cc.moveToNext());
            }
            String[] from = new String[] {"name"};
            int[] to = new int[] {android.R.id.text1};
            SimpleCursorAdapter light_sca = new SimpleCursorAdapter(mContext, android.R.layout.simple_spinner_item, light_cc, from, to);
            light_sca.setDropDownViewResource(android.R.layout.simple_spinner_item);
            spinnerLightSelector.setAdapter(light_sca);
            light_cc.close();
        }
        public void onNothingSelected(AdapterView<?> parent) {}
    });

    spinnerLightSelector = (Spinner) findViewById(R.id.spinnerLightSelector);
    spinnerLightSelector.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
            Z = light_db.getLight((int) id).getZ();
            System.out.println("Z set to : " + Z);
        }
        public void onNothingSelected(AdapterView<?> parent) {}
    });


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

public void createLightDB() {
    // HMI
    // ARRI PocketPar
    light_db.addLight(new Light("HMI","ARRI PocketPar 150", 150, 3.67));
    light_db.addLight(new Light("HMI","ARRI PocketPar 200", 200, 2.49));
    light_db.addLight(new Light("HMI","ARRI PocketPar 400", 400, 1.94));
    // K5600 Joker-Series
    light_db.addLight(new Light("HMI","K5600 Joker 200", 200, 2.44));
    light_db.addLight(new Light("HMI","K5600 Joker 400", 400, 2.44));
    light_db.addLight(new Light("HMI","K5600 Joker 800", 800, 1.45));
    light_db.addLight(new Light("HMI","K5600 Joker 1600", 1600, 0.67));
    // K5600 Alpha-Series
    light_db.addLight(new Light("HMI","K5600 Alpha 4k", 200, 0.45));
    light_db.addLight(new Light("HMI","K5600 Alpha 18k", 400, 0.28));
    // Arri D-Series
    light_db.addLight(new Light("HMI","ARRI D5", 575, 1.78));
    light_db.addLight(new Light("HMI","ARRI D12", 1200, 1.14));
    light_db.addLight(new Light("HMI","ARRI D25", 2500, 0.87));
    light_db.addLight(new Light("HMI","ARRI D40", 4000, 0.69));
    // ArriSun series
    light_db.addLight(new Light("HMI","ARRISUN 25", 2500, 0.87));
    light_db.addLight(new Light("HMI","ARRISUN 40", 4000, 0.69));
    // Arri Compact
    light_db.addLight(new Light("HMI","ARRI Compact 6000", 6000, 0.54));
    // Arri M-Series
    light_db.addLight(new Light("HMI","ARRI M8", 800, 1.49));
    light_db.addLight(new Light("HMI","ARRI M18", 1800, 0.96));
    light_db.addLight(new Light("HMI","ARRI M25", 2500, 0.96));
    light_db.addLight(new Light("HMI","ARRI M40", 4000, 0.60));
    light_db.addLight(new Light("HMI","ARRI M90", 9000, 0.34));
    // Arri Daylight-Series
    light_db.addLight(new Light("HMI","ARRI Daylight 12", 12000, 0.42));
    light_db.addLight(new Light("HMI","ARRI Daylight 18", 18000, 0.36));
    light_db.addLight(new Light("HMI","ARRIMAX 18", 18000, 0.27));

    // Tungstene
    // Fresnels
    light_db.addLight(new Light("Tungsten","150 W Fresnel", 150, 6.76));
    light_db.addLight(new Light("Tungsten","300 W Fresnel", 300, 4.78));
    light_db.addLight(new Light("Tungsten","650 W Fresnel", 650, 2.68));
    light_db.addLight(new Light("Tungsten","1 kW Fresnel", 1000, 2.04));
    light_db.addLight(new Light("Tungsten","2 kW Fresnel", 2000, 1.60));
    light_db.addLight(new Light("Tungsten","5 kW Fresnel", 50000, 0.83));
    light_db.addLight(new Light("Tungsten","10 kW Fresnel", 10000, 0.62));
    light_db.addLight(new Light("Tungsten","12 W Fresnel", 12000, 0.56));
    light_db.addLight(new Light("Tungsten","20 kW Fresnel", 20000, 0.46));
    light_db.addLight(new Light("Tungsten","24 W Fresnel", 240000, 0.42));
    // Open Face
    light_db.addLight(new Light("Tungsten","800 W Open Face", 2000, 2.36));
    light_db.addLight(new Light("Tungsten","2 kW Open Face", 2000, 1.99));

    // Fluorescent
    // KinoFlo
    light_db.addLight(new Light("Fluorescent","KinoFlo 4ft 4Bank", 300, 5.21));
    light_db.addLight(new Light("Fluorescent","KinoFlo 4ft 2Bank", 150, 7.17));
    light_db.addLight(new Light("Fluorescent","KinoFlo 2ft 4Bank", 160, 0.78));
    light_db.addLight(new Light("Fluorescent","KinoFlo 2ft 2Bank", 80, 12.42));
    light_db.addLight(new Light("Fluorescent","KinoFlo 4ft 1Bank", 75, 9.56));
    light_db.addLight(new Light("Fluorescent","KinoFlo 2ft 1Bank", 40, 17.56));

    // LED
    // LitePanel
    light_db.addLight(new Light("LED","LitePanel 30x30", 40, 4.57));
}
}

这里是logcat(证明光标肯定有数据,我选择&#34; LED&#34;然后&#34; Fluorescent&#34;在spinnerLightType中):

01-16 13:17:42.144  31987-31987/com.example.georges.benilux I/System.out﹕ Item : 43 Technology : LED name : LitePanel 30x30
01-16 13:17:47.409  31987-31987/com.example.georges.benilux I/System.out﹕ Technology set to : Fluorescent
01-16 13:17:47.413  31987-31987/com.example.georges.benilux I/System.out﹕ Item : 37 Technology : Fluorescent name : KinoFlo 4ft 4Bank
01-16 13:17:47.413  31987-31987/com.example.georges.benilux I/System.out﹕ Item : 38 Technology : Fluorescent name : KinoFlo 4ft 2Bank
01-16 13:17:47.413  31987-31987/com.example.georges.benilux I/System.out﹕ Item : 39 Technology : Fluorescent name : KinoFlo 2ft 4Bank
01-16 13:17:47.413  31987-31987/com.example.georges.benilux I/System.out﹕ Item : 40 Technology : Fluorescent name : KinoFlo 2ft 2Bank
01-16 13:17:47.413  31987-31987/com.example.georges.benilux I/System.out﹕ Item : 41 Technology : Fluorescent name : KinoFlo 4ft 1Bank
01-16 13:17:47.413  31987-31987/com.example.georges.benilux I/System.out﹕ Item : 42 Technology : Fluorescent name : KinoFlo 2ft 1Bank

那么,为什么光标中的数据不会出现在LightTypeSelector中?其他问题:为什么Android Studio会告诉我&#34; SimpleCursorAdapter&#34;折旧...

1 个答案:

答案 0 :(得分:0)

Android Studio可能会告诉您,不推荐使用SimpleCursorAdapter的构造函数:

public SimpleCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to)

在API级别1中添加 在API级别11中不推荐使用此构造函数。

请参阅Android文档:http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html