Example usage for android.database MatrixCursor MatrixCursor

List of usage examples for android.database MatrixCursor MatrixCursor

Introduction

In this page you can find the example usage for android.database MatrixCursor MatrixCursor.

Prototype

public MatrixCursor(String[] columnNames) 

Source Link

Document

Constructs a new cursor.

Usage

From source file:osu.appclub.corvallisbus.MainActivity.java

@Override
public void searchComplete(Cursor cursor) {
    Log.d("osu.appclub", "Found " + cursor.getCount() + " results");
    if (cursor.getCount() == 0) {
        MatrixCursor placeholder = new MatrixCursor(new String[] { "_id" });
        placeholder.addRow(new Object[] { 0 });
        searchView.setSuggestionsAdapter(new CursorAdapter(this, placeholder, false) {
            @Override/*from  w  ww.  ja  v  a2  s .co m*/
            public View newView(Context context, Cursor cursor, ViewGroup parent) {
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View view = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
                return view;
            }

            @Override
            public void bindView(View view, Context context, Cursor cursor) {
                TextView text = (TextView) view.findViewById(android.R.id.text1);
                text.setText(R.string.search_no_results);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // no-op
                    }
                });
            }
        });
    } else {
        searchView.setSuggestionsAdapter(new CursorAdapter(this, cursor, false) {
            @Override
            public View newView(Context context, Cursor cursor, ViewGroup parent) {
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View view = inflater.inflate(android.R.layout.simple_list_item_2, parent, false);
                return view;
            }

            @SuppressLint("DefaultLocale")
            @Override
            public void bindView(View view, Context context, Cursor cursor) {
                final int stopID = cursor.getInt(0);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        searchView.clearFocus();
                        enqueueBusStop(stopID);
                    }
                });

                TextView textStopName = (TextView) view.findViewById(android.R.id.text1);
                textStopName.setText(cursor.getString(1));

                TextView textDistance = (TextView) view.findViewById(android.R.id.text2);
                if (cursor.getType(2) == Cursor.FIELD_TYPE_NULL) {
                    textDistance.setText("");
                } else {
                    textDistance.setText(String.format("%.1f miles", cursor.getDouble(2)));
                }

            }
        });
    }
}

From source file:com.esri.arcgisruntime.sample.findplace.MainActivity.java

/**
 * Sets up the POI SearchView. Uses MatrixCursor to show suggestions to the user as the user inputs text.
 *///from w  w w .  j  a v a 2 s  .  c  o  m
private void setupPoi() {

    mPoiSuggestParameters = new SuggestParameters();
    // filter categories for POI
    mPoiSuggestParameters.getCategories().add("POI");
    mPoiGeocodeParameters = new GeocodeParameters();
    // get all attributes
    mPoiGeocodeParameters.getResultAttributeNames().add("*");
    mPoiSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String address) {
            // if proximity SearchView text box is empty, use the device location
            if (mProximitySearchViewEmpty) {
                mPreferredSearchProximity = mLocationDisplay.getMapLocation();
                mProximitySearchView.setQuery("Using current location...", false);
            }
            // keep track of typed address
            mPoiAddress = address;
            // geocode typed address
            geoCodeTypedAddress(address);
            // clear focus from search views
            mPoiSearchView.clearFocus();
            mProximitySearchView.clearFocus();
            return true;
        }

        @Override
        public boolean onQueryTextChange(final String newText) {
            // as long as newText isn't empty, get suggestions from the locatorTask
            if (!newText.equals("")) {
                mPoiSuggestParameters.setSearchArea(mCurrentExtentGeometry);
                final ListenableFuture<List<SuggestResult>> suggestionsFuture = mLocatorTask
                        .suggestAsync(newText, mPoiSuggestParameters);
                suggestionsFuture.addDoneListener(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            // get the results of the async operation
                            List<SuggestResult> suggestResults = suggestionsFuture.get();

                            if (!suggestResults.isEmpty()) {
                                MatrixCursor suggestionsCursor = new MatrixCursor(mColumnNames);
                                int key = 0;
                                // add each poi_suggestion result to a new row
                                for (SuggestResult result : suggestResults) {
                                    suggestionsCursor.addRow(new Object[] { key++, result.getLabel() });
                                }
                                // define SimpleCursorAdapter
                                String[] cols = new String[] { COLUMN_NAME_ADDRESS };
                                int[] to = new int[] { R.id.suggestion_address };
                                final SimpleCursorAdapter suggestionAdapter = new SimpleCursorAdapter(
                                        MainActivity.this, R.layout.suggestion, suggestionsCursor, cols, to, 0);
                                mPoiSearchView.setSuggestionsAdapter(suggestionAdapter);
                                // handle a poi_suggestion being chosen
                                mPoiSearchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
                                    @Override
                                    public boolean onSuggestionSelect(int position) {
                                        return false;
                                    }

                                    @Override
                                    public boolean onSuggestionClick(int position) {
                                        // get the selected row
                                        MatrixCursor selectedRow = (MatrixCursor) suggestionAdapter
                                                .getItem(position);
                                        // get the row's index
                                        int selectedCursorIndex = selectedRow
                                                .getColumnIndex(COLUMN_NAME_ADDRESS);
                                        // get the string from the row at index
                                        mPoiAddress = selectedRow.getString(selectedCursorIndex);
                                        mPoiSearchView.setQuery(mPoiAddress, true);
                                        return true;
                                    }
                                });
                            } else {
                                mPoiAddress = newText;
                            }
                        } catch (Exception e) {
                            Log.e(TAG, "Geocode suggestion error: " + e.getMessage());
                        }
                    }
                });
            }
            return true;
        }
    });
}

From source file:org.cdmckay.android.provider.MediaWikiProvider.java

private Cursor parseXmlPage(String apiUrl, String pageString) {
    final MatrixCursor cursor = new MatrixCursor(PAGE_COLUMN_NAMES);

    try {// ww  w. j ava2s.c  o m
        final PageHandler handler = new PageHandler();
        Xml.parse(pageString, handler);

        final List<PageHandler.Result> results = handler.getResults();
        for (PageHandler.Result result : results) {
            cursor.addRow(new Object[] { result.pageId, result.namespace, result.title, result.content });
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return cursor;
}

From source file:org.cdmckay.android.provider.MediaWikiProvider.java

private Cursor parseJsonPage(String apiUrl, String pageString) {
    final MatrixCursor cursor = new MatrixCursor(PAGE_COLUMN_NAMES);

    try {/*from  w w  w  .  ja  v a  2s  . c  o m*/
        final JSONObject query = new JSONObject(pageString).getJSONObject("query");
        final JSONObject pages = query.getJSONObject("pages");

        @SuppressWarnings("unchecked")
        final Iterator<String> keys = (Iterator<String>) pages.keys();
        while (keys.hasNext()) {
            final String key = (String) keys.next();
            final JSONObject page = pages.getJSONObject(key);
            final String content = page.getJSONArray("revisions").getJSONObject(0)
                    // TODO This should be "parsetree" later.
                    .getString("*");

            cursor.addRow(new Object[] { page.getLong("pageid"), page.getLong("ns"), page.getString("title"),
                    content });
        }
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        throw new RuntimeException(e);
    }

    return cursor;
}

From source file:org.cdmckay.android.provider.MediaWikiProvider.java

private Cursor getSectionsByTitle(String apiUrl, String pageString) {
    final MatrixCursor cursor = new MatrixCursor(SECTION_INFO_COLUMN_NAMES);

    // TODO Parse this mamma-jamma.

    return cursor;
}

From source file:com.futureplatforms.kirin.test.DatabasesBackendTest_inactive.java

public void testCoerceToJSONObject() throws JSONException {
    String[] columnNames = new String[] { "foo", "bar", "baz" };
    MatrixCursor cursor = new MatrixCursor(columnNames);
    cursor.addRow(new Object[] { 1, 2, 3 });

    cursor.moveToFirst();/*from ww w  .j a v a 2s.c o m*/

    JSONObject obj = mBackend.coerceToJSONObject(columnNames, cursor);

    assertEquals(1, obj.getInt("foo"));
    assertEquals(2, obj.getInt("bar"));
    assertEquals(3, obj.getInt("baz"));
}

From source file:com.csipsimple.ui.account.AccountsLoader.java

/**
 * Creates a cursor that contains a single row and maps the section to the
 * given value.//from  w w  w  . j  ava2  s.  c o m
 */
private Cursor createCursorForAccount(FilteredProfile fa) {
    MatrixCursor matrixCursor = new MatrixCursor(COLUMN_HEADERS);

    matrixCursor.addRow(new Object[] { fa.account.id, fa.account.id, fa.account.display_name, fa.account.wizard,
            fa.isForceCall ? 1 : 0, fa.rewriteNumber(numberToCall), fa.getStatusForOutgoing() ? 1 : 0,
            fa.getStatusColor() });
    return matrixCursor;
}

From source file:com.smedic.tubtub.Pane.java

private Loader suggestionLoader(final List<String> suggestions, final CursorAdapter suggestionAdapter,
        final String query) {
    return getSupportLoaderManager().restartLoader(4, null, new LoaderManager.LoaderCallbacks<List<String>>() {
        @Override/*from   w  w  w . j av  a 2  s .  co  m*/
        public Loader<List<String>> onCreateLoader(final int id, final Bundle args) {
            return new SuggestionsLoader(getApplicationContext(), query);
        }

        @Override
        public void onLoadFinished(Loader<List<String>> loader, List<String> data) {
            if (data == null)
                return;
            suggestions.clear();
            suggestions.addAll(data);
            String[] columns = { BaseColumns._ID, SearchManager.SUGGEST_COLUMN_TEXT_1 };
            MatrixCursor cursor = new MatrixCursor(columns);

            for (int i = 0; i < data.size(); i++) {
                String[] tmp = { Integer.toString(i), data.get(i) };
                cursor.addRow(tmp);
            }
            suggestionAdapter.swapCursor(cursor);
        }

        @Override
        public void onLoaderReset(Loader<List<String>> loader) {
            suggestions.clear();
            suggestions.addAll(Collections.<String>emptyList());
        }
    });
}

From source file:com.futureplatforms.kirin.test.DatabasesBackendTest_inactive.java

public void testCoerceToJSONArray() throws JSONException {
    String[] columnNames = new String[] { "foo", "bar", "baz" };
    MatrixCursor cursor = new MatrixCursor(columnNames);
    cursor.addRow(new Object[] { 1, 2, 3 });
    cursor.addRow(new Object[] { 4, 5, 6 });
    cursor.addRow(new Object[] { 7, 8, 9 });

    cursor.moveToFirst();// w  ww  .  jav a  2 s .  c  om

    JSONArray array = mBackend.coerceToJSONArray(columnNames, cursor);

    assertEquals(3, array.length());

    JSONObject obj = array.getJSONObject(0);
    assertEquals(1, obj.getInt("foo"));
    assertEquals(2, obj.getInt("bar"));
    assertEquals(3, obj.getInt("baz"));

    obj = array.getJSONObject(1);
    assertEquals(4, obj.getInt("foo"));
    assertEquals(5, obj.getInt("bar"));
    assertEquals(6, obj.getInt("baz"));

    obj = array.getJSONObject(2);
    assertEquals(7, obj.getInt("foo"));
    assertEquals(8, obj.getInt("bar"));
    assertEquals(9, obj.getInt("baz"));
}

From source file:de.nware.app.hsDroid.provider.onlineService2Provider.java

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    Cursor cursor = null;/*from   w w w  .  j  av  a  2  s . c o  m*/
    switch (mUriMatcher.match(uri)) {
    case EXAMS:
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        qb.setTables(mOpenHelper.getTableName());
        qb.setProjectionMap(examsProjectionMap);
        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        try {
            cursor = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
        } catch (SQLException e) {
            e.printStackTrace();
            Log.d(TAG, "SqlError: " + e.getMessage());
        }
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        break;
    case EXAMS_UPDATE:
        MatrixCursor cur = new MatrixCursor(EXAMS_UPDATE_COLUMNS);
        Integer[] columnValues = updateGrades();
        cur.addRow(new Object[] { 0, columnValues[0], columnValues[1] });
        return cur;
    case EXAMINFOS:
        cursor = getExamInfos(selectionArgs[0], selectionArgs[1], false);
        break;
    case CERTIFICATIONS:
        cursor = getCertifications();
        break;
    default:
        throw new IllegalArgumentException("Unbekannte URI " + uri);
    }

    return cursor;
}