简体   繁体   中英

What is the meaning of new String[]{“0”}?

I found a method call OrdersCursorToList() take a cursor as parameter and return an object of a Model call Orders

 List<Orders> Orderss = OrdersCursorToList(mContext.getContentResolver().query(CONTENT_URI,
            null,
            COLUMN_ORDER_IS_CONFIRMED + " = ? AND "
          + COLUMN_USER_ID + " = ?",
            new String[]{"0"},
            COLUMN_ORDER_ID));

So what is the meaning of new String []{"0"}

new String []{"0"}是具有一个元素的String-array-literal: ["0"]

In a query, you can add several conditions so you get only the rows that you want. In your sample, you have:

COLUMN_USER_ID + " = ?",
new String[]{"0"},

query method will replace every "?" character by the selection parameter provided in the next string array (in your sample, the string array is new String[] {"0"} ).

Note that your filtering the rows only by COLUMN_USER_ID . Since you have only one select condition, you must provide an array with one element only. That is the meaning of new String[] {"0"} .

That line is creating and instantiating an array with one element only. That element is "0" . It's the same:

String [] selectionArgument = new String[1];
selectionArgument[0] = "0";

That parameter is a String array because you can have as many conditions as you want... So, if you want to use two conditions, you have to pass an string array with two elements. Something like:

COLUMN_USER_ID + " = ?" + " AND " + COLUMN_USER_AGE + " > ? ",
new String[]{"0", "18"},

Note on this sample that you have two "?" characters as selection criteria. So, the string array also must be an array with two elements: "0" and "18" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM