我需要检查微调框是否为空。但是,如果微调框内有数据,它仍然向我吐司,它是空的,不包含任何数据。主要问题是微调器已经包含来自数据库的数据,但是程序在再次打开应用程序后仍会继续添加新数据。
主要活动
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
databaseHelper db = new databaseHelper(getApplicationContext());
if (spinner.getCount() == 0){
Toast.makeText(MainActivity.this,"Spinner will be populated now",Toast.LENGTH_LONG).show();
db.insertData();
}else {
Toast.makeText(MainActivity.this,"Spinners is already populated",Toast.LENGTH_LONG).show();
}
loadData();
}
public void loadData(){
databaseHelper db = new databaseHelper(getApplicationContext());
List<String> labels = db.getAllLabels();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, labels);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String label = adapterView.getItemAtPosition(i).toString();
Toast.makeText(adapterView.getContext(),"Selected: "+label,Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
数据库
public databaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_2 + " TEXT," + COLUMN_3 + " TEXT," + COLUMN_4 + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertData() {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_2, "1");
contentValues.put(COLUMN_3, "2");
contentValues.put(COLUMN_4, "3");
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1) {
return false;
} else
return true;
}
public List<String> getAllLabels() {
List<String> labels = new ArrayList<String>();
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(1));
labels.add(cursor.getString(2));
labels.add(cursor.getString(3));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return labels;
}
}
答案 0 :(得分:0)
您不应该执行耗时的工作,例如从主应用程序线程中调用db。异步调用db,例如使用AsyncTask
。
您可以像这样获得微调项目:
int count = spinner.getAdapter() != null ? spinner.getAdapter().getCount() : 0;
答案 1 :(得分:0)
我已经检查过您的代码,并且始终在调用db.insertData(),因为开始时的Spinner getCount始终为0。
因此,我让您在MainActivity中进行了一些更改,并在Database类中创建了一个方法,以了解何时需要填充数据库。
MainActivity
public class MainActivity extends AppCompatActivity implements Spinner.OnItemSelectedListener{
private Spinner spinner;
private Database db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = findViewById(R.id.spinner);
db = new Database(getApplicationContext());
if(db.shouldPopulate()){
db.insertData();
}
loadData();
}
public void loadData(){
List<String> labels = db.getAllLabels();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, labels);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setOnItemSelectedListener(this);
spinner.setAdapter(dataAdapter);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String label = adapterView.getItemAtPosition(i).toString();
Toast.makeText(adapterView.getContext(),"Selected: "+label,Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
数据库 在这里,我将insertData方法的结果条件更改为插入结果。
我创建了一种方法来知道何时应该插入数据。
public boolean insertData() {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_2, "1");
contentValues.put(COLUMN_3, "2");
contentValues.put(COLUMN_4, "3");
return db.insert(TABLE_NAME, null, contentValues) != -1;
}
public boolean shouldPopulate(){
return getAllLabels().isEmpty();
}
我希望这段代码对您有所帮助。
重要
您应该将db调用包装在后台处理程序中,这对于Android应用程序中的性能和良好做法非常重要。
答案 2 :(得分:0)
您要将数据添加到微调器中的什么位置?您只需声明它即可:
spinner = (Spinner) findViewById(R.id.spinner);
您应该使用适配器将数据添加到Spinner:
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinner.getCount()
是什么意思?可能是必须引用adapter
吗?