List of usage examples for android.database MatrixCursor MatrixCursor
public MatrixCursor(String[] columnNames)
From source file:cm.aptoide.pt.LatestLikesComments.java
public Cursor getLikes() { endPointLikes = String.format(endPointLikes, repoName); MatrixCursor cursor = new MatrixCursor(new String[] { "_id", "apkid", "name", "like", "username" }); try {// ww w.j a v a2 s.c o m HttpURLConnection connection = (HttpURLConnection) new URL(endPointLikes).openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); JSONObject respJSON = new JSONObject(sb.toString()); JSONArray array = respJSON.getJSONArray("listing"); for (int i = 0; i != array.length(); i++) { String apkid = ((JSONObject) array.get(i)).getString("apkid"); String name = ((JSONObject) array.get(i)).getString("name"); String like = ((JSONObject) array.get(i)).getString("like"); String username = ((JSONObject) array.get(i)).getString("username"); if (username.equals("NOT_SIGNED_UP")) { username = ""; } cursor.newRow().add(i).add(apkid).add(name).add(like).add(username); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return cursor; }
From source file:fr.matthiasbosc.translucentmap.PlaceProvider.java
@Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Cursor c = null;/* w w w . j a v a 2 s.c o m*/ PlaceJSONParser parser = new PlaceJSONParser(); PlaceDetailsJSONParser detailsParser = new PlaceDetailsJSONParser(); String jsonString = ""; String jsonPlaceDetails = ""; List<HashMap<String, String>> list = null; List<HashMap<String, String>> detailsList = null; MatrixCursor mCursor = null; switch (mUriMatcher.match(uri)) { case SEARCH: // Defining a cursor object with columns description, lat and lng mCursor = new MatrixCursor(new String[] { "description", "lat", "lng" }); // Create a parser object to parse places in JSON format parser = new PlaceJSONParser(); // Create a parser object to parse place details in JSON format detailsParser = new PlaceDetailsJSONParser(); // Get Places from Google Places API jsonString = getPlaces(selectionArgs); try { // Parse the places ( JSON => List ) list = parser.parse(new JSONObject(jsonString)); // Finding latitude and longitude for each places using Google Places Details API for (int i = 0; i < list.size(); i++) { HashMap<String, String> hMap = (HashMap<String, String>) list.get(i); detailsParser = new PlaceDetailsJSONParser(); // Get Place details jsonPlaceDetails = getPlaceDetails(hMap.get("reference")); // Parse the details ( JSON => List ) detailsList = detailsParser.parse(new JSONObject(jsonPlaceDetails)); // Creating cursor object with places for (int j = 0; j < detailsList.size(); j++) { HashMap<String, String> hMapDetails = detailsList.get(j); // Adding place details to cursor mCursor.addRow(new String[] { hMap.get("description"), hMapDetails.get("lat"), hMapDetails.get("lng") }); } } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } c = mCursor; break; case SUGGESTIONS: // Defining a cursor object with columns id, SUGGEST_COLUMN_TEXT_1, SUGGEST_COLUMN_INTENT_EXTRA_DATA mCursor = new MatrixCursor(new String[] { "_id", SearchManager.SUGGEST_COLUMN_TEXT_1, SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA }); // Creating a parser object to parse places in JSON format parser = new PlaceJSONParser(); // Get Places from Google Places API jsonString = getPlaces(selectionArgs); try { // Parse the places ( JSON => List ) list = parser.parse(new JSONObject(jsonString)); // Creating cursor object with places for (int i = 0; i < list.size(); i++) { HashMap<String, String> hMap = (HashMap<String, String>) list.get(i); // Adding place details to cursor mCursor.addRow( new String[] { Integer.toString(i), hMap.get("description"), hMap.get("reference") }); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } c = mCursor; break; case DETAILS: // Defining a cursor object with columns description, lat and lng mCursor = new MatrixCursor(new String[] { "description", "lat", "lng" }); detailsParser = new PlaceDetailsJSONParser(); jsonPlaceDetails = getPlaceDetails(selectionArgs[0]); try { detailsList = detailsParser.parse(new JSONObject(jsonPlaceDetails)); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } for (int j = 0; j < detailsList.size(); j++) { HashMap<String, String> hMapDetails = detailsList.get(j); mCursor.addRow(new String[] { hMapDetails.get("formatted_address"), hMapDetails.get("lat"), hMapDetails.get("lng") }); } c = mCursor; break; } return c; }
From source file:com.example.android.notepad.CMNotesProvider.java
@Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { String[] keys = null;//from w ww. j av a 2s. c om switch (sUriMatcher.match(uri)) { case NOTES: //qb.setProjectionMap(sNotesProjectionMap); break; case NOTE_ID: //qb.setProjectionMap(sNotesProjectionMap); //qb.appendWhere(Notes._ID + "=" + uri.getPathSegments().get(1)); keys = new String[] { uri.getPathSegments().get(1) }; break; case LIVE_FOLDER_NOTES: //qb.setProjectionMap(sLiveFolderProjectionMap); break; default: throw new IllegalArgumentException("Unknown URI " + uri); } // Get the database and run the query //SQLiteDatabase db = mOpenHelper.getReadableDatabase(); //Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy); CMAdapter cmadapter = new CMAdapter(); JSONObject objects = cmadapter.getValues(keys); MatrixCursor c = new MatrixCursor(projection); // objects is a map of key => value mappings, where key is the _id Iterator<String> note_ids = objects.keys(); while (note_ids.hasNext()) { String id = note_ids.next(); RowBuilder row = c.newRow(); for (String field : projection) { if (field.equals(Notes._ID)) { row.add(id); } else { String val = "bad entry"; try { val = objects.getJSONObject(id).getString(field); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } row.add(val); } } } // Tell the cursor what uri to watch, so it knows when its source data changes c.setNotificationUri(getContext().getContentResolver(), uri); return c; }
From source file:com.feathercoin.wallet.feathercoin.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(); if (exchangeRates == null || now - lastUpdated > UPDATE_FREQ_MS) { Map<String, ExchangeRate> newExchangeRates = getFeathercoinCharts(); if (exchangeRates == null && newExchangeRates == null) newExchangeRates = getBlockchainInfo(); if (newExchangeRates != null) { exchangeRates = newExchangeRates; lastUpdated = now;//w w w .ja v a2s . c om } } if (exchangeRates == null) return null; final MatrixCursor cursor = new MatrixCursor( new String[] { BaseColumns._ID, KEY_CURRENCY_CODE, KEY_RATE, KEY_SOURCE }); if (selection == null) { for (final Map.Entry<String, ExchangeRate> entry : exchangeRates.entrySet()) { final ExchangeRate rate = entry.getValue(); cursor.newRow().add(entry.getKey().hashCode()).add(rate.currencyCode).add(rate.rate.longValue()) .add(rate.source); } } else if (selection.equals(KEY_CURRENCY_CODE)) { final String code = selectionArgs[0]; final ExchangeRate rate = exchangeRates.get(code); try { cursor.newRow().add(code.hashCode()).add(rate.currencyCode).add(rate.rate.longValue()) .add(rate.source); } catch (NullPointerException e) { Log.e("Feathercoin", "Unable to add an exchange rate. NullPointerException."); } } return cursor; }
From source file:de.schildbach.wallet.elysium.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(); if (exchangeRates == null || now - lastUpdated > UPDATE_FREQ_MS) { Map<String, ExchangeRate> newExchangeRates = getElysiumCharts(); if (exchangeRates == null && newExchangeRates == null) newExchangeRates = getBlockchainInfo(); if (newExchangeRates != null) { exchangeRates = newExchangeRates; lastUpdated = now;/*from ww w .j a v a 2 s.co m*/ } } if (exchangeRates == null) return null; final MatrixCursor cursor = new MatrixCursor( new String[] { BaseColumns._ID, KEY_CURRENCY_CODE, KEY_RATE, KEY_SOURCE }); if (selection == null) { for (final Map.Entry<String, ExchangeRate> entry : exchangeRates.entrySet()) { final ExchangeRate rate = entry.getValue(); cursor.newRow().add(entry.getKey().hashCode()).add(rate.currencyCode).add(rate.rate.longValue()) .add(rate.source); } } else if (selection.equals(KEY_CURRENCY_CODE)) { final String code = selectionArgs[0]; final ExchangeRate rate = exchangeRates.get(code); try { cursor.newRow().add(code.hashCode()).add(rate.currencyCode).add(rate.rate.longValue()) .add(rate.source); } catch (NullPointerException e) { Log.e("Elysium", "Unable to add an exchange rate. NullPointerException."); } } return cursor; }
From source file:de.schildbach.wallet.marscoin.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(); if (exchangeRates == null || now - lastUpdated > UPDATE_FREQ_MS) { Map<String, ExchangeRate> newExchangeRates = getmarscoinCharts(); if (exchangeRates == null && newExchangeRates == null) newExchangeRates = getBlockchainInfo(); if (newExchangeRates != null) { exchangeRates = newExchangeRates; lastUpdated = now;//w w w .j a v a 2s . c om } } if (exchangeRates == null) return null; final MatrixCursor cursor = new MatrixCursor( new String[] { BaseColumns._ID, KEY_CURRENCY_CODE, KEY_RATE, KEY_SOURCE }); if (selection == null) { for (final Map.Entry<String, ExchangeRate> entry : exchangeRates.entrySet()) { final ExchangeRate rate = entry.getValue(); cursor.newRow().add(entry.getKey().hashCode()).add(rate.currencyCode).add(rate.rate.longValue()) .add(rate.source); } } else if (selection.equals(KEY_CURRENCY_CODE)) { final String code = selectionArgs[0]; final ExchangeRate rate = exchangeRates.get(code); try { cursor.newRow().add(code.hashCode()).add(rate.currencyCode).add(rate.rate.longValue()) .add(rate.source); } catch (NullPointerException e) { Log.e("marscoin", "Unable to add an exchange rate. NullPointerException."); } } return cursor; }
From source file:de.schildbach.wallet.worldcoin.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(); if (exchangeRates == null || now - lastUpdated > UPDATE_FREQ_MS) { Map<String, ExchangeRate> newExchangeRates = getworldcoinCharts(); if (exchangeRates == null && newExchangeRates == null) newExchangeRates = getBlockchainInfo(); if (newExchangeRates != null) { exchangeRates = newExchangeRates; lastUpdated = now;/*from ww w . j a va 2 s . c o m*/ } } if (exchangeRates == null) return null; final MatrixCursor cursor = new MatrixCursor( new String[] { BaseColumns._ID, KEY_CURRENCY_CODE, KEY_RATE, KEY_SOURCE }); if (selection == null) { for (final Map.Entry<String, ExchangeRate> entry : exchangeRates.entrySet()) { final ExchangeRate rate = entry.getValue(); cursor.newRow().add(entry.getKey().hashCode()).add(rate.currencyCode).add(rate.rate.longValue()) .add(rate.source); } } else if (selection.equals(KEY_CURRENCY_CODE)) { final String code = selectionArgs[0]; final ExchangeRate rate = exchangeRates.get(code); try { cursor.newRow().add(code.hashCode()).add(rate.currencyCode).add(rate.rate.longValue()) .add(rate.source); } catch (NullPointerException e) { Log.e("worldcoin", "Unable to add an exchange rate. NullPointerException."); } } return cursor; }
From source file:nl.terr.tabweave.TabWeave.java
public void fillData(List<JSONObject> lTabs) { // Create an array to specify the fields we want to display in the list (only TITLE) String[] from = new String[] { SyncWeave.KEY_TITLE, SyncWeave.KEY_URL, SyncWeave.KEY_ROWID }; // and an array of the fields we want to bind those fields to (in this case just text1) int[] to = new int[] { R.id.title, R.id.url }; int iBrowserInstances = lTabs.size(); MatrixCursor matrixCursor = new MatrixCursor(from); int iTabId = 0; // Show "No tabs" message TextView tvNoTabs = (TextView) findViewById(R.id.no_tabs); tvNoTabs.setVisibility(View.VISIBLE); try {/*ww w .j a v a2 s . c o m*/ for (int iWeaveBrowserInstance = 0; iWeaveBrowserInstance < iBrowserInstances; iWeaveBrowserInstance++) { int iNumberTabs = lTabs.get(iWeaveBrowserInstance).getJSONArray("tabs").length(); // Hide "No tabs" message if (iNumberTabs > 0) { tvNoTabs.setVisibility(View.INVISIBLE); } for (int iWeaveTab = 0; iWeaveTab < iNumberTabs; iWeaveTab++) { matrixCursor.newRow() .add(lTabs.get(iWeaveBrowserInstance).getJSONArray("tabs").getJSONObject(iWeaveTab) .getString("title")) .add(lTabs.get(iWeaveBrowserInstance).getJSONArray("tabs").getJSONObject(iWeaveTab) .getJSONArray("urlHistory").getString(0)) .add(iTabId); iTabId++; } } } catch (Exception e) { e.printStackTrace(); AlertDialog alert = createAlertDialog(this, e.getClass().toString(), e.getMessage()); alert.show(); } ListAdapter listAdapter = new SimpleCursorAdapter(this, // Context R.layout.tab_row, // Specify the row template to use (here, two columns bound to the two retrieved cursor rows). matrixCursor, from, to); setListAdapter(listAdapter); }
From source file:de.schildbach.wallet.litecoin.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(); if (exchangeRates == null || now - lastUpdated > UPDATE_FREQ_MS) { LycBtcRate = getLycBtcRate();/*w w w . java 2 s. c o m*/ if (LycBtcRate == null) return null; Map<String, ExchangeRate> newExchangeRates = getBlockchainInfo(); if (exchangeRates == null && newExchangeRates == null) newExchangeRates = getLycancoinCharts(); if (newExchangeRates != null) { exchangeRates = newExchangeRates; lastUpdated = now; } } if (exchangeRates == null) return null; final MatrixCursor cursor = new MatrixCursor( new String[] { BaseColumns._ID, KEY_CURRENCY_CODE, KEY_RATE, KEY_SOURCE }); if (selection == null) { for (final Map.Entry<String, ExchangeRate> entry : exchangeRates.entrySet()) { final ExchangeRate rate = entry.getValue(); cursor.newRow().add(entry.getKey().hashCode()).add(rate.currencyCode).add(rate.rate.longValue()) .add(rate.source); } } else if (selection.equals(KEY_CURRENCY_CODE)) { final String code = selectionArgs[0]; final ExchangeRate rate = exchangeRates.get(code); try { cursor.newRow().add(code.hashCode()).add(rate.currencyCode).add(rate.rate.longValue()) .add(rate.source); } catch (NullPointerException e) { Log.e("Lycancoin", "Unable to add an exchange rate. NullPointerException."); } } return cursor; }
From source file:com.android.messaging.datamodel.FrequentContactsCursorBuilder.java
/** * Attempt to build the cursor records from the frequent and all contacts cursor if they * are both ready to be consumed./*from w w w .j a v a2 s. c o m*/ * @return the frequent contact cursor if built successfully, or null if it can't be built yet. */ public Cursor build() { if (mFrequentContactsCursor != null && mAllContactsCursor != null) { Assert.isTrue(!mFrequentContactsCursor.isClosed()); Assert.isTrue(!mAllContactsCursor.isClosed()); // Frequent contacts cursor has one record per contact, plus it doesn't contain info // such as phone number and type. In order for the records to be usable by Bugle, we // would like to populate it with information from the all contacts cursor. final MatrixCursor retCursor = new MatrixCursor(ContactUtil.PhoneQuery.PROJECTION); // First, go through the frequents cursor and take note of all lookup keys and their // corresponding rank in the frequents list. final SimpleArrayMap<String, Integer> lookupKeyToRankMap = new SimpleArrayMap<String, Integer>(); int oldPosition = mFrequentContactsCursor.getPosition(); int rank = 0; mFrequentContactsCursor.moveToPosition(-1); while (mFrequentContactsCursor.moveToNext()) { final String lookupKey = mFrequentContactsCursor.getString(ContactUtil.INDEX_LOOKUP_KEY_FREQUENT); lookupKeyToRankMap.put(lookupKey, rank++); } mFrequentContactsCursor.moveToPosition(oldPosition); // Second, go through the all contacts cursor once and retrieve all information // (multiple phone numbers etc.) and store that in an array list. Since the all // contacts list only contains phone contacts, this step will ensure that we filter // out any invalid/email contacts in the frequents list. final ArrayList<Object[]> rows = new ArrayList<Object[]>(mFrequentContactsCursor.getCount()); oldPosition = mAllContactsCursor.getPosition(); mAllContactsCursor.moveToPosition(-1); while (mAllContactsCursor.moveToNext()) { final String lookupKey = mAllContactsCursor.getString(ContactUtil.INDEX_LOOKUP_KEY); if (lookupKeyToRankMap.containsKey(lookupKey)) { final Object[] row = new Object[ContactUtil.PhoneQuery.PROJECTION.length]; row[ContactUtil.INDEX_DATA_ID] = mAllContactsCursor.getLong(ContactUtil.INDEX_DATA_ID); row[ContactUtil.INDEX_CONTACT_ID] = mAllContactsCursor.getLong(ContactUtil.INDEX_CONTACT_ID); row[ContactUtil.INDEX_LOOKUP_KEY] = mAllContactsCursor.getString(ContactUtil.INDEX_LOOKUP_KEY); row[ContactUtil.INDEX_DISPLAY_NAME] = mAllContactsCursor .getString(ContactUtil.INDEX_DISPLAY_NAME); row[ContactUtil.INDEX_PHOTO_URI] = mAllContactsCursor.getString(ContactUtil.INDEX_PHOTO_URI); row[ContactUtil.INDEX_PHONE_EMAIL] = mAllContactsCursor .getString(ContactUtil.INDEX_PHONE_EMAIL); row[ContactUtil.INDEX_PHONE_EMAIL_TYPE] = mAllContactsCursor .getInt(ContactUtil.INDEX_PHONE_EMAIL_TYPE); row[ContactUtil.INDEX_PHONE_EMAIL_LABEL] = mAllContactsCursor .getString(ContactUtil.INDEX_PHONE_EMAIL_LABEL); rows.add(row); } } mAllContactsCursor.moveToPosition(oldPosition); // Now we have a list of rows containing frequent contacts in alphabetical order. // Therefore, sort all the rows according to their actual ranks in the frequents list. Collections.sort(rows, new Comparator<Object[]>() { @Override public int compare(final Object[] lhs, final Object[] rhs) { final String lookupKeyLhs = (String) lhs[ContactUtil.INDEX_LOOKUP_KEY]; final String lookupKeyRhs = (String) rhs[ContactUtil.INDEX_LOOKUP_KEY]; Assert.isTrue(lookupKeyToRankMap.containsKey(lookupKeyLhs) && lookupKeyToRankMap.containsKey(lookupKeyRhs)); final int rankLhs = lookupKeyToRankMap.get(lookupKeyLhs); final int rankRhs = lookupKeyToRankMap.get(lookupKeyRhs); if (rankLhs < rankRhs) { return -1; } else if (rankLhs > rankRhs) { return 1; } else { // Same rank, so it's two contact records for the same contact. // Perform secondary sorting on the phone type. Always place // mobile before everything else. final int phoneTypeLhs = (int) lhs[ContactUtil.INDEX_PHONE_EMAIL_TYPE]; final int phoneTypeRhs = (int) rhs[ContactUtil.INDEX_PHONE_EMAIL_TYPE]; if (phoneTypeLhs == Phone.TYPE_MOBILE && phoneTypeRhs == Phone.TYPE_MOBILE) { return 0; } else if (phoneTypeLhs == Phone.TYPE_MOBILE) { return -1; } else if (phoneTypeRhs == Phone.TYPE_MOBILE) { return 1; } else { // Use the default sort order, i.e. sort by phoneType value. return phoneTypeLhs < phoneTypeRhs ? -1 : (phoneTypeLhs == phoneTypeRhs ? 0 : 1); } } } }); // Finally, add all the rows to this cursor. for (final Object[] row : rows) { retCursor.addRow(row); } return retCursor; } return null; }