【问题标题】:Inserting data from database into spinner in android将数据库中的数据插入android中的微调器
【发布时间】:2013-05-16 08:11:03
【问题描述】:

谁能教我如何从 SQLite 数据库中获取数据并将从数据库中收集到的所有信息放入 Spinner 中。例如,我的数据库中有一个 tblState,我需要从数据库中获取所有 State_name 并将其获取到微调器。我将如何做到这一点?谢谢!

【问题讨论】:

    标签: android sqlite android-spinner


    【解决方案1】:

    以下是带有 sqlite 的 Android 示例。它对初学者非常有用。示例包含 ListActivity,而不是 Spinner。只要正确学习示例,您就可以根据您的要求进行更改。

    public class CRUDonDB extends ListActivity {
    
        private final String SAMPLE_DB_NAME = "myFriendsDb";
        private final String SAMPLE_TABLE_NAME = "friends";
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
    
            ArrayList<String> results = new ArrayList<String>();
            SQLiteDatabase sampleDB = null;
    
            try {
                sampleDB =  this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
    
                sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                        SAMPLE_TABLE_NAME +
                        " (LastName VARCHAR, FirstName VARCHAR," +
                        " Country VARCHAR, Age INT(3));");
    
                sampleDB.execSQL("INSERT INTO " +
                        SAMPLE_TABLE_NAME +
                        " Values ('Makam','Sai Geetha','India',25);");
                sampleDB.execSQL("INSERT INTO " +
                        SAMPLE_TABLE_NAME +
                        " Values ('Chittur','Raman','India',25);");
                sampleDB.execSQL("INSERT INTO " +
                        SAMPLE_TABLE_NAME +
                        " Values ('Solutions','Collabera','India',20);");
    
                Cursor c = sampleDB.rawQuery("SELECT FirstName, Age FROM " +
                        SAMPLE_TABLE_NAME +
                        " where Age > 10 LIMIT 5", null);
    
                if (c != null ) {
                    if  (c.moveToFirst()) {
                        do {
                            String firstName = c.getString(c.getColumnIndex("FirstName"));
                            int age = c.getInt(c.getColumnIndex("Age"));
                            results.add("" + firstName + ",Age: " + age);
                        }while (c.moveToNext());
                    } 
                }
    
                this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
    
            } catch (SQLiteException se ) {
                Log.e(getClass().getSimpleName(), "Could not create or Open the database");
            } finally {
                if (sampleDB != null) 
                    sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME);
                    sampleDB.close();
            }
        }
    }
    

    也看看这个Spinner Example

    【讨论】:

    • @MaciejGórski,是的,同意,但正如我所说,这是非常初学者的例子。
    猜你喜欢
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多