具有多种选择和自定义视图的对话框
我现在已经在这个问题上苦苦挣扎了一天,但我不知道如何解决它。
因此,我有一个 AlertDialog,我想在其中显示要选择的项目列表。这些项目必须具有多个文本视图,因此我不能依赖使用默认格式的构建器上的简单 setMultiChoiceItems()
。
我在这里使用我的自定义 ArrayAdapter,这样做:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose details to display");
builder.setAdapter(new ContactAdapter(this, 0, items), null);
我想我可以使用 OnClickListener
作为 setAdapter
的第二个参数,但在选择一个选项后会关闭对话框。
为了解决这个问题,当我获取行布局时,我在 ContactsAdapter 中添加了一个 setOnClickListener()
,如下所示:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.contact_row, null);
view.setOnClickListener(new OnClickListener() {....}
这有效,但当我单击一行时,我不再获得突出显示,而我确实没有这样做不想要。
有什么想法如何解决这个问题吗?我知道我可以使用 ListActivity 并且这很容易解决,但我真的想在对话框中执行此操作。谢谢!
I'm struggling with this problem for a day now, and I just can't figure out how to solve it.
So, I have an AlertDialog where I want to display a list of items to choose. These items must have multiple text views and so, so I cannot rely on a simple setMultiChoiceItems()
on builder using default format.
I use my custom ArrayAdapter here, by doing this:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose details to display");
builder.setAdapter(new ContactAdapter(this, 0, items), null);
I thought I could use an OnClickListener
as the second paramtere of setAdapter
but that closes the dialog after choosing one option.
Trying to fix this I added a setOnClickListener()
inside my ContactsAdapter when I fetch the row layout, like this:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.contact_row, null);
view.setOnClickListener(new OnClickListener() {....}
This works, but I stop getting the highlight when I click a row, which I really don't want.
Any ideas how to solve this? I know I could use a ListActivity and that would be easy to solve, but I really want to do this in a dialog. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您认为使用
ListActivity
很容易做到,为什么不这样做并使用setTheme(android.R.style.Theme_Dialog)
或 <应用程序清单中的 Activity 的 code>android:theme="@android:style/Theme.Dialog" ?如果您的目标是 Honeycomb 或 Ice Cream Sandwich (Android 3.0+),则称为
setTheme(android.R.style.Theme_Holo_Dialog)
和android:theme="@android:style/Theme .Holo.Dialog”
。If you think it'd be easy to do using a
ListActivity
, why not do that and use the dialog theme usingsetTheme(android.R.style.Theme_Dialog)
orandroid:theme="@android:style/Theme.Dialog"
for the Activity in the application manifest?If you're targeting Honeycomb or Ice Cream Sandwich (Android 3.0+), it's called
setTheme(android.R.style.Theme_Holo_Dialog)
andandroid:theme="@android:style/Theme.Holo.Dialog"
.