如何在 MergeAdapter 中组合适配器和视图
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太可能支持将单独的
Views
添加到MergeAdapter
以在Spinner
中使用。这意味着我以某种方式知道如何为某些任意View
创建下拉View
。我为您提供的解决方法是将以下内容添加到您的
MergeAdapter
分支中:然后,不要通过
addView()View
code>,为您的附加数据添加您自己的ArrayAdapter
。请务必在MergeAdapter
内的所有适配器上调用setDropDownViewResource()
——您的示例代码从未调用过此函数,从而导致其他一些困难(例如,“某些条目旋转器中是空白的”)。我可能会将此更改合并到
MergeAdapter
中,或者可能会用它创建一个SpinnerMergeAdapter
子类。对SpinnerAdapter
的盲目强制转换让我感到害怕,这就是为什么我想多思考一下这个问题。在简单的测试中,我上面的代码似乎工作正常。另外,请从代码中删除所有出现的
getApplicationContext()
,并将其替换为this
或WhateverYourActivityNameIs.this
视情况而定。 仅当您知道为什么使用getApplicationContext()
时才使用getApplicationContext()
。It is unlikely that I will ever support adding individual
Views
to aMergeAdapter
to be used in aSpinner
. That would imply that I somehow know how to create a drop-downView
for 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
View
viaaddView()
, add your ownArrayAdapter
for 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 aSpinnerMergeAdapter
subclass with it. The blind cast toSpinnerAdapter
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 withthis
orWhateverYourActivityNameIs.this
as appropriate. Only usegetApplicationContext()
when you know why you are usinggetApplicationContext()
.