我正在构建一个血库数据库应用程序。我将在其中获取一些信息以及血型。我将血液类型存储为“ INTEGER NOT NULL”,其中1、2,...,7表示A +,.....,AB-血液类型。但是,当我尝试根据用户从微调器中选择血型来查询列表视图时,会出现错误(下面给出了堆栈跟踪)。在不给错误的情况下,在数据库中插入数据非常有用。
- echo -----BEGIN OPENSSH PRIVATE KEY----- >> deploy-key
- echo b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW >> deploy-key
- echo QyNTUxOQAAACByjJBGT21Arna/pirWVXQqGAr/aszqQ5HzvrA2MzVDZAAAAJiGKEEKhihB >> deploy-key
- echo CgAAAAtzc2gtZWQyNTUxOQAAACByjJBGT21Arna/pirWVXQqGAr/aszqQ5HzvrA2MzVDZA >> deploy-key
- echo AAAEAKbObQgJGXbrKQt4wdCy3YQfpVBqkT5RNEt2IYU5pv3HKMkEZPbUCudr+mKtZVdCoY >> deploy-key
- echo Cv9qzOpDkfO+sDYzNUNkAAAAFHN2ZW5AREVTS1RPUC0xTjVKUjRSAQ== >> deploy-key
- echo -----END OPENSSH PRIVATE KEY----- >> deploy-key
相关代码-
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);
}
答案 0 :(得分:2)
为您修复了所有问题。有一些问题。
getString
崩溃了。这不是您要用于解析任何整数的方法,而是用于获取resource
并传递该资源的id
的方法。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);
答案 1 :(得分:1)
致电
String [] selectionArgs = new String[] {String.valueOf(mBloodType)};
代替
String [] selectionArgs = new String[] {getString(mBloodType)};