2015-11-25 119 views
0

嗨我已經創建了以下代碼來填充存儲在SQLite數據庫中的表的第1列中的數據列表的微調器。當我運行程序時,微調器上沒有任何東西出現。我粘貼了下面的代碼。如果有人能夠幫助我解決高度讚賞的問題。用sqlite數據填充微調器

public class MainActivity extends ActionBarActivity implements AdapterView.OnItemSelectedListener { 
    DatabaseHelper myDb; 
    Spinner group; 
    Spinner volt1; 
    Spinner sf; 
    Spinner energy; 
    String group_text; 
    String sf_text; 
    String energy_text; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     myDb = new DatabaseHelper(this); 

     volt1 = (Spinner) findViewById(R.id.spinner_voltage); 

     List<String>names = myDb.getAllNames(); 
     ArrayAdapter<String> adaptervolt = new ArrayAdapter<String>(this, R.layout.spinner_layout, R.id.txt, names); 
     adaptervolt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     volt1.setAdapter(adaptervolt); 
    } 

public class DatabaseHelper extends SQLiteOpenHelper { 

    public static final String TABLE_NAME2 = "Spark Energy Table"; 

    public DatabaseHelper(Context context) { 
     super(context, DATABASE_NAME, null, 1); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

    } 

    public List<String> getAllNames() { 
     List<String> names = new ArrayList<String>(); 
     SQLiteDatabase db = this.getReadableDatabase(); 
     String selectQuery = "SELECT * FROM"+ TABLE_NAME2; 

     db.beginTransaction(); 
     try { 
      Cursor cursor_volt = db.rawQuery(selectQuery, null); 
      if (cursor_volt.moveToFirst()) { 
       do { 
        names.add(cursor_volt.getString(0)); 
       } while (cursor_volt.moveToNext()); 
       cursor_volt.close(); 
      } 

      db.setTransactionSuccessful(); 
     } catch (SQLiteException e) { 
      e.printStackTrace(); 
     } finally { 
      db.endTransaction(); 
      db.close(); 
     } 
     return names; 
    } 
} 

的Xml文件1:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="1"> 

    <TextView 
     android:id="@+id/txt" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 

的Xml文件2:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" 
    android:id="@+id/id1"> 

    <Spinner 
     android:layout_width="100dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/spinner_voltage"  
     android:layout_alignTop="@+id/textView2" 
     android:layout_toRightOf="@+id/textView2" 
     android:layout_toEndOf="@+id/textView2" /> 
</RelativeLayout> 

回答

0

你沒有助手類創建數據庫。 首先在你的輔助類的oncreate方法中創建你的數據庫。

您需要執行create語句來創建表和列。

試試這個教程的幫助 sqlite tutorial