Android getting strings from sqlite database to autocompletetextview

梦想的初衷 提交于 2019-12-04 10:57:07

Use a SimpleCursorAdapter, as shown in this article.

_descriptionText = (AutoCompleteTextView) findViewById(R.id.description);
final int[] to = new int[]{android.R.id.text1};
final String[] from = new String[]{VehicleDescriptionsTable.DESCRIPTION};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
        android.R.layout.simple_dropdown_item_1line,
        null,
        from,
        to);

// This will provide the labels for the choices to be displayed in the AutoCompleteTextView
adapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() {
    @Override
    public CharSequence convertToString(Cursor cursor) {
        final int colIndex = cursor.getColumnIndexOrThrow(VehicleDescriptionsTable.DESCRIPTION);
        return cursor.getString(colIndex);
    }
});

// This will run a query to find the descriptions for a given vehicle.
adapter.setFilterQueryProvider(new FilterQueryProvider() {
    @Override
    public Cursor runQuery(CharSequence description) {
        String vehicle = getSelectedVehicle();
        Cursor managedCursor = _helper.getDescriptionsFor(vehicle, description.toString());
        Log.d(TAG, "Query has " + managedCursor.getCount() + " rows of description for " + vehicle);
        return managedCursor;
    }
});

_descriptionText.setAdapter(adapter);

From here, you just need to add a function to return a cursor with your database, and add the adapter to your AutoTextCompleteTextView. You will need to get _id from your logbook query, in fact, you might just consider getting the entire row (SELECT * FROM table). If you don't have a column _id, try SELECT data, rowid AS _id FROM table.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!