2011-08-25 51 views
0

一個觀點我是比較新的結合,我理解CWAC-MergeAdapter的使用有一些問題。如何在Android的適配器和MergeAdapter

我試圖用MergeAdapter來填充微調;我的MergeAdapter實例應該包含一個SimpleCursorAdapter,它正確地從我的數據庫讀取數據,並且(作爲頁腳)一個新的TextView(或Button),它應該是可點擊的。

目前,如果我喂從數據庫中只包含數據的mergeAdapter,一切就像一個魅力的微調;然而,當我添加新視圖時,我只在整個微調器中獲得一個單一的空白條目。有人可以幫助我嗎?

這裏如下代碼:

essenceItems = new SimpleCursorAdapter(this, R.layout.db_row_view, 
    essenceCursor, from, to); 

    TextView addEssence = new TextView(getApplicationContext()); 
    addEssence.setTextColor(R.color.red); 
    addEssence.setText("Add new item..."); 
    addEssence.setWidth(ViewGroup.LayoutParams.FILL_PARENT); 
    addEssence.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); 
    addEssence.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
     startActivityForResult(new Intent(v.getContext(), 
     EssencePopup.class), ADD_ESSENCE); 

    } 
    }); 
    ma = new MergeAdapter(); 
    ma.addAdapter(essenceItems); 
    ma.addView(addEssence, true); 
    spinner.setAdapter(ma); 
+0

僅供參考,爲GitHub庫主頁顯示,您需要標記您的問題與'commonsware'爲我一定去接他們。我會盡快調整你的標籤列表。 「當我添加新視圖時,我只在整個微調器中得到一個單一的空白條目」 - 如果你在'MergeAdapter'上調用'getCount()'會發生什麼?它會返回正確的數字嗎? – CommonsWare

+0

感謝您指點!是:getCount()在添加適配器後返回1,在添加視圖後返回2。但是,當我打開微調器時,仍然只有一條空行。 – Tilvia

+0

這很奇怪。如果您可以創建一個演示此錯誤的示例項目,我很樂意看看它。你可以從這個問題鏈接到它(如果你可以上傳到某個地方)或者發送到mmurphy -at- commonsware.com。 – CommonsWare

回答

2

這是不可能的,我將永遠支持添加個人ViewsMergeAdapterSpinner使用。這意味着我以某種方式知道如何爲一些任意的View創建下拉View

的解決方法我對你是爲你添加以下到您的MergeAdapter叉:

public View getDropDownView(int position, View convertView, ViewGroup parent) { 
    for (ListAdapter piece : pieces) { 
    int size = piece.getCount(); 

    if (position < size) { 
     return (((SpinnerAdapter)piece).getDropDownView(position, convertView, parent)); 
    } 

    position -= size; 
    } 

return (null); 
} 

然後,而不是通過addView()添加個人View,添加自己的ArrayAdapter爲您額外的數據。一定要打電話setDropDownViewResource()在所有適配器MergeAdapter裏面 - 你的示例代碼從來不叫這個,引起你的一些其他方面的困難(例如,「在微調一些條目是空白」)。

我可能會將此更改合併到MergeAdapter中,或者可能會創建一個SpinnerMergeAdapter子類。盲目地投到SpinnerAdapter讓我害怕,這就是爲什麼我想多思考這個問題。在輕度測試中,我上面的代碼似乎工作正常。


另外,請從您的代碼刪除所有出現getApplicationContext(),與thisWhateverYourActivityNameIs.this適當取代它。 只有使用getApplicationContext()當你知道爲什麼使用getApplicationContext()

+0

感謝您的建議,並花時間看看我的代碼。我現在真的瞭解更多 - 並且該項目正在開展工作:)再次感謝。 – Tilvia