无法从sqlite数据库填充微调器

时间:2013-09-11 17:22:16

标签: java android sqlite

我无法从sqlite db填充微调器。有人可以帮帮我吗?

以下是我的代码

LoginDatabaseAdapter.java

public List<String> GetAllLabels(){
    List<String> labels = new ArrayList<String>();
    try
    {
        String selectQuery = "SELECT  ID,category FROM CategoryMaster";
        SQLiteDatabase db = this.getDatabaseInstance();

        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {

            do {

                labels.add(cursor.getString(1));

            } while (cursor.moveToNext());
        }
        // closing connection
        cursor.close();
    }
    catch(Exception e)
    {
        String StackTrace=Log.getStackTraceString(e);
        Toast.makeText(context,lineno+StackTrace,Toast.LENGTH_LONG).show();
    }
    // returning lables
    return labels;
}
}

AddProduct.java

private void loadSpinnerData() 
{
    try
    {

        LoginDataBaseAdapter db = new LoginDataBaseAdapter(getApplicationContext());
        List<String> lables = db.GetAllLabels();

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, lables);

        // Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        Spinnercategory.setAdapter(dataAdapter);
    }
    catch(Exception e)
    {
        String stackTrace = Log.getStackTraceString(e);
        Toast.makeText(getApplicationContext(), stackTrace, Toast.LENGTH_LONG).show();
    }
}

提前感谢你

1 个答案:

答案 0 :(得分:0)

在我声明了微调器并在loadSpinnerData()中获取微调器之后。之后我能够在微调器中填充项目