显示在微调器

时间:2017-07-26 11:09:53

标签: android sqlite android-sqlite simplecursoradapter android-cursorloader

我正在使用SimpleCursorAdapter在数据库中使用名称列填充Spinner。

适配器:

spinnerAdapter = new SimpleCursorAdapter(
        this,
        android.R.layout.simple_spinner_item,
        null,
        new String[] {SupplierEntry.COLUMN_SUPPLIER_NAME},
        new int[] {android.R.id.text1},
        0);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
mSuppliersSpinner.setAdapter(spinnerAdapter);

getLoaderManager().initLoader(SUPPLIERS_LOADER, null, this);

光标加载器:

@Override
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
        // Define a projection that specifies the columns from the table we care about.
        String[] projection = {
                SupplierEntry._ID,
                SupplierEntry.COLUMN_SUPPLIER_NAME};

        // This loader will execute the ContentProvider's query method on a background thread
        return new CursorLoader(this,        // Parent activity context
                SupplierEntry.CONTENT_URI,   // Provider content URI to query
                projection,                  // Columns to include in the resulting Cursor
                null,                        // No selection clause
                null,                        // No selection arguments
                null);                       // Default sort order
    }

在微调器(名称列)中选择项目后,如何在某些文本视图中显示所有其他详细信息?

1 个答案:

答案 0 :(得分:1)

首先,将一个侦听器设置为微调器,以便在选择一个项目时获得回调。

mSuppliersSpinner.setOnItemSelectedListener(this);

我提供'this'作为监听器,因为我的Fragment / Activity实现了接口,但你也可以在括号之间写一个。 您可以实现此方法:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
    //Start another cursorloader to get the details
}

根据ID或位置,您知道选择了哪个条目。此时,您可以启动另一个CursorLoader(带有选择,以便您只获取此特定条目的详细信息)。当您在onLoadFinished中获得回调时,您可以在TextViews中显示详细信息。