List of usage examples for android.widget SimpleCursorAdapter.CursorToStringConverter SimpleCursorAdapter.CursorToStringConverter
SimpleCursorAdapter.CursorToStringConverter
From source file:org.gnucash.android.ui.transaction.TransactionFormFragment.java
/** * Initializes the transaction name field for autocompletion with existing transaction names in the database */// w ww. j a va 2s . c o m private void initTransactionNameAutocomplete() { final int[] to = new int[] { android.R.id.text1 }; final String[] from = new String[] { DatabaseHelper.KEY_NAME }; SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_dropdown_item_1line, null, from, to, 0); adapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() { @Override public CharSequence convertToString(Cursor cursor) { final int colIndex = cursor.getColumnIndexOrThrow(DatabaseHelper.KEY_NAME); return cursor.getString(colIndex); } }); adapter.setFilterQueryProvider(new FilterQueryProvider() { @Override public Cursor runQuery(CharSequence name) { return mTransactionsDbAdapter.fetchTransactionsStartingWith(name.toString()); } }); mNameEditText.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { mTransaction = mTransactionsDbAdapter.getTransaction(id); mTransaction.setUID(UUID.randomUUID().toString()); mTransaction.setExported(false); mTransaction.setTime(System.currentTimeMillis()); long accountId = ((TransactionsActivity) getSherlockActivity()).getCurrentAccountID(); mTransaction.setAccountUID(mTransactionsDbAdapter.getAccountUID(accountId)); initializeViewsWithTransaction(); } }); mNameEditText.setAdapter(adapter); }
From source file:nl.sogeti.android.gpstracker.actions.NameTrack.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //This activity hides itself, the TrackList activity switches it's visible state this.setVisible(false); paused = false;//from w ww . j av a 2 s. c om mTrackUri = this.getIntent().getData(); mCurStartStationNameFilter = ""; mCurEndStationNameFilter = ""; getSupportLoaderManager().initLoader(STARTSTATIONCURSOR_LOADER, null, this); getSupportLoaderManager().initLoader(ENDSTATIONCURSOR_LOADER, null, this); String[] uiBindFrom = { Tracks.NAME }; int[] uiBindTo = { android.R.id.text1 }; mStartStationSimpleCursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, null, uiBindFrom, uiBindTo, 0); mEndStationSimpleCursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, null, uiBindFrom, uiBindTo, 0); // Set the CursorToStringConverter, to provide the labels for the // choices to be displayed in the AutoCompleteTextView. mStartStationSimpleCursorAdapter .setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() { public String convertToString(android.database.Cursor cursor) { // Get the label for this row out of the "state" column final int columnIndex = cursor.getColumnIndexOrThrow(Tracks.NAME); final String str = cursor.getString(columnIndex); return str; } }); //TODO : Maybe I should have this in an external object I'd reuse instead of two anonymous objects ? mEndStationSimpleCursorAdapter .setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() { public String convertToString(android.database.Cursor cursor) { // Get the label for this row out of the "state" column final int columnIndex = cursor.getColumnIndexOrThrow(Tracks.NAME); final String str = cursor.getString(columnIndex); return str; } }); //DO NOT BE TEMPTED, THE FOLLOWING CODE MAKE THE QUERY FROM THE UI THREAD I THINK, AND THIS IS BAD, VERY BAD ! // Set the FilterQueryProvider, to run queries for choices // that match the specified input. /*mStartStationSimpleCursorAdapter.setFilterQueryProvider(new FilterQueryProvider() { public Cursor runQuery(CharSequence constraint) { // Search for states whose names begin with the specified letters. Cursor cursor = mDbHelper.getMatchingStates( (constraint != null ? constraint.toString() : null)); return cursor; } });*/ }