如何在 MergeAdapter 中组合适配器和视图

发布于 2024-12-01 05:33:49 字数 1057 浏览 1 评论 0原文

我对 Android 相当陌生,在理解 cwac-MergeAdapter 的用法方面遇到了一些问题。

我正在尝试使用 MergeAdapter 来填充微调器;我的 MergeAdapter 实例应该包括一个 SimpleCursorAdapter,它可以正确地从我的数据库读取数据,以及(作为页脚)一个新的 TextView(或 Button),它应该是可单击的。

目前,如果我向 Spinner 提供仅包含数据库中数据的 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);

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦亿 2024-12-08 05:33:49

我不太可能支持将单独的Views 添加到MergeAdapter 以在Spinner 中使用。这意味着我以某种方式知道如何为某些任意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 code>,为您的附加数据添加您自己的 ArrayAdapter。请务必在 MergeAdapter 内的所有适配器上调用 setDropDownViewResource() ——您的示例代码从未调用过此函数,从而导致其他一些困难(例如,“某些条目旋转器中是空白的”)。

我可能会将此更改合并到 MergeAdapter 中,或者可能会用它创建一个 SpinnerMergeAdapter 子类。对 SpinnerAdapter 的盲目强制转换让我感到害怕,这就是为什么我想多思考一下这个问题。在简单的测试中,我上面的代码似乎工作正常。


另外,请从代码中删除所有出现的 getApplicationContext(),并将其替换为 thisWhateverYourActivityNameIs.this视情况而定。 当您知道为什么使用getApplicationContext()时才使用getApplicationContext()

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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文