您好我已经创建了以下代码来填充spinner,其中包含SQLite数据库中存储的表的第1列中的数据列表。当我运行程序时,旋转器上没有任何内容。我已粘贴下面的代码。如果有人可以帮助我解决那个值得高度赞赏的问题。
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 :(得分:0)