0

大家好,我有这样的数据库

public class Dbhelper extends SQLiteOpenHelper{

private static final String DATABASE_NAME="ComboBox.db";
private static final int SCHEMA_VERSION=1;
public SQLiteDatabase db;
public String [][] isiDb={
        {"MI","Minimarket"},{"IDM","Indomaret"},{"CRM","Ceriamart"},{"OMI","OMI"},{"CK","Circle K"},
        {"7EL","7 Eleven"},{"STM","Starmart"},{"AX","Alfa Express"},{"MID","Alfa Midi"},{"LWN","Lawson"},
        {"YMT","Yomart"},{"HRO","Hero"},{"SDO","Superindo"},{"HPM","Hypermart"},{"SWN","Swalayan"}

};

public Dbhelper(Context context) {
    super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE isicombo (kdcombo text PRIMARY KEY,descp text)");
    for(int i=0; i<=1;i++)
     {
         db.execSQL("INSERT INTO isicombo(kdcombo,descp) VALUES ('"+isiDb[i][0]+"','"+isiDb[i][1]+"')");
     }
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // no-op, since will not be called until 2nd schema
    // version exists
    db.execSQL("DROP TABLE IF EXIST ComboBox.db");
    onCreate(db);

}

public Cursor getAllProjectss() {
    // TODO Auto-generated method stub
    return(getReadableDatabase()
            .rawQuery("SELECT * from isicombo",null));
}

我想创建一个组合框,我想用我的数据库中的数据填充这个组合框..我很困惑如何将数据填充到组合框..你能给我一个简单的代码吗?谢谢

我尝试使用这样的代码

public class CobaCombo extends Activity implements AdapterView.OnItemSelectedListener {

TextView selection;
Dbhelper helper = new Dbhelper(this);
String temp;


@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    selection = (TextView) findViewById(R.id.selection);
    Spinner spin = (Spinner) findViewById(R.id.spinner);
    Cursor anu = helper.getAllProjectss();
    if(anu.moveToFirst()){
        do{
            temp += anu.getString(1);

        }while(anu.moveToNext());
    }

    String[] isi = { ""+temp+"" };
    System.out.println(isi);    
    spin.setOnItemSelectedListener(this);
    ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, isi);
    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin.setAdapter(aa);

}

但是组合框以长字符串从数据库中填充数据..不是下拉项..请帮助我

4

2 回答 2

0

您现在可以使用rawQuery进行查询,它会返回一个Cursor

Cursor mCursor = mSQLiteDatabase.rawQuery("Select * from myTable", null);

现在,您必须遍历游标并获取所需的数据。这是示例代码

于 2012-04-16T07:30:13.997 回答
0

在你的声明中

db.execSQL("CREATE TABLE isicombo (kdcombo text PRIMARY KEY,descp text)");

最后加上;

db.execSQL("CREATE TABLE isicombo (kdcombo text PRIMARY KEY,descp text);");

与插入语句相同的错误。

于 2012-04-16T07:31:35.600 回答