android - 特定行的数据库查询
问题描述
我正在构建一个血库数据库应用程序。我将在哪里获取一些信息以及血型。我将血型存储为“INTEGER NOT NULL”,其中 1、2、....、7 表示 A+、.....、AB- 血型。但是当我尝试根据用户从微调器中选择血型来查询列表视图时,我得到了错误(下面给出了堆栈跟踪)。在数据库中插入数据做得很好,不会出错。
MainActivity
相关代码 -
private void displayDatabaseInfo(){
String[] projection = {
DonorEntry.COLUMN_DONOR_NAME,
DonorEntry.COLUMN_DONOR_MOBILE,
DonorEntry.COLUMN_BLOOD_GROUP,
DonorEntry.COLUMN_DONATE_DATE };
String selection = DonorEntry.COLUMN_BLOOD_GROUP + "=?";
String [] selectionArgs = new String[] {getString(mBloodType)};
Cursor cursor = getContentResolver().query(DonorEntry.CONTENT_URI,
projection, selection, selectionArgs,null);
ListView listView = (ListView) findViewById(R.id.list);
DonorCursorAdapter adapter = new DonorCursorAdapter(this, cursor);
listView.setAdapter(adapter);
}
DonorCursorAdapter
相关代码 -
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Find individual views that we want to modify in the list item layout
TextView nameTextView = (TextView) view.findViewById(R.id.name);
TextView mobileTextView = (TextView) view.findViewById(R.id.mobileNo);
TextView bloodTypeTextView = (TextView) view.findViewById(R.id.bloodType);
TextView lastDonateTextView = (TextView) view.findViewById(R.id.donateDate);
// Find the columns of donor's attributes that we're interested in
int nameColumnIndex = cursor.getColumnIndex(DonorEntry.COLUMN_DONOR_NAME);
int mobileColumnIndex = cursor.getColumnIndex(DonorEntry.COLUMN_DONOR_MOBILE);
int bloodTypeColumnIndex = cursor.getColumnIndex(DonorEntry.COLUMN_BLOOD_GROUP);
int lastDonateColumnIndex = cursor.getColumnIndex(DonorEntry.COLUMN_DONATE_DATE);
// Read the donor attributes from the Cursor for the current pet
String donorName = cursor.getString(nameColumnIndex);
String donorMobileNo = cursor.getString(mobileColumnIndex);
String donorBloodType = cursor.getString(bloodTypeColumnIndex);
String donorLastDonate = cursor.getString(lastDonateColumnIndex);
// Update the TextViews with the attributes for the current pet
nameTextView.setText(donorName);
mobileTextView.setText(donorMobileNo);
bloodTypeTextView.setText(donorBloodType);
lastDonateTextView.setText(donorLastDonate);
}
堆栈跟踪
<code>
2019-03-02 17:25:37.140 28705-28705/com.sarkerjr.greenBlood E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sarkerjr.greenBlood, PID: 28705
android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.content.res.Resources.getText(Resources.java:339)
at android.content.res.Resources.getString(Resources.java:433)
at android.content.Context.getString(Context.java:556)
at com.sarkerjr.greenBlood.MainActivity.displayDatabaseInfo(MainActivity.java:121)
at com.sarkerjr.greenBlood.MainActivity.access$000(MainActivity.java:21)
at com.sarkerjr.greenBlood.MainActivity$2.onClick(MainActivity.java:56)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
</code>
解决方案
在这里为您解决了所有问题。有一些问题。
getString
正在崩溃。这不是您要用来解析任何整数的方法,它用于获取 aresource
并且您传递id
该资源的 an。CursorAdapter
需要_id
列在里面cursor
,当你传递projection
没有_id
列的数组时,你的适配器崩溃了。所以我删除了投影,现在你会得到所有的列。getString
尽管这以某种方式起作用,但是当列值类型为 时您不应该使用INTEGER
,所以我将其更改为getInt
.- 您直接将列值分配给
TextView
显示整数的列值,因此我创建了一个方法MainActivity
来获取血型的实际值。
MainActivity
变化 -
private void displayDatabaseInfo() {
String selection = DonorEntry.COLUMN_BLOOD_GROUP + "=?";
String[] selectionArgs = new String[]{String.valueOf(mBloodType)};
Cursor cursor = getContentResolver().query(DonorEntry.CONTENT_URI,
null, selection, selectionArgs, null);
ListView listView = findViewById(R.id.list);
DonorCursorAdapter adapter = new DonorCursorAdapter(this, cursor);
listView.setAdapter(adapter);
}
// Get value of readable blood type
public String getBloodTypeString(int bloodType) {
switch (bloodType) {
case A_Positive:
return getResources().getString(R.string.a_positive);
case A_Negative:
return getResources().getString(R.string.a_negative);
case B_Positive:
return getResources().getString(R.string.b_positive);
case B_Negative:
return getResources().getString(R.string.b_negative);
case O_Positive:
return getResources().getString(R.string.o_positive);
case O_Negative:
return getResources().getString(R.string.o_negative);
case AB_Positive:
return getResources().getString(R.string.ab_positive);
case AB_Negative:
return getResources().getString(R.string.ab_negative);
default:
return "UNKNOWN";
}
}
DonorCursorAdapter
变化 -
int donorBloodType = cursor.getInt(bloodTypeColumnIndex);
String donorBloodTypeString;
try {
donorBloodTypeString = ((MainActivity) context).getBloodTypeString(donorBloodType);
} catch (ClassCastException e) {
throw new ClassCastException("Trying to access MainActivity method from different context");
}
bloodTypeTextView.setText(donorBloodTypeString);
推荐阅读
- c# - 在 Xamarin.Forms 跨平台应用程序中使用 Amazon Cognito 用户池进行身份验证
- javascript - 我怎样才能使这个javascript代码更短?
- macos - 有没有办法不显示 SwiftUI 生命周期 macOS 应用程序的窗口?
- python - 通过 lambda 定义的函数列表的意外行为
- python - 过滤掉前一个条目的 30 个周期内的值
- flutter - 可以在 android 和 ios 的颤振应用程序上删除启动画面
- python - 创建包含 0 或 1 的 N 个元素加起来指定数字 X 的列表
- google-cloud-platform - 使用 org.apache.spark.sql.json 在 Spark sql 中创建临时视图
- blazor - Blazor - 如何为超出该数据类型最大值的值自定义 InputNumber 验证消息?
- python - 使用语音识别库中的监听函数时出错