0

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);
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Tilvia
  • 43
  • 1
  • 5
  • FYI, as the GitHub repo home page indicates, you need to tag your questions with `commonsware` for me to necessarily pick them up. I will adjust your tag list shortly. "as I add the new view, I only get a single, blank entry in the whole spinner" -- what happens if you call `getCount()` on the `MergeAdapter`? Does it return the right number? – CommonsWare Aug 25 '11 at 11:01
  • Thanks for pointing! Yes: getCount() returns 1 after the adapter is added and 2 after also the view is added. However, when I open the spinner, there is still just one blank line. – Tilvia Aug 25 '11 at 11:37
  • That is very strange. If you can create a sample project that demonstrates this error, I would be happy to take a look at it. You can either link to it from this issue (if you can upload it somewhere public) or send it to mmurphy -at- commonsware.com. – CommonsWare Aug 25 '11 at 14:08

1 Answers1

2

It is unlikely that I will ever support adding individual Views to a MergeAdapter to be used in a Spinner. That would imply that I somehow know how to create a drop-down View for some arbitrary View.

The workaround I have for you is for you to add the following to your fork of 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);
}

Then, instead of adding an individual View via addView(), add your own ArrayAdapter for your additional data. Be sure to call setDropDownViewResource() on all your adapters inside of the MergeAdapter -- 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 a SpinnerMergeAdapter subclass with it. The blind cast to SpinnerAdapter scares 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 with this or WhateverYourActivityNameIs.this as appropriate. Only use getApplicationContext() when you know why you are using getApplicationContext().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for your suggestions and for having taken the time to have a look at my code. I really do understand more now - and the project is working :) Thanks again. – Tilvia Aug 26 '11 at 12:10