简体   繁体   English

使用sqlite数据填充微调器

[英]populate spinner with sqlite data

Hi I have created following code to populate spinner with list of data in column 1 of table stored in SQLite database. 嗨,我创建了以下代码以使用存储在SQLite数据库中的表的第1列中的数据列表填充微调器。 When I ran the program nothing appears on the spinner. 当我运行程序时,微调器上没有任何显示。 I have pasted the code below. 我已经粘贴了下面的代码。 If someone could help me with the issue that would be highly appreciated. 如果有人可以帮助我解决这个问题,将不胜感激。

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 file 1: 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 file 2: 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>

you have not created your database in your helper class. 您尚未在助手类中创建数据库。 first create your database inside your oncreate method of you're helper class. 首先在助手类的oncreate方法中创建数据库。

you need to execute your create statement to create table and columns. 您需要执行create语句来创建表和列。

try this tutorial for help sqlite tutorial 试试这个教程寻求帮​​助sqlite教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM