开发者

How to combine an adapter and a view in MergeAdapter

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 SimpleCu开发者_如何学运维rsorAdapter, 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 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().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜