gpt4 book ai didi

java - 无法从 SQLite 数据库填充我的 Activity (DrinkActivity)

转载 作者:行者123 更新时间:2023-11-30 09:59:14 24 4
gpt4 key购买 nike

我不知道我错过了什么,我无法从我的数据库中填充我的 DrinkActivity!

这是我的 SQLiteOpenHelper 类:

public class StarbuzzDatabaseHelper extends SQLiteOpenHelper {

private static final String DB_NAME = "starbuzz"; // the name of our database
private static final int DB_VERSION = 2; // the version of the database

StarbuzzDatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
updateMyDatabase(db, 0, DB_VERSION);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
updateMyDatabase(db, oldVersion, newVersion);
}

private static void insertDrink(SQLiteDatabase db, String name, String description,
int resourceId) {
ContentValues drinkValues = new ContentValues();
drinkValues.put("NAME", name);
drinkValues.put("DESCRIPTION", description);
drinkValues.put("IMAGE_RESOURCE_ID", resourceId);
db.insert("DRINK", null, drinkValues);
}

private void updateMyDatabase(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 1) {
db.execSQL("CREATE TABLE DRINK (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "NAME TEXT, "
+ "DESCRIPTION TEXT, "
+ "IMAGE_RESOURCE_ID INTEGER);");
insertDrink(db, "Latte", "Espresso and steamed milk", R.drawable.latte);
insertDrink(db, "Cappuccino", "Espresso, hot milk and steamed-milk foam",
R.drawable.cappuccino);
insertDrink(db, "Filter", "Our best drip coffee", R.drawable.filter);
}
if (oldVersion < 2) {
db.execSQL("ALTER TABLE DRINK ADD COLUMN FAVORITE NUMERIC;");
}
}
}

和导致 DrinkActivity 的其他 Activity (DrinkCategoryActivity) 在这里:

public class DrinkCategoryActivity extends AppCompatActivity {

private SQLiteDatabase db;
private Cursor cursor;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink_category);
SQLiteOpenHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this);
ListView listDrinks = findViewById(R.id.list_drinks);

try {
db = starbuzzDatabaseHelper.getReadableDatabase();
cursor = db.query("DRINK",
new String[]{"_id", "NAME"},
null, null, null, null, null);
SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
cursor,
new String[]{"NAME"},
new int[]{android.R.id.text1},
0);
listDrinks.setAdapter(listAdapter);
} catch(SQLiteException e) {
Toast toast = Toast.makeText(this, "Database unavailable", Toast.LENGTH_SHORT);
toast.show();
}

AdapterView.OnItemClickListener itemClickListener =
new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> listDrinks,
View itemView,
int position,
long id) {
//Pass the drink the user clicks on to DrinkActivity
Intent intent = new Intent(DrinkCategoryActivity.this,
DrinkActivity.class);
intent.putExtra(DrinkActivity.EXTRA_DRINK_ID, (int) id);
startActivity(intent);
}
};
listDrinks.setOnItemClickListener(itemClickListener);

}

@Override
protected void onDestroy() {
super.onDestroy();
cursor.close();
db.close();
}
}

最后是DrinkActivity:

public class DrinkActivity extends Activity {


public static final String EXTRA_DRINK_ID = "drinkId";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink);

int drinkId = Objects.requireNonNull(getIntent().getExtras()).getInt("EXTRA_DRINK_ID");

SQLiteOpenHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this);
try {
SQLiteDatabase db = starbuzzDatabaseHelper.getReadableDatabase();
Cursor cursor = db.query ("DRINK",
new String[] {"NAME", "DESCRIPTION", "IMAGE_RESOURCE_ID"},
"_id = ?",
new String[] {Integer.toString(drinkId)},
null, null,null);
if (cursor.moveToFirst()) {
//Get the drink details from the cursor
String nameText = cursor.getString(0);
String descriptionText = cursor.getString(1);
int photoId = cursor.getInt(2);

//Populate the drink name
TextView name = findViewById(R.id.name1);
name.setText(nameText);

//Populate the drink description
TextView description = findViewById(R.id.description1);
description.setText(descriptionText);

//Populate the drink image
ImageView photo = findViewById(R.id.photo1);
photo.setImageResource(photoId);
photo.setContentDescription(nameText);
}

cursor.close();
db.close();


} catch (SQLException e) {
Toast.makeText(this, "Database unavailable", Toast.LENGTH_LONG).show();
}
}
}

the DrinkActivity's final layout which is not populated at the end

最佳答案

首先 DATABASE_NAME 应该是 name.db,您缺少文件的扩展名。

引用: https://developer.android.com/training/data-storage/sqlite


另一个重要的事情是如何从 Intent 中检索数据,你不应该使用:

getIntent().getExtras()).getInt("EXTRA_DRINK_ID")

相反,一旦您有了 Intent ,就可以通过这种方式直接提取数据:

Intent intent = getIntent();
int extraId = intent.getExtraInt(DrinkActivity.EXTRA_DRINK_ID);

关于java - 无法从 SQLite 数据库填充我的 Activity (DrinkActivity),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59083704/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com