首页 > 解决方案 > 特定行的数据库查询

问题描述

我正在构建一个血库数据库应用程序。我将在哪里获取一些信息以及血型。我将血型存储为“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>

标签: android

解决方案


在这里为您解决了所有问题。有一些问题。

  1. getString正在崩溃。这不是您要用来解析任何整数的方法,它用于获取 aresource并且您传递id该资源的 an。
  2. CursorAdapter需要_id列在里面cursor,当你传递projection没有_id列的数组时,你的适配器崩溃了。所以我删除了投影,现在你会得到所有的列。
  3. getString尽管这以某种方式起作用,但是当列值类型为 时您不应该使用INTEGER,所以我将其更改为getInt.
  4. 您直接将列值分配给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);

推荐阅读