微调器在android中工作不正常
当第一个微调器的值插入到第二个微调器时,我对微调器下拉列表有问题。该值已存储在sqlite db中。我想做的是两个按钮微调器从db中输出不同的值。我不知道如何将该值插入第二个微调器。已尝试为databasehelper中的第二个微调器创建新方法,但仍不起作用微调器在android中工作不正常,android,spinner,Android,Spinner,当第一个微调器的值插入到第二个微调器时,我对微调器下拉列表有问题。该值已存储在sqlite db中。我想做的是两个按钮微调器从db中输出不同的值。我不知道如何将该值插入第二个微调器。已尝试为databasehelper中的第二个微调器创建新方法,但仍不起作用 1) DatabaseHelper package com.development.user.onebajacrops; import android.content.ContentValues; import android.conte
1) DatabaseHelper
package com.development.user.onebajacrops;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
/**
* Created by User on 22/10/2017.
*/
public class DatabaseHelper2 extends SQLiteOpenHelper {
static final String DATABASE_NAME = "tasks.db";
static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "tasks";
public static final String DATABASE_CREATE= "create table tasks" + "( " + "id" + " integer primary key autoincrement, " + " poption TEXT NULL UNIQUE, " + " environ TEXT NULL UNIQUE ); ";
public static final String DELETE_TABLE = "DROP TABLE IF EXITS" + TABLE_NAME;
private static final String COLUMN_ID = "id";
private static final String COLUMN_OPTION = "poption";
private static final String COLUMN_ENVIRON = "environ";
//Constructor
public DatabaseHelper2 (Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db1) {
db1.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db1, int oldVersion, int newVersion) {
//Drop older table if existed
db1.execSQL(DELETE_TABLE);
//Create tables again
onCreate(db1);
}
public void insertValue(String poption) {
ContentValues values = new ContentValues();
// Open the database for writing
SQLiteDatabase db1 = this.getWritableDatabase();
// Start the transaction.
db1.beginTransaction();
try
{
values = new ContentValues();
values.put("poption", poption);
// Insert Row
db1.insert(TABLE_NAME, null, values);
// Insert into database successfully.
db1.setTransactionSuccessful();
} catch (Exception e) {
e.printStackTrace();
} finally {
{
db1.endTransaction();
// End the transaction.
db1.close();
// Close database
}
}
}
public void insertValue1(String environ) {
ContentValues values1 = new ContentValues();
// Open the database for writing
SQLiteDatabase db1 = this.getWritableDatabase();
// Start the transaction.
db1.beginTransaction();
try
{
values1 = new ContentValues();
values1.put("environ", environ);
// Insert Row
db1.insert(TABLE_NAME, null, values1);
// Insert into database successfully.
db1.setTransactionSuccessful();
} catch (Exception e) {
e.printStackTrace();
} finally {
{
db1.endTransaction();
// End the transaction.
db1.close();
// Close database
}
}
}
public ArrayList<String> getAllValues(){
ArrayList<String> list=new ArrayList<String>();
// Open the database for reading
SQLiteDatabase db1 = this.getReadableDatabase();
// Start the transaction.
db1.beginTransaction();
try
{
String selectQuery = "SELECT * FROM "+ TABLE_NAME;
Cursor cursor = db1.rawQuery(selectQuery, null);
if(cursor.getCount() >0)
{
while (cursor.moveToNext()) {
// Add province name to arraylist
String poption= cursor.getString(cursor.getColumnIndex("poption"));
list.add(poption);
}
}
db1.setTransactionSuccessful();
}
catch (SQLiteException e)
{
e.printStackTrace();
}
finally
{
db1.endTransaction();
// End the transaction.
db1.close();
// Close database
}
return list;
}
public List<String> getAllInfo(){
List<String> list1=new ArrayList<String>();
// Open the database for reading
SQLiteDatabase db1 = this.getReadableDatabase();
// Start the transaction.
db1.beginTransaction();
try
{
String selectQuery = "SELECT * FROM "+ TABLE_NAME;
Cursor cursor = db1.rawQuery(selectQuery, null);
if(cursor.getCount() > 1)
{
while (cursor.moveToNext()) {
// Add province name to arraylist
String environ= cursor.getString(cursor.getColumnIndex("environ"));
list1.add(environ);
}
}
db1.setTransactionSuccessful();
}
catch (SQLiteException e)
{
e.printStackTrace();
}
finally
{
db1.endTransaction();
// End the transaction.
db1.close();
// Close database
}
return list1;
}
}
2)Java class:
package com.development.user.onebajacrops;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import java.util.ArrayList;
import java.util.List;
public class TaskScreenActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_task_screen);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
// Create DataHelper object and insert some sample data
DatabaseHelper2 databaseHelper1 = new DatabaseHelper2(this);
databaseHelper1.insertValue("Vegetables");
databaseHelper1.insertValue("Flowers");
databaseHelper1.insertValue1("Indoor");
databaseHelper1.insertValue1("Container");
databaseHelper1.insertValue1("Outdoor");
// Get sample data from the database and display them in the spinner
ArrayList<String> list = databaseHelper1.getAllValues();
List<String> list1 = databaseHelper1.getAllInfo();
Spinner sp = (Spinner)findViewById(R.id.spinnerPlant);
Spinner sp1 = (Spinner)findViewById(R.id.spinnerEnvi);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_layout,R.id.txt, list);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.spinner_layout,R.id.txt, list1);
sp.setAdapter(adapter);
sp1.setAdapter(adapter1);
}
}
1)数据库助手
包com.development.user.onebajacrops;
导入android.content.ContentValues;
导入android.content.Context;
导入android.database.Cursor;
导入android.database.sqlite.SQLiteDatabase;
导入android.database.sqlite.SQLiteException;
导入android.database.sqlite.SQLiteOpenHelper;
导入java.util.ArrayList;
导入java.util.List;
/**
*由用户于2017年10月22日创建。
*/
公共类DatabaseHelper2扩展了SQLiteOpenHelper{
静态最终字符串数据库\u NAME=“tasks.db”;
静态最终int数据库_版本=1;
私有静态最终字符串表\u NAME=“tasks”;
公共静态最终字符串数据库_CREATE=“CREATE table tasks”+”(“+”id“+”integer主键自动递增“+”poption TEXT NULL UNIQUE“+”environ TEXT NULL UNIQUE”);
公共静态最终字符串DELETE_TABLE=“退出时删除表”+表名称;
私有静态最终字符串列\u ID=“ID”;
私有静态最终字符串列\u OPTION=“poption”;
私有静态最终字符串列_ENVIRON=“ENVIRON”;
//建造师
公共数据库助手2(上下文){
super(上下文、数据库名称、null、数据库版本);
}
@凌驾
public void onCreate(SQLiteDatabase db1){
db1.execSQL(创建数据库);
}
@凌驾
public void onUpgrade(SQLiteDatabase db1,int-oldVersion,int-newVersion){
//删除旧表(如果存在)
db1.execSQL(DELETE_TABLE);
//再次创建表
onCreate(db1);
}
公共void insertValue(字符串选项){
ContentValues=新的ContentValues();
//打开数据库进行编写
SQLiteDatabase db1=this.getWritableDatabase();
//启动事务。
db1.beginTransaction();
尝试
{
values=新的ContentValues();
值。put(“poption”,poption);
//插入行
db1.insert(表名称,null,值);
//成功插入数据库。
db1.setTransactionSuccessful();
}捕获(例外e){
e、 printStackTrace();
}最后{
{
db1.endTransaction();
//结束交易。
db1.close();
//关闭数据库
}
}
}
public void insertValue1(字符串环境){
ContentValues1=新的ContentValues();
//打开数据库进行编写
SQLiteDatabase db1=this.getWritableDatabase();
//启动事务。
db1.beginTransaction();
尝试
{
values1=新的ContentValues();
价值1.出售(“环境”,环境);
//插入行
db1.insert(表名称,null,值1);
//成功插入数据库。
db1.setTransactionSuccessful();
}捕获(例外e){
e、 printStackTrace();
}最后{
{
db1.endTransaction();
//结束交易。
db1.close();
//关闭数据库
}
}
}
公共ArrayList getAllValues(){
ArrayList=新建ArrayList();
//打开数据库进行读取
SQLiteDatabase db1=this.getReadableDatabase();
//启动事务。
db1.beginTransaction();
尝试
{
String selectQuery=“SELECT*FROM”+表格名称;
Cursor Cursor=db1.rawQuery(selectQuery,null);
if(cursor.getCount()>0)
{
while(cursor.moveToNext()){
//将省名称添加到arraylist
字符串poption=cursor.getString(cursor.getColumnIndex(“poption”);
列表。添加(poption);
}
}
db1.setTransactionSuccessful();
}
catch(sqlitee异常)
{
e、 printStackTrace();
}
最后
{
db1.endTransaction();
//结束交易。
db1.close();
//关闭数据库
}
退货清单;
}
公共列表getAllInfo(){
List list1=新的ArrayList();
//打开数据库进行读取
SQLiteDatabase db1=this.getReadableDatabase();
//启动事务。
db1.beginTransaction();
尝试
{
String selectQuery=“SELECT*FROM”+表格名称;
Cursor Cursor=db1.rawQuery(selectQuery,null);
if(cursor.getCount()>1)
{
while(cursor.moveToNext()){
//将省名称添加到arraylist
String environ=cursor.getString(cursor.getColumnIndex(“environ”);
清单1.添加(环境);
}
}
db1.setTransactionSuccessful();
}
catch(sqlitee异常)
{
e、 printStackTrace();
}
最后
{
db1.endTransaction();
//结束交易。
db1.close();
//关闭数据库
}
返回列表1;
}
}
2) Java类:
包com.development.user.onebajacrops;
导入android.os.Bundle;
导入android.support.design.widget.FloatingActionButton;
导入android.support.design.widget.Snackbar;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.Toolbar;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.Spinner