我试图使用Spinner来显示来自SQLite的数据。没有错误但旋转器没有显示任何内容。任何人都可以解决这个问题。以下是我的代码 CategoryBO.java
package com.giao.ordersystem;
/**
* Created by Long on 2/13/2016.
* /
public class CategoryBO {
private String categoryName;
public CategoryBO()
{
}
public CategoryBO(String categoryName)
{
this.categoryName=categoryName;
}
public String getCategoryName()
{
return this.categoryName;
}
public void setCategoryName(String categoryName)
{
this.categoryName=categoryName;
}
}
DishDAO.java
package com.giao.ordersystem;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import java.util.List;
import java.util.ArrayList;
/**
* Created by Long on 2/13/2016.
*/
public class DishDAO {
private static final String KEY_ROWID = "dishID";
private static final String CATEGORYID = "categoryID";
private static final String DISH_NAME = "dishName";
private static final String DISH_PRICE = "dishPrice";
private static final String DISH_DESCRIPTION = "dishDescription";
private static final String DISH_AVAILABILITY="availability";
private static final String DATABASE_TABLE = "Menu";
private static DatabaseHelper databaseHelper;
//
public final Context context;
private SQLiteDatabase database;
public DishDAO(Context context) {
this.context = context;
// TODO Auto-generated constructor stub
}
public DishDAO open() throws SQLException {
database = databaseHelper.getReadableDatabase();
return this;
}
public void close() throws SQLException {
this.close();
}
public long create(String categoryID, String dishName, String dishPrice, String dishDecription, String availability) throws SQLException {
ContentValues cv = new ContentValues();
cv.put(CATEGORYID, categoryID);
cv.put(DISH_NAME, dishName);
cv.put(DISH_PRICE, dishPrice);
cv.put(DISH_DESCRIPTION, dishDecription);
cv.put(DISH_AVAILABILITY,availability);
return database.insert(DATABASE_TABLE, null, cv);
}
public List<DishBO> list() throws SQLException {
String query = "SELECT * FROM Tables";
Cursor cur = database.rawQuery(query, null);
List<DishBO> list = new ArrayList<DishBO>();
int iRow = cur.getColumnIndex(KEY_ROWID);
for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
DishBO record = new DishBO(Integer.parseInt(cur.getString(0)), Integer.parseInt(cur.getString(1)), cur.getString(2)
, Integer.parseInt(cur.getString(3)), cur.getString(4),Integer.parseInt(cur.getString(5)));
list.add(record);
}
cur.close();
return list;
}
public boolean remove(int dishID) throws SQLException {
return database.delete(DATABASE_TABLE, KEY_ROWID + "=" + dishID, null) > 0;
}
public boolean removeAll() {
return database.delete(DATABASE_TABLE, null, null) > 0;
}
public long update(String dishID, String categoryID, String dishName, String dishPrice, String dishDecription) throws SQLException
{
ContentValues cv = new ContentValues();
if (categoryID != null)
cv.put(CATEGORYID, categoryID);
if (dishName != null)
cv.put(DISH_NAME, dishName);
if (dishPrice != null)
cv.put(DISH_PRICE, dishPrice);
if (dishDecription != null)
cv.put(DISH_DESCRIPTION, dishDecription);
return database.update(DATABASE_TABLE, cv, KEY_ROWID + "=?", new String[]{dishID});
}
}
Menu_Event.Java
package com.giao.ordersystem;
import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Long on 2/8/2016.
*/
public class Menu_Event {
private Context context;
private CategoryDAO categoryDAO;
public Menu_Event()
{}
public Menu_Event(Context context)
{
this.context=context;
categoryDAO = new CategoryDAO(context);
}
public void categoryListView_OnLoad(Spinner categorySpinner)
{
categoryDAO.open();
List<String> categoryList = new ArrayList<String>();
for(int i=0;i<categoryDAO.list().size();i++)
{
categoryList.add(categoryDAO.list().get(i).getCategoryName());
}
categoryDAO.close();
ArrayAdapter<String> categoryAdapter = new ArrayAdapter(context,android.R.layout.simple_spinner_item,categoryList);
categoryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
categorySpinner.setAdapter(categoryAdapter);
categoryAdapter.notifyDataSetChanged();
}
}
style.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
CategoryDAO
package com.giao.ordersystem;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import java.util.List;
import java.util.ArrayList;
/**
* Created by Long on 2/12/2016.
*/
public class CategoryDAO{
public static final String KEY_TableName="CategoryName";
private static final String DATABASE_TABLE="Category";
private static DatabaseHelper databaseHelper;
//
public final Context context;
private SQLiteDatabase database;
public CategoryDAO(Context context) {
this.context=context;
databaseHelper= new DatabaseHelper(context);
// TODO Auto-generated constructor stub
}
public CategoryDAO open() throws SQLException
{
database=databaseHelper.getReadableDatabase();
return this;
}
public void close() throws SQLException
{
databaseHelper.close();
}
public long create(String tableName) throws SQLException
{
ContentValues cv= new ContentValues();
cv.put(KEY_TableName, tableName);
return database.insert(DATABASE_TABLE,null ,cv);
}
public ArrayList<CategoryBO> list()
{
String query="SELECT * FROM Category";
Cursor cur=database.rawQuery(query,null);
ArrayList<CategoryBO> list = new ArrayList<CategoryBO>();
int iRow= cur.getColumnIndex(KEY_TableName);
for(cur.moveToFirst();!cur.isAfterLast();cur.moveToNext()) {
CategoryBO record = new CategoryBO(cur.getString(0));
list.add(record); }
cur.close();
return list;
}
public boolean remove(int categoryName)
{
return database.delete(DATABASE_TABLE, KEY_TableName + "=" + categoryName, null) > 0;
}
public boolean removeAll()
{
return database.delete(DATABASE_TABLE, null, null) > 0;
}
}