Android 对特定行的数据库查询

Android 对特定行的数据库查询,android,Android,我正在构建一个血库数据库应用程序。在那里我会带上一些血型信息。我将血型存储为“整数非空”,其中1、2、…、7表示A+、…、AB-血型。但是,当我试图根据用户从微调器中选择血型来查询listview时,会出现错误(下面给出了堆栈跟踪)。在数据库中插入数据做得很好,但没有给出错误 main活动相关代码- private void displayDatabaseInfo(){ String[] projection = { DonorEntry.COL

我正在构建一个血库数据库应用程序。在那里我会带上一些血型信息。我将血型存储为“整数非空”,其中1、2、…、7表示A+、…、AB-血型。但是,当我试图根据用户从微调器中选择血型来查询listview时,会出现错误(下面给出了堆栈跟踪)。在数据库中插入数据做得很好,但没有给出错误

main活动
相关代码-

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>
召唤

String [] selectionArgs = new String[] {String.valueOf(mBloodType)};
而不是

String [] selectionArgs = new String[] {getString(mBloodType)};

这里为你修好了一切。有一些问题

  • getString
    正在崩溃。这不是您要用来解析任何整数的方法,它用于获取
    资源
    ,并传递该资源的
    id
  • CursorAdapter
    needs
    \u id
    列位于
    cursor
    中,当您通过
    projection
    数组而没有
    \u id
    列时,您的适配器崩溃。所以我删除了投影,现在你将得到所有的列
  • 虽然这在某种程度上起了作用,但当列值类型为
    INTEGER
    时,不应该使用
    getString
    ,因此我将其更改为
    getInt
  • 您直接将列值分配给显示整数的
    TextView
    ,因此我在
    MainActivity
    中创建了一个方法来获取血型的实际值
  • main活动
    更改-

    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";
    
        }
    }
    
    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);
    
    DonorCursorAdapter
    更改-

    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";
    
        }
    }
    
    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);
    

    它崩溃是因为cursor.getString(bloodTypeColumnIndex)我相信。您正在数据库中存储
    Integer
    值,但试图从该列索引中获取字符串。因此,请尝试将其更改为
    cursor.getInt()
    ,然后使用
    String.valueOf()
    将int解析为String。@Ranjan我进行了修改,但在int donorBloodType=cursor.getInt(bloodTypeColumnIndex)中无效;bloodTypeTextView.setText(String.valueOf(donorBloodType));