自定义Listview适配器,添加静态页脚,并了解R.id.list
有些东西我没有得到,我正在寻求帮助来理解这里发生的事情。
我有一个自定义列表适配器(仅扩展 BaseAdapter),我已成功使用它来生成和显示列表。现在我想将静态页脚添加到列表底部。查看了许多资源后(特别是 这个) 我开始意识到我对使用 XML 的不情愿必须结束,并在名为 devices_list.xml 的文件中设置以下 xml 布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout android:id="@+id/bottom_control_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<ToggleButton android:id="@+id/bottom_control_toggle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textOff="Filter Favourites OFF"
android:textOn="Filter Favourites ON"/>
</LinearLayout>
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_above="@id/bottom_control_bar">
</ListView>
<TextView android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_empty_list"
android:layout_above="@id/bottom_control_bar"/>
</RelativeLayout>
:保存列表,我运行代码。我看到我的页脚(以及作为所有内容的父级的选项卡小部件),但列表所在的区域是空的。
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.devices_list);
db = new DbManager(this);
db.open();
AllCur = db.fetchAllDevices();
startManagingCursor(AllCur);
list = new DeviceListAdapter(this, AllCur); //make my custom list adapter
setListAdapter(list);
}
有没有办法将我的 xml 中声明的 ListView 小部件与我的 DeviceListAdapter 链接起来?现在我很清楚,我并不完全确定这一切是如何运作的。任何澄清方面的帮助将不胜感激。
There is something I'm just not getting, and I'm looking for assistance in understanding what is happening here.
I have a custom list adapter (that just extends BaseAdapter) that I have successfully been using to generate and display a list. Now I want to add a static footer to the bottom of my list. After looking at a number of resources (specifically this one) I've come to realize that my reluctance of using XML has to come to an end, and set up the following xml layout in a file called devices_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout android:id="@+id/bottom_control_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<ToggleButton android:id="@+id/bottom_control_toggle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textOff="Filter Favourites OFF"
android:textOn="Filter Favourites ON"/>
</LinearLayout>
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_above="@id/bottom_control_bar">
</ListView>
<TextView android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/main_empty_list"
android:layout_above="@id/bottom_control_bar"/>
</RelativeLayout>
After some adjustments to the activity that holds the list, I ran the code. I see my footer, (and also the tab widget which is parent to everything), but the area where the list goes is empty.
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.devices_list);
db = new DbManager(this);
db.open();
AllCur = db.fetchAllDevices();
startManagingCursor(AllCur);
list = new DeviceListAdapter(this, AllCur); //make my custom list adapter
setListAdapter(list);
}
Is there some way to link up the ListView widget declared in my xml with my DeviceListAdapter? It's pretty clear to me now that I'm not entirely sure about how this is all working. Any help in clarification would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将
ListView
和TextView
设置为android:layout_above="@id/bottom_control_bar"
,这意味着TextView< /code> 将与
ListView
重叠。而且,您已经说过您的ListView
高度为0dip
,这将使列表变得非常短。我将
ListView
定义为位于TextView
之上并锚定到屏幕顶部 (android:layout_alignParentTop="true"
)。通过调用
setListAdapter()
,您已经做到了。You have both the
ListView
and theTextView
set toandroid:layout_above="@id/bottom_control_bar"
, which means theTextView
will overlap theListView
. And, you have said that yourListView
height is0dip
, which will make for an extremely short list.I would define the
ListView
as being above theTextView
and anchored to the top of the screen (android:layout_alignParentTop="true"
).You already are, by calling
setListAdapter()
.