0

我正在使用SimpleCursorAdapter從數據庫中填充名稱列的微調器。顯示數據庫中選擇的項目的數據庫詳細信息

適配器:

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 
    } 

我怎麼可能,在離心器中選擇一個項目(名稱列),顯示出一些textviews所有其他細節?

回答

1

首先,爲微調器設置一個偵聽器,以便在選擇某個項目時獲得回調。

mSuppliersSpinner.setOnItemSelectedListener(this); 

我提供「這個」作爲聽衆,因爲我的片段/活動實現了接口,但你可以寫在括號中的一個爲好。 可以實現此方法:

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

基於ID,或位置,你知道選擇哪個條目。此時您可以啓動另一個CursorLoader(帶有選擇,因此您只能獲得此特定條目的詳細信息)。當您在onLoadFinished中獲得回調時,可以在TextView中顯示詳細信息。

相關問題