如何为游标列创建条件语句

时间:2018-07-11 04:27:56

标签: java android sql sqlite cursor

我有一个游标,它使用JOIN方法从两个不同的表中读取。当从第二个表中读取的列为null时,如何创建条件,光标不会产生错误?

//create a database instance to get the current cursor
    DatabaseHelper db = new DatabaseHelper(getContext());
    SQLiteDatabase database = db.getReadableDatabase();

    //set empty activity
    ListView listView = rootView.findViewById(R.id.display_view);
    currentCursor = database.rawQuery("SELECT " + StoreEntry.TABLE_NAME_STORE +"."+ StoreEntry._ID + ", "
                + StoreEntry.COLUMN_PRODUCT_NAME + ", "
                + StoreEntry.COLUMN_PRODUCT_MEASURE + ", "
                + StoreEntry.COLUMN_PRODUCT_WEIGHT + ", "
                + IngredientCost.COLUMN_COST_100 + " AS COST " + " FROM " + StoreEntry.TABLE_NAME_STORE + " INNER JOIN "
            + IngredientCost.TABLE_NAME_INGREDIENT_COST + " ON "
            + StoreEntry.TABLE_NAME_STORE +"." + StoreEntry.COLUMN_PRODUCT_NAME + "="
            + IngredientCost.TABLE_NAME_INGREDIENT_COST +"."+ IngredientCost.COLUMN_ITEM_NAME , null );
    View emptyView = rootView.findViewById(R.id.empty_view);
    listView.setEmptyView(emptyView);
    //create a new cursor adapter
    mCursorAdapter = new ItemCursorAdapter(getActivity(),currentCursor);

这是我的游标适配器,它将填充列,在每个表中分别创建数据。

    @Override
public void bindView(final View view, final Context context, Cursor cursor) {

    //find which fields should be populated
    TextView itemName = (TextView) view.findViewById(R.id.item_name);
    final TextView itemWeight = view.findViewById(R.id.item_weight);
    TextView itemCost = view.findViewById(R.id.item_current_cost);
    DatabaseUtils.dumpCurrentRow(cursor);
    if(cursor.moveToFirst()) {
        //find the columns of pets we're interested in
        final String mItemName = cursor.getString(cursor.getColumnIndex(StoreEntry.COLUMN_PRODUCT_NAME));
        final String mItemWeight = cursor.getString(cursor.getColumnIndex(StoreEntry.COLUMN_PRODUCT_WEIGHT));
        final String mItemMeasure = cursor.getString(cursor.getColumnIndex(StoreEntry.COLUMN_PRODUCT_MEASURE));
        if(cursor.getFloat(cursor.getColumnIndex("COST")) != 0.0f) {
            final float mItemCost = cursor.getFloat(cursor.getColumnIndex("COST"));
            itemCost.setText(Float.toString(mItemCost));
        }
        //get current item id
        int currentId = cursor.getInt(cursor.getColumnIndex(StoreEntry._ID));
        //Create the content Uri for the current Id
        final Uri contentUri = Uri.withAppendedPath(StoreEntry.CONTENT_URI, Integer.toString(currentId));

        //populate fields item fragment
        itemName.setText(mItemName);
        itemWeight.setText(mItemWeight + " " + mItemMeasure);

    }
}

0 个答案:

没有答案