'我试图创建一个依赖spinner与数据从sqlite的android studio。但spinner不显示任何内容的预期。前缀是假设缩小选择的Lotno和最后显示种植园_名称基于数据库SmartSawit.db。请帮助:〉(it been days)
Spinner spinnerPrefix, spinnerLotno, spinnerPlantation_name;
Context context;
database Database;
String prefixValue, lotnoValue, nameValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_daftar_pokok);
spinnerPrefix = findViewById(R.id.spinnerPrefix);
spinnerLotno = findViewById(R.id.spinnerLotno);
spinnerPlantation_name = findViewById(R.id.spinnerPlantation_name);
context = this;
Database = new database(this, "SmartSawit.db", 1);
try {
Database.CheckDB();
fillSpinner(context,spinnerPrefix, "registered_plantation", "prefix", "");
spinnerPrefix.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
prefixValue = parent.getItemAtPosition(position).toString();
fillSpinner(context,spinnerLotno,"registered_plantation","lotno","where prefix = '"+prefixValue+"'");
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinnerLotno.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
lotnoValue = parent.getItemAtPosition(position).toString();
fillSpinner(context,spinnerPlantation_name,"registered_plantation","plantation_name","where prefix = '"+prefixValue+"' and lotno ='"+lotnoValue+"'");
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}catch(Exception e){
e.printStackTrace();
}
}
@SuppressLint("Range")
private void fillSpinner (Context context, Spinner mSpinner,String table, String column, String where) {
SQLiteDatabase db = Database.OpenDatabase("registered_plantation.db");
ArrayList<String> mArray = new ArrayList<>();
Cursor cursor = db.rawQuery("Select distinct "+column+" from "+table+""+where, null);
while (cursor.moveToNext()){
mArray.add(cursor.getString(cursor.getColumnIndex(column)));
}
cursor.close();
db.close();
ArrayAdapter mAdapter = new ArrayAdapter(context, android.R.layout.simple_spinner_item,mArray);
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(mAdapter);
}
}
my databasehelper code:
private Context context;
private static final String DATABASE_NAME = "SmartSawit.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "registered_plantation";
//private static final String COLUMN_NO = "_no";
private static final String COLUMN_NAME = "plantation_name";
private static final String COLUMN_PREFIX = "prefix";
private static final String COLUMN_LOT_NUMBER = "lotno";
String DbPath;
Context mcontext;
String DbName;
public database(Context context, String name, int version) {
super(context, DATABASE_NAME, null, version);
this.context = context;
this.mcontext = context;
this.DbName = DATABASE_NAME;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
this.DbPath = context.getFilesDir() + "/database/";
}
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_NAME +
" (" + COLUMN_NAME + " TEXT PRIMARY KEY, " +
COLUMN_PREFIX + " TEXT, " +
COLUMN_LOT_NUMBER + " INTEGER);";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void CheckDB(){
SQLiteDatabase checkDB = null;
String filePath = DbPath + DbName;
File file = new File(filePath);
if(file.isFile() && file.exists()){
Toast.makeText(mcontext, "already exist", Toast.LENGTH_SHORT).show();
} else {
CopyDatabase();
}
}
private void CopyDatabase(){
try {
InputStream ios = mcontext.getAssets().open(DbName);
File directory = new File(DbPath);
if(!directory.exists()){
directory.mkdirs();
}
OutputStream os = new FileOutputStream(DbPath + DbName);
byte[] buffer = new byte[1024];
int len;
while((len = ios.read(buffer)) >0) {
os.write(buffer,0, len);
}
os.flush();
ios.close();
os.close();
Log.d("CopyDb", "Databse Copied");
} catch (Exception e) {
e.printStackTrace();
}
}
void addLadang (String prefix, String namaLadang, int lotNo){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_PREFIX, prefix );
cv.put(COLUMN_NAME, namaLadang );
cv.put(COLUMN_LOT_NUMBER, lotNo );
long result = db.insert(TABLE_NAME,null, cv);
if (result == -1){
Toast.makeText(context, "FAILED", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(context, "SUCCESSFULLY SAVED", Toast.LENGTH_SHORT).show();
}
}
Cursor readAllData(){
String query = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
if(db != null){
cursor = db.rawQuery(query, null);
}
return cursor;
}
public SQLiteDatabase OpenDatabase(String dbName){
String filePath = DbPath + dbName;
return SQLiteDatabase.openDatabase(filePath,null,0);
}
}
1条答案
按热度按时间cwtwac6a1#
您有许多问题(实际上与纺纱机无关)。
首先,您试图打开一个不存在的数据库。也就是说,您在
fillSpinner
方法中使用的是表名,而不是数据库名,方法是:-即数据库名称为SmartSawit. db未注册种植园. db。
以下修复了该问题(请参阅嵌入的注解):-
然后,在
fillSpinner
方法中,WHERE子句中再次出现空格缺失的问题(使用修复,再次参见注解):-应用上述修正,并使用一个布局,其中有3个旋转器并排,不同颜色的背景和过高。并与数据库中的以下数据(在资产文件夹名称SmartSawit.db):-
第一次运行时:-
单击第一个微调器
单击B并
等等。
如果您使用原始代码检查日志,即使它没有崩溃,您也可以发现问题,例如日志将包括:-
因此,尽管使用设备资源管理器成功完成了复制,但您仍会遇到以下情况:-
因此,很容易看出registered_plantation. db不在预期位置,而SmartSwait. db在预期位置。
简而言之,如果使用try/catch,但未按预期工作,则检查日志,以了解您希望在日志中看到的内容(例如,查找已复制的数据库将很容易看到未按预期工作,因为错误行紧跟在该消息之后)。