Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

getColumnIndex("") doesn't recognized

That’s the code getColumnIndex() displays with a red color.

Like this image

I’m trying to add the orders that the user click to another activity so I used sqlite database

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public List<Order> getCarts() {
    SQLiteDatabase db = getReadableDatabase();
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    String[] sqlSelect = {"ProductName", "ProductId", "Quantity", "Price", "Discount"};
    String sqlTable = "OrderDetail";

    qb.setTables(sqlTable);
    Cursor c = qb.query(db, sqlSelect, null, null, null, null, null);
    final List<Order> result = new ArrayList<>();
    if (c.moveToFirst()) {
        do {
            result.add(new Order(c.getString(c.getColumnIndex()),
                    c.getString(c.getColumnIndex("ProductName")),
                    c.getString(c.getColumnIndex("Quantity")),
                    c.getString(c.getColumnIndex("Price")),
                    c.getString(c.getColumnIndex("Discount"))
                    ));
        }while (c.moveToNext());
    }
    return result;
}

Order Class:

public class Order {
    private  String ProductId;
    private String ProductName;
    private String Quantity;
    private String Price;
    private String Discount;

    public Order() {
    }

    public Order(String productId, String productName, String quantity, String price, String discount) {
        ProductId = productId;
        ProductName = productName;
        Quantity = quantity;
        Price = price;
        Discount = discount;
    }
}

>Solution :

First of all, you’re missing the column name in your first getColumnIndex().

Second, Android platform has added @IntRange annotations to these methods. getColumnIndex() returns an int from -1 up while getString() and similar require an int argument up from 0, and lint complains about that.

You can suppress it with @SuppressLint("range").

Or, since you’re defining the projection (column names) yourself, you can just hardcode the column indices as 0, 1, 2, … without using getColumnIndex() at all.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading