List of usage examples for android.database MatrixCursor MatrixCursor
public MatrixCursor(String[] columnNames)
From source file:edu.purdue.safewalk.MapItems.MapJSONParser.java
/** Parses all the JSON files defined in the schemas and provides cursors to the data in each */ private void parseJSON() throws JSONException { // Create the fileCursorMap fileCursorMap = new HashMap<String, Cursor>(); // Iterate over each JSON file for (String file : jsonFiles) { // Get the resource identifier for the JSON file we're parsing String packageName = context.getPackageName(); int id = context.getResources().getIdentifier(packageName + ":raw/" + file, null, null); Log.d("JSONParsing", packageName + ":raw/" + file); // Create the buffered reader which will read in the json file BufferedReader bfr = new BufferedReader( new InputStreamReader(context.getResources().openRawResource(id))); // Read in the file line-by-line and store it in a JSONArray JSONArray array = null;//from w w w . j a v a 2 s . c om try { String line = null; StringBuilder sb = new StringBuilder(); while ((line = bfr.readLine()) != null) { sb.append(line); } array = new JSONArray(sb.toString()); } catch (IOException e) { e.printStackTrace(); } // Create the cursor which will store our data. // Its columns are defined by the Map provided by the schema MatrixCursor c = new MatrixCursor(fileSchemaMap.get(file)); // Populate the rows of the cursor by iterating over the json array for (int i = 0; i < array.length(); i++) { JSONObject entry = array.getJSONObject(i); // And populate the columns by iterating over the known schemas for each row Object[] row = new Object[fileSchemaMap.get(file).length]; for (int col = 0; col < fileSchemaMap.get(file).length; col++) { row[col] = entry.get(fileSchemaMap.get(file)[col]); } c.addRow(row); } // Map the filename string to the cursor we just created fileCursorMap.put(file, c); } }
From source file:de.schildbach.wallet.data.ExchangeRatesProvider.java
@Override public Cursor query(final Uri uri, final String[] projection, final String selection, final String[] selectionArgs, final String sortOrder) { final long now = System.currentTimeMillis(); final boolean offline = uri.getQueryParameter(QUERY_PARAM_OFFLINE) != null; if (!offline && (lastUpdated == 0 || now - lastUpdated > UPDATE_FREQ_MS)) { Map<String, ExchangeRate> newExchangeRates = null; if (newExchangeRates == null) newExchangeRates = requestExchangeRates(); if (newExchangeRates != null) { exchangeRates = newExchangeRates; lastUpdated = now;// ww w . jav a 2 s . c o m final ExchangeRate exchangeRateToCache = bestExchangeRate(config.getExchangeCurrencyCode()); if (exchangeRateToCache != null) config.setCachedExchangeRate(exchangeRateToCache); } } if (exchangeRates == null) return null; final MatrixCursor cursor = new MatrixCursor( new String[] { BaseColumns._ID, KEY_CURRENCY_CODE, KEY_RATE_COIN, KEY_RATE_FIAT, KEY_SOURCE }); if (selection == null) { for (final Map.Entry<String, ExchangeRate> entry : exchangeRates.entrySet()) { final ExchangeRate exchangeRate = entry.getValue(); final org.bitcoinj.utils.ExchangeRate rate = exchangeRate.rate; final String currencyCode = exchangeRate.getCurrencyCode(); cursor.newRow().add(currencyCode.hashCode()).add(currencyCode).add(rate.coin.value) .add(rate.fiat.value).add(exchangeRate.source); } } else if (selection.equals(QUERY_PARAM_Q)) { final String selectionArg = selectionArgs[0].toLowerCase(Locale.US); for (final Map.Entry<String, ExchangeRate> entry : exchangeRates.entrySet()) { final ExchangeRate exchangeRate = entry.getValue(); final org.bitcoinj.utils.ExchangeRate rate = exchangeRate.rate; final String currencyCode = exchangeRate.getCurrencyCode(); final String currencySymbol = GenericUtils.currencySymbol(currencyCode); if (currencyCode.toLowerCase(Locale.US).contains(selectionArg) || currencySymbol.toLowerCase(Locale.US).contains(selectionArg)) cursor.newRow().add(currencyCode.hashCode()).add(currencyCode).add(rate.coin.value) .add(rate.fiat.value).add(exchangeRate.source); } } else if (selection.equals(KEY_CURRENCY_CODE)) { final String selectionArg = selectionArgs[0]; final ExchangeRate exchangeRate = bestExchangeRate(selectionArg); if (exchangeRate != null) { final org.bitcoinj.utils.ExchangeRate rate = exchangeRate.rate; final String currencyCode = exchangeRate.getCurrencyCode(); cursor.newRow().add(currencyCode.hashCode()).add(currencyCode).add(rate.coin.value) .add(rate.fiat.value).add(exchangeRate.source); } } return cursor; }
From source file:com.abcvoipsip.ui.favorites.FavLoader.java
/** * Creates a cursor that contains a single row and maps the section to the * given value.// w w w . j a v a 2 s .co m */ private Cursor createHeaderCursorFor(SipProfile account) { MatrixCursor matrixCursor = new MatrixCursor(new String[] { BaseColumns._ID, ContactsWrapper.FIELD_TYPE, SipProfile.FIELD_DISPLAY_NAME, SipProfile.FIELD_WIZARD }); matrixCursor.addRow( new Object[] { account.id, ContactsWrapper.TYPE_GROUP, account.display_name, account.wizard }); return matrixCursor; }
From source file:com.bamalearn.bamalearnburmese.DrawerMainActivity.java
private void populateAdapter(String query) { final MatrixCursor c = new MatrixCursor(new String[] { BaseColumns._ID, "cityName" }); for (int i = 0; i < SUGGESTIONS.length; i++) { if (SUGGESTIONS[i].toLowerCase().startsWith(query.toLowerCase())) c.addRow(new Object[] { i, SUGGESTIONS[i] }); }/*from ww w . j a va2 s . co m*/ mAdapter.changeCursor(c); }
From source file:de.janrenz.app.mediathek.ArdMediathekProvider.java
private Cursor processResultForBroadcast(String result) { MatrixCursor cursor = new MatrixCursor(new String[] { "_id", "title", "subtitle", "image", "extId", "startTime", "startTimeAsTimestamp", "isLive", "description", "timeinfo" }); try {/*from www. jav a2 s .c o m*/ // TODO Auto-generated catch block JSONArray jsonArray = new JSONArray(result); for (int i = 0; i < jsonArray.length(); i++) { JSONObject json_data = jsonArray.getJSONObject(i); // build the Headline String t1 = android.text.Html.fromHtml(json_data.getString("Title1")).toString(); String t2 = android.text.Html.fromHtml(json_data.getString("Title3")).toString(); String t3 = android.text.Html.fromHtml(json_data.getString("Title2")).toString(); cursor.addRow(new Object[] { i, t1, t3, json_data.getString("ImageUrl").toString(), json_data.getString("VId"), json_data.getString("BTimeF").toString(), json_data.getString("BTime").toString(), "true", android.text.Html.fromHtml(json_data.getString("Teasertext").toString()).toString(), android.text.Html.fromHtml(json_data.getString("Title5").toString()).toString() }); } } catch (JSONException e) { e.printStackTrace(); } return (Cursor) cursor; }
From source file:ro.expectations.expenses.ui.transactions.TransactionsActivity.java
@Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { MatrixCursor matrixCursor = new MatrixCursor( new String[] { ExpensesContract.Accounts._ID, ExpensesContract.Accounts.TITLE }); matrixCursor.addRow(new Object[] { "0", getString(R.string.all_accounts) }); MergeCursor mergeCursor = new MergeCursor(new Cursor[] { matrixCursor, data }); ((AccountsSpinnerAdapter) mAccountsSpinner.getAdapter()).swapCursor(mergeCursor); setSpinnerItemById(mSelectedAccountId); }
From source file:io.github.carlorodriguez.alarmon.MediaListView.java
protected void query(Uri contentUri, String nameColumn, String selection, int rowResId, String[] displayColumns, int[] resIDs) { this.nameColumn = nameColumn; final ArrayList<String> queryColumns = new ArrayList<>(displayColumns.length + 1); queryColumns.addAll(Arrays.asList(displayColumns)); // The ID column is required for the SimpleCursorAdapter. Make sure to // add it if it's not already there. if (!queryColumns.contains(BaseColumns._ID)) { queryColumns.add(BaseColumns._ID); }// w ww. ja v a2s . com Cursor dbCursor; if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { dbCursor = getContext().getContentResolver().query(contentUri, queryColumns.toArray(new String[queryColumns.size()]), selection, null, sortOrder); } else { dbCursor = new MatrixCursor(queryColumns.toArray(new String[queryColumns.size()])); } if (staticCursor != null) { Cursor[] cursors = new Cursor[] { staticCursor, dbCursor }; cursor = new MergeCursor(cursors); } else { cursor = dbCursor; } manageCursor(cursor); this.contentUri = contentUri; final SimpleCursorAdapter adapter = new SimpleCursorAdapter(getContext(), rowResId, cursor, displayColumns, resIDs, 1); // Use a custom binder to highlight the selected element. adapter.setViewBinder(new ViewBinder() { @Override public boolean setViewValue(View view, Cursor cursor, int columnIndex) { if (view.getVisibility() == View.VISIBLE && view instanceof TextView) { TextView text = (TextView) view; if (isItemChecked(cursor.getPosition())) { text.setTypeface(Typeface.DEFAULT_BOLD); } else { text.setTypeface(Typeface.DEFAULT); } } // Let the default binder do the real work. return false; } }); setAdapter(adapter); setOnItemClickListener(this); }
From source file:com.owncloud.android.providers.UsersAndGroupsSearchProvider.java
private Cursor searchForUsersOrGroups(Uri uri) { MatrixCursor response = null;//w w w . jav a2 s . c o m String userQuery = uri.getLastPathSegment().toLowerCase(); /// need to trust on the AccountUtils to get the current account since the query in the client side is not /// directly started by our code, but from SearchView implementation Account account = AccountUtils.getCurrentOwnCloudAccount(getContext()); /// request to the OC server about users and groups matching userQuery GetRemoteShareesOperation searchRequest = new GetRemoteShareesOperation(userQuery, REQUESTED_PAGE, RESULTS_PER_PAGE); RemoteOperationResult result = searchRequest.execute(account, getContext()); List<JSONObject> names = new ArrayList<JSONObject>(); if (result.isSuccess()) { for (Object o : result.getData()) { // Get JSonObjects from response names.add((JSONObject) o); } } else { showErrorMessage(result); } /// convert the responses from the OC server to the expected format if (names.size() > 0) { response = new MatrixCursor(COLUMNS); Iterator<JSONObject> namesIt = names.iterator(); int count = 0; JSONObject item; String displayName; Uri dataUri; Uri userBaseUri = new Uri.Builder().scheme("content").authority(DATA_USER).build(); Uri groupBaseUri = new Uri.Builder().scheme("content").authority(DATA_GROUP).build(); try { while (namesIt.hasNext()) { item = namesIt.next(); String userName = item.getString(GetRemoteShareesOperation.PROPERTY_LABEL); JSONObject value = item.getJSONObject(GetRemoteShareesOperation.NODE_VALUE); byte type = (byte) value.getInt(GetRemoteShareesOperation.PROPERTY_SHARE_TYPE); String shareWith = value.getString(GetRemoteShareesOperation.PROPERTY_SHARE_WITH); if (GetRemoteShareesOperation.GROUP_TYPE.equals(type)) { displayName = getContext().getString(R.string.share_group_clarification, userName); dataUri = Uri.withAppendedPath(groupBaseUri, shareWith); } else { displayName = userName; dataUri = Uri.withAppendedPath(userBaseUri, shareWith); } response.newRow().add(count++) // BaseColumns._ID .add(displayName) // SearchManager.SUGGEST_COLUMN_TEXT_1 .add(dataUri); } } catch (JSONException e) { Log_OC.e(TAG, "Exception while parsing data of users/groups", e); } } return response; }
From source file:com.google.android.dialer.provider.DialerProvider.java
private Cursor buildResultCursor(String[] array, JSONArray jsonArray, int n) throws JSONException { int indexDisplayName = -1; int indexData3 = -1; int indexHasPhoneNumber = -1; int indexId = -1; int indexContactId = -1; int indexData1 = -1; int indexData2 = -1; int indexPhotoUri = -1; int indexPhotoThumbUri = -1; int indexLookup = -1; for (int i = 0; i < array.length; ++i) { String s = array[i];// w w w .ja va2s .c o m if ("display_name".equals(s)) { indexDisplayName = i; } else if ("data3".equals(s)) { indexData3 = i; } else if ("has_phone_number".equals(s)) { indexHasPhoneNumber = i; } else if ("_id".equals(s)) { indexId = i; } else if ("contact_id".equals(s)) { indexContactId = i; } else if ("data1".equals(s)) { indexData1 = i; } else if ("data2".equals(s)) { indexData2 = i; } else if ("photo_uri".equals(s)) { indexPhotoUri = i; } else if ("photo_thumb_uri".equals(s)) { indexPhotoThumbUri = i; } else if ("lookup".equals(s)) { indexLookup = i; } } ContentResolver resolver = getContext().getContentResolver(); boolean showNearbyDistance = Gservices.getBoolean(resolver, "dialer_debug_display_nearby_place_distance", false); int n12; if (Gservices.getBoolean(resolver, "dialer_enable_nearby_places_export", true)) { n12 = 2; } else { n12 = 0; } MatrixCursor matrixCursor = new MatrixCursor(array); JSONArray jsonArray2 = jsonArray.getJSONArray(1); int n13 = 1; int position = 0; while (position < jsonArray2.length()) { try { JSONArray jsonArray3 = jsonArray2.getJSONArray(position); String displayName = decodeHtml(jsonArray3.getString(0)); JSONObject jsonObject = jsonArray3.getJSONObject(3); String data1 = decodeHtml(jsonObject.getString("b")); String data3 = decodeHtml(jsonObject.getString("g")); String optString = jsonObject.optString("f", null); String photoUri = jsonObject.optString("d", null); if (showNearbyDistance) { String miles = jsonObject.optString("c", null); if (miles != null) { displayName = displayName + " [" + miles + " miles]"; } } if (!data1.isEmpty()) { Object[] array2 = new Object[array.length]; if (indexDisplayName >= 0) { array2[indexDisplayName] = displayName; } if (indexData3 >= 0) { array2[indexData3] = data3; } if (indexHasPhoneNumber >= 0) { array2[indexHasPhoneNumber] = true; } if (indexContactId != -1) { array2[indexContactId] = n13; } if (indexData1 >= 0) { array2[indexData1] = data1; } if (indexData2 >= 0) { array2[indexData2] = 12; } String photoThumbUri; if (photoUri == null) { photoUri = new Uri.Builder().scheme("android.resource") .authority("com.google.android.dialer") .appendPath(String.valueOf(R.drawable.ic_places_picture_180_holo_light)).toString(); photoThumbUri = new Uri.Builder().scheme("android.resource") .authority("com.google.android.dialer") .appendPath(String.valueOf(R.drawable.ic_places_picture_holo_light)).toString(); } else { photoThumbUri = photoUri; } if (indexPhotoUri >= 0) { array2[indexPhotoUri] = photoUri; } if (indexPhotoThumbUri >= 0) { array2[indexPhotoThumbUri] = photoThumbUri; } if (indexLookup >= 0) { JSONObject put = new JSONObject() .put("vnd.android.cursor.item/name", new JSONObject().put("data1", displayName)) .put("vnd.android.cursor.item/phone_v2", JsonUtil.newJsonArray( new JSONObject().put("data1", data1).put("data2", 12))) .put("vnd.android.cursor.item/postal-address_v2", JsonUtil.newJsonArray( new JSONObject().put("data1", displayName + ", " + data3).put("data2", 2))); if (optString != null) { put.put("vnd.android.cursor.item/website", JsonUtil .newJsonArray(new JSONObject().put("data1", optString).put("data2", 3))); } array2[indexLookup] = new JSONObject().put("display_name", displayName) .put("display_name_source", 30).put("exportSupport", n12).put("photo_uri", photoUri) .put("vnd.android.cursor.item/contact", put).toString(); } if (indexId != -1) { array2[indexId] = n13; } matrixCursor.addRow(array2); if (n != -1 && matrixCursor.getCount() >= n) { break; } n13++; } } catch (JSONException e) { Log.e("DialerProvider", "Skipped the suggestions at position " + position, e); } position++; } return matrixCursor; }
From source file:com.csipsimple.ui.favorites.FavLoader.java
/** * Creates a cursor that contains a single row and maps the section to the * given value.// ww w .j a va2 s . c o m */ private Cursor createHeaderCursorFor(SipProfile account) { MatrixCursor matrixCursor = new MatrixCursor( new String[] { BaseColumns._ID, ContactsWrapper.FIELD_TYPE, SipProfile.FIELD_DISPLAY_NAME, SipProfile.FIELD_WIZARD, SipProfile.FIELD_ANDROID_GROUP, SipProfile.FIELD_PUBLISH_ENABLED, SipProfile.FIELD_REG_URI, SipProfile.FIELD_PROXY, SipProfile.FIELD_ACC_ID }); String proxies = ""; if (account.proxies != null) { proxies = TextUtils.join(SipProfile.PROXIES_SEPARATOR, account.proxies); } matrixCursor .addRow(new Object[] { account.id, ContactsWrapper.TYPE_GROUP, account.display_name, account.wizard, account.android_group, account.publish_enabled, account.reg_uri, proxies, account.acc_id }); return matrixCursor; }