它是好的动态改变一个ListView的接口?它是、接口、动态、ListView

2023-09-06 01:37:12 作者:叫兽.灯泡好闪!

而不是创建多个活动的

,我想根据需要修改 ArrayAdapter 的ListView 的。我没有看到任何提及API关于是否是可以调用 setAdapter()不止一次。

要更具体,说我想开始有一个的ListView 的活动。在这个例子中,的ListView 与一个 listView.setAdapter(本),比如说,从一个 CategoryArrayAdapter 。

然后,用户选择一个类别。如果没有开始一个新的活动时,code将设置为相同的的ListView 一个新的适配器。新的适配器,说 ItemArrayAdapter 通话 listView.setAdapter(本)

是否有人有经验,成就了这个成功还是知道的具体原因本不应该做?

解决方案   

我没有看到任何提及有关是否是好的调用setAdapter(API中)一次以上。

简单的答案是肯定的,我以前也做过类似的某种东西。

C listView1 Click点击事件,然后显示在textbox上,连接数据库的

这就是为什么适配器是存在的API中提供的原因。实际内容(模型)和它如何呈现(查看)为每个列表项分离和内部android.widget.Adapter实施,而不是直接结合到android.widget.AdapterView。只要您的适配器正确实施,可以交换/更改绑定到ListView实际的基础适配器,只需通过调用setAdapter()方法。

Instead of creating multiple activities, I would like to change the ArrayAdapter of the ListView as needed. I don't see any mention in the API about whether or not it is okay to call setAdapter() more than once.

To be more specific, say I would like to start an activity that has a ListView. In this example, the ListView is initialized with a listView.setAdapter(this) from, say, a CategoryArrayAdapter.

Then a user selects a category. Without starting a new activity, the code will set a new adapter for the same ListView. The new adapter, say ItemArrayAdapter calls listView.setAdapter(this).

Does someone have experience having done this successfully or know of a specific reason why this shouldn't be done?

解决方案

I don't see any mention in the API about whether or not it is okay to call setAdapter() more than once.

The simple answer is YES, and I have done similar sort of things before.

This is exactly the reason why Adapter is existed and provided in the API. The actual content (Model) and how it is rendered (View) for each list items is isolated and implemented inside android.widget.Adapter, instead of directly bound to android.widget.AdapterView. As long as your adapter is properly implemented, you can swap/change the actual underlying adapter that bound to the ListView, simply by calling the setAdapter() method.