I am rather new at Android and am having some issues in understanding the usage of cwac-MergeAdapter.
I am trying to use MergeAdapter to populate a spinner; my instance of MergeAdapter should include a SimpleCursorAdapter, which is correctly reading data from my db, and (as a footer) a new TextView (or Button) which should be clickable.
Currently, if I feed the spinner the mergeAdapter containing only data from the db, everything works like a charm; however, as I add the new view, I only get a single, blank entry in the whole spinner. Can someone please help me with this?
Here follows the code:
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);
It is unlikely that I will ever support adding individual
Viewsto aMergeAdapterto be used in aSpinner. That would imply that I somehow know how to create a drop-downViewfor some arbitraryView.The workaround I have for you is for you to add the following to your fork of
MergeAdapter:Then, instead of adding an individual
ViewviaaddView(), add your ownArrayAdapterfor your additional data. Be sure to callsetDropDownViewResource()on all your adapters inside of theMergeAdapter— your sample code never called this, causing some of your other difficulties (e.g., “some entries in the spinner are blank”).It is possible that I will merge this change into
MergeAdapter, or perhaps will create aSpinnerMergeAdaptersubclass with it. The blind cast toSpinnerAdapterscares me, which is why I want to ponder this a bit more. In light testing, the code I have above seems to work OK.Also, please delete all occurrences of
getApplicationContext()from your code, replacing it withthisorWhateverYourActivityNameIs.thisas appropriate. Only usegetApplicationContext()when you know why you are usinggetApplicationContext().