需要从sqlite获取微调器中所选项的Id

时间:2014-11-16 17:11:21

标签: android sqlite spinner

我是Android的新手,我有这个问题在这里希望你们可以帮助我这个:) 无论如何,我想从sqlite数据库中获取微调器中所选项的id,以便我以后可以将它保存到另一个表中。 这是我的代码:

在我的DB.java中:

public List<String> getSemesterList() {
     List<String> List = new ArrayList<String>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_SEMESTER;
        Cursor c = ourDatabase.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (c.moveToFirst()) {
            do {
                List.add((c.getString(1)));

            } while (c.moveToNext());
        }
        return List;
}
public String getSemesterId() {
    String[] columns = new String[] { KEY_SEMESTER_ID, KEY_SEMESTER };
    Cursor c = ourDatabase.query(TABLE_SEMESTER, columns, null, null, null, null, null, null);      

    int id = c.getColumnIndex(KEY_SEMESTER_ID);


    String semId = "";

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        semId   = semId  + c.getInt(id) + " " 
                + "\n";
    }

    return semId ;
}

和我的createSYAttended.class

    // TODO Auto-generated method stub
    DB entry = new DB(this);
    entry.open();
    final List<String> all = entry.getSemesterList();
        if(all.size()>0) // check if list contains items.
        {    
        sqlSem = (Spinner) findViewById(R.id.sprSemester);

    arrayAdapter = new ArrayAdapter<String>(CreateSyAttended.this,android.R.layout.simple_spinner_dropdown_item, all);
    arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    sqlSem.setAdapter(arrayAdapter);

    entry.close();


  sqlSem.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
            int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // TODO Auto-generated method stub

    }
});  
   } } 

1 个答案:

答案 0 :(得分:0)

使用List<String> all = entry.getSemesterList();到微调框

的索引的映射

所以当你得到回调

public void onItemSelected(AdapterView<?> parent, View view,
            int position, long id) {

然后,您可以使用回调中的位置来映射到semesterList all

中的项目