我是android studio的新用户,并且对列表有疑问。我将从数据库中提取信息,列出存储在数据库中的所有活动(类活动)(mapActivity,景点Activities等。在创建新的空活动类时,此类活动)。这应该在主要活动中列出。我希望你明白我的意思。我正在使用sqlite数据库
// DBhelper
public class DBHelper extends SQLiteOpenHelper {
private static final String DB_name = "Mydb";
private static final int DB_version = 1;
private String createActivityTypeQuery = "CREATE TABLE activity_type (_id INTEGER PRIMARY KEY AUTOINCREMENT, type TEXT)";
private String createpriceQuery = "CREATE TABLE price (_id INTEGER PRIMARY KEY AUTOINCREMENT, price TEXT)";
private String createActivityQuery = "CREATE TABLE activity (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "name TEXT,"
+ "description TEXT,"
+ "type INTEGER,"
+ "homePage TEXT,"
+ "price INTEGER,"
+ "outdoor NUMERIC,"
+ "FOREIGN KEY (type) REFERENCES activity_type(_id),"
+ "FOREIGN KEY (price) REFERENCES price(_id)"
+ ")";
public DBHelper(@Nullable Context context) {
super(context, DB_name, null, DB_version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(createActivityTypeQuery);
db.execSQL(createpriceQuery);
db.execSQL(createActivitytQuery);
insertActivityType(db, "Amusement park");
insertActivityType(db, "Waterpark");
insertprice(db, "Low");
insertprice(db, "Medium");
insertprice(db, "High");
insertActivity(db, "Thorpe park", "Amusement park in London, United Kingdom. Carousels and roller coaster plus much more", 1, "www.thorpark.uk", 3, true);
insertActivity(db, "Lee Water Centre", "Lee Water Centre in London, United Kindgom. Lots of water and fun", 2, "www.LeeWaterCentre.uk", 2, true);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private static void insertActivityType(SQLiteDatabase db, String type) {
ContentValues activityValues = new ContentValues();
aktivitetValues.put("type", type);
db.insert("activity_type", null, activityValues);
}
private static void insertprice(SQLiteDatabase db, String price) {
ContentValues priceValues = new ContentValues();
priceValues.put("price", price);
db.insert("price", null, priceValues);
}
private static void insertActivity(SQLiteDatabase db, String name, String
description, int type, String webPage, int price, boolean outdoor) {
ContentValues activityValues = new ContentValues();
activitytValues.put("name", name);
activitytValues.put("description", description);
activitytValues.put("type", type);
activityValues.put("webPage", webPage);
activitytValues.put("price", price);
activityValues.put("outdoor", outdoor);
db.insert("activity", null, activityValues);
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
private SQLiteDatabase db;
private Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView activitylist = findViewById(R.id.activitylist);
//Database
SQLiteOpenHelper dbHelper = new DBHelper(this);
try {
db = dbHelper.getReadableDatabase();
cursor = db.query("activity", new String[] {"_id", "name", "description"}, null, null, null, null, null);
SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, new String[]{"name", "description"}, new int[]{android.R.id.text1, android.R.id.text2 },0);
activitylist.setAdapter(listAdapter);
} catch(SQLiteException e) {
Toast dbToast = Toast.makeText(this, "Database Unavailable", Toast.LENGTH_SHORT);
dbToast.show();
}
} // end onCreate()
public void onDestroy() {
super.onDestroy();
cursor.close();
db.close();
}
}
答案 0 :(得分:0)
您出现的问题可能是由于活动表中没有数据,原因是根据活动表的创建SQL,没有名为 webPage 的列,相反,您可能希望将数据插入 homePage 列。
这是一个很好的示例,说明了为什么建议使用静态变量定义表名和列名,然后始终使用该变量来引用表/列。
解决方案的简短解决方法是使用:-
private static void insertActivity(SQLiteDatabase db, String name, String
description, int type, String webPage, int price, boolean outdoor) {
ContentValues activityValues = new ContentValues();
activitytValues.put("name", name);
activitytValues.put("description", description);
activitytValues.put("type", type);
activityValues.put("homePage", webPage); //<<<<<<<<<< CHANGED
activitytValues.put("price", price);
activityValues.put("outdoor", outdoor);
db.insert("activity", null, activityValues);
}
但是,更好的解决方案是按照:-
public class DBHelper extends SQLiteOpenHelper {
private static final String DB_name = "Mydb";
private static final int DB_version = 1;
private static TB_ACTIVITY = "activity"; // DEFINED constant variable for the TABLE NAME
private static ACTIVITY_COL_HOMEPAGE = "homePage"; //<<<<<<<<<< DEFINES constant variable for the COLUMN NAME
.......... other columns done likewise
private String createActivityTypeQuery = "CREATE TABLE activity_type (_id INTEGER PRIMARY KEY AUTOINCREMENT, type TEXT)";
private String createpriceQuery = "CREATE TABLE price (_id INTEGER PRIMARY KEY AUTOINCREMENT, price TEXT)";
private String createActivityQuery = "CREATE TABLE " + TB_ACTIVITY + "(_id INTEGER PRIMARY KEY AUTOINCREMENT, " //<<<<<<<<<< APPLIED constant variable for the TABLE NAME
+ "name TEXT,"
+ "description TEXT,"
+ "type INTEGER,"
+ ACTIVITY_COL_HOMEPAGE + " TEXT," //<<<<<<<<<< CHANGED TO USE constant
+ "price INTEGER,"
+ "outdoor NUMERIC,"
+ "FOREIGN KEY (type) REFERENCES activity_type(_id),"
+ "FOREIGN KEY (price) REFERENCES price(_id)"
+ ")";
public DBHelper(@Nullable Context context) {
super(context, DB_name, null, DB_version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(createActivityTypeQuery);
db.execSQL(createpriceQuery);
db.execSQL(createActivitytQuery);
insertActivityType(db, "Amusement park");
insertActivityType(db, "Waterpark");
insertprice(db, "Low");
insertprice(db, "Medium");
insertprice(db, "High");
insertActivity(db, "Thorpe park", "Amusement park in London, United Kingdom. Carousels and roller coaster plus much more", 1, "www.thorpark.uk", 3, true);
insertActivity(db, "Lee Water Centre", "Lee Water Centre in London, United Kindgom. Lots of water and fun", 2, "www.LeeWaterCentre.uk", 2, true);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private static void insertActivityType(SQLiteDatabase db, String type) {
ContentValues activityValues = new ContentValues();
aktivitetValues.put("type", type);
db.insert("activity_type", null, activityValues);
}
private static void insertprice(SQLiteDatabase db, String price) {
ContentValues priceValues = new ContentValues();
priceValues.put("price", price);
db.insert("price", null, priceValues);
}
private static void insertActivity(SQLiteDatabase db, String name, String
description, int type, String webPage, int price, boolean outdoor) {
ContentValues activityValues = new ContentValues();
activitytValues.put("name", name);
activitytValues.put("description", description);
activitytValues.put("type", type);
activityValues.put(ACTIVITY_COL_HOMEPAGE, webPage); //<<<<<<<<<<
activitytValues.put("price", price);
activityValues.put("outdoor", outdoor);
db.insert(TB_ACTIVITY, null, activityValues); //<<<<<<<<<<
}
}
//<<<<<<<<<<
的行cursor = db.query("activity", new String[] {"_id", "name", "description"}, null, null, null, null, null);
cursor = db.query(DBHelper.TB_ACTIVITY, new String[] {DBHelper.ACTIVITY_ID,DBHelper.NAME,DBHelper.DESCRIPTION}, null, null, null, null, null);