英文:
Layout shows only CheckBox, does not show the ImageView etc
问题
我已经添加了布局和类别。我遇到的问题是,尽管我在布局中指定了它,但除了复选框之外,我看不到ImageView和TextViews。布局只显示复选框,不显示ImageView,TextView和第二个TextView!
项目页面CheckBox点击:
![项目页面CheckBox点击][1]
模拟器上的项目页面:
![模拟器上的项目页面][2]
AndroidStudio中的项目页面:
![AndroidStudio中的项目页面][3]
Drink Activity Layout是
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hfad.starbuzz.DrinkActivity" >
<ImageView
android:id="@+id/photo"
android:layout_width="190dp"
android:layout_height="190dp"/>
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<CheckBox android:id="@+id/favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/favorite"
android:onClick="onFavoriteClicked" />
</LinearLayout>
Drink Activity:
package com.hfad.starbuzz;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Objects;
public class DrinkActivity extends Activity {
public static final String EXTRA_DRINKID = "drinkId";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink);
//从Intent中获取饮料
int drinkId = (Integer) Objects.requireNonNull(getIntent().getExtras()).getInt(EXTRA_DRINKID);
//创建一个游标
SQLiteOpenHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this);
try {
SQLiteDatabase db = starbuzzDatabaseHelper.getReadableDatabase();
Cursor cursor = db.query("DRINK",
new String[]{"NAME","DESCRIPTION","IMAGE_RESOURCE_ID","FAVORITE"},
"_id = ?",
new String[]{Integer.toString(drinkId)},null,null,null);
//移动游标到第一个条目
if (cursor.moveToFirst()) {
//从游标获取饮料详情
String nameText = cursor.getString(0);
String descriptionText = cursor.getString(1);
int photoId = cursor.getInt(2);
boolean isFavorite = (cursor.getInt(3) == 1);
//填充饮料的名称
TextView name = (TextView) findViewById(R.id.name);
name.setText(nameText);
//填充饮料的描述
TextView description = (TextView) findViewById(R.id.description);
description.setText(descriptionText);
//填充饮料的图片
ImageView photo = (ImageView) findViewById(R.id.photo);
photo.setImageResource(photoId);
photo.setContentDescription(nameText);
//填充您最喜欢的饮料的标志
CheckBox favorite = (CheckBox)findViewById(R.id.favorite);
favorite.setChecked(isFavorite);
}
cursor.close();
db.close();
} catch (SQLiteException e) {
Toast toast = Toast.makeText(this,
"不可用",
Toast.LENGTH_SHORT);
toast.show();
}
}
//单击复选框时刷新数据库
public void onFavoriteClicked(View view){
int drinkId = (Integer) Objects.requireNonNull(getIntent().getExtras()).getInt(EXTRA_DRINKID);
//获取复选框的值
CheckBox favorite = (CheckBox) findViewById(R.id.favorite);
ContentValues drinkValues = new ContentValues();
drinkValues.put("FAVORITE", favorite.isChecked());
//检索数据库链接并更新FAVORITE列
SQLiteOpenHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this);
try {
SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase();
db.update("DRINK",
drinkValues,
"_id = ?",
new String[] {Integer.toString(drinkId)});
db.close();
} catch(SQLiteException e) {
Toast toast = Toast.makeText(this, "数据库不可用", Toast.LENGTH_SHORT);
toast.show();
}
}
}
StarbuzzDatabaseHelper:
package com.hfad.starbuzz;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class StarbuzzDatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "starbuzz"; // 数据库名称
// 增加版本号意味着SQLite助手将知道要更新数据库。
private static final int DB_VERSION = 12; // 数据库版本和版本号。
StarbuzzDatabaseHelper(Context context){
super(context, DB_NAME, null, DB_VERSION);
}
@Override
// 当数据库首次创建时调用onCreate()方法;
// 我们用它来创建表格和插入数据
public void onCreate(SQLiteDatabase db){
updateMyDatabase(db, 0, DB_VERSION);
}
// 通过传递的参数从onUpgrade()调用updateMyDatabase()方法。
@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, "
<details>
<summary>英文:</summary>
I have added layout and classes below. The problem I am having is that although I have specified it in the layout, I cannot see the ImageView and TextViews other than checkbox. Layout shows only **CheckBox**, does not show the **ImageView**, the **TextView** and **second TextView**!
Project page CheckBox clicked:
![project page CheckBox clicked][1]
Project page on emulator:
![project page on emulator][2]
Project page in AndroidStudio:
![project page in AndroidStudio][3]
Drink Activity Layout is
**XML:**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hfad.starbuzz.DrinkActivity" >
<ImageView
android:id="@+id/photo"
android:layout_width="190dp"
android:layout_height="190dp"/>
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<CheckBox android:id="@+id/favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/favorite"
android:onClick="onFavoriteClicked" />
</LinearLayout>
**Drink Activity:**
package com.hfad.starbuzz;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Objects;
public class DrinkActivity extends Activity {
public static final String EXTRA_DRINKID = "drinkId";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink);
//Getting a drink from an intent
int drinkId = (Integer) Objects.requireNonNull(getIntent().getExtras()).getInt(EXTRA_DRINKID);
//Creating a cursor
SQLiteOpenHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this);
try {
SQLiteDatabase db = starbuzzDatabaseHelper.getReadableDatabase();
Cursor cursor = db.query("DRINK",
new String[]{"NAME","DESCRIPTION","IMAGE_RESOURCE_ID","FAVORITE"},
"_id = ?",
new String[]{Integer.toString(drinkId)},null,null,null);
//Move to the first entry in the cursor
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);
boolean isFavorite = (cursor.getInt(3) == 1);
//Filling in the name of the drink
TextView name = (TextView) findViewById(R.id.name);
name.setText(nameText);
//Filling in the description of the drink
TextView description = (TextView) findViewById(R.id.description);
description.setText(descriptionText);
//Filling the drink image
ImageView photo = (ImageView) findViewById(R.id.photo);
photo.setImageResource(photoId);
photo.setContentDescription(nameText);
//Filling the Flag for Your Favorite Drink
CheckBox favorite = (CheckBox)findViewById(R.id.favorite);
favorite.setChecked(isFavorite);
}
cursor.close();
db.close();
} catch (SQLiteException e) {
Toast toast = Toast.makeText(this,
" unavailable",
Toast.LENGTH_SHORT);
toast.show();
}
}
//Database refresh on click of a checkbox
public void onFavoriteClicked(View view){
int drinkId = (Integer) Objects.requireNonNull(getIntent().getExtras()).getInt(EXTRA_DRINKID);
//Getting the value of a checkbox
CheckBox favorite = (CheckBox) findViewById(R.id.favorite);
ContentValues drinkValues = new ContentValues();
drinkValues.put("FAVORITE", favorite.isChecked());
//Retrieving a database link and updating the FAVORITE column
SQLiteOpenHelper starbuzzDatabaseHelper = new StarbuzzDatabaseHelper(this);
try {
SQLiteDatabase db = starbuzzDatabaseHelper.getWritableDatabase();
db.update("DRINK",
drinkValues,
"_id = ?",
new String[] {Integer.toString(drinkId)});
db.close();
} catch(SQLiteException e) {
Toast toast = Toast.makeText(this, "Database unavailable", Toast.LENGTH_SHORT);
toast.show();
}
}
}
**StarbuzzDatabaseHelper:**
package com.hfad.starbuzz;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class StarbuzzDatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "starbuzz"; // Database name
//Increasing the version number means that the SQLite Assistant will know to update the database.
private static final int DB_VERSION = 12; // Database version and version number.
StarbuzzDatabaseHelper(Context context){
super(context, DB_NAME, null, DB_VERSION);
}
@Override
//The onCreate () method is called when the database is first created;
//We use it to create a table and insert data
public void onCreate(SQLiteDatabase db){
updateMyDatabase(db, 0, DB_VERSION);
}
//The updateMyDatabase () method is called from onUpgrade () with parameters passed.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
updateMyDatabase(db, oldVersion, newVersion);
}
//It is necessary to insert the data of several drinks,
//so we created a separate method for inserting.
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);
}
//This code is executed if the user already has version 1 of the database installed
if (oldVersion >= 2) {
}
//Code for adding a new column
}
}
[1]: https://i.stack.imgur.com/YLieU.png
[2]: https://i.stack.imgur.com/4GWfV.png
[3]: https://i.stack.imgur.com/p00ej.png
</details>
# 答案1
**得分**: 1
你的查询是否返回任何数据?您可能需要检查cursor.getCount()的输出。
另外,如果您进入设备的开发者选项,您可以看到一个名为"显示布局边界"的选项。它会显示您的视图元素在屏幕上的位置。
<details>
<summary>英文:</summary>
Is your query returning any data? You might want to check the output of cursor.getCount().
Also if you go into Developer Options of your device you can see an option called "Show Layout Bounds". It will show you where your view elements are on the screen.
</details>
# 答案2
**得分**: 0
I finally found a solution, in public class StarbuzzDatabaseHelper
```java
private static final int DB_VERSION = 12;
change to:
private static final int DB_VERSION = 2;
if (oldVersion >= 2) {
change to:
if (oldVersion < 2) {
db.execSQL("ALTER TABLE DRINK ADD COLUMN FAVORITE NUMERIC;");
}
<details>
<summary>英文:</summary>
I finally found a solution, in public class StarbuzzDatabaseHelper
''''
private static final int DB_VERSION = 12;
''''
change to: private static final int DB_VERSION = 2;
''''
if (oldVersion >= 2) {
}
''''
change to: if (oldVersion < 2) {
db.execSQL("ALTER TABLE DRINK ADD COLUMN FAVORITE NUMERIC;");
}
''''
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论