自定义Listview适配器,添加静态页脚,并了解R.id.list

发布于 2024-09-07 22:29:18 字数 2091 浏览 9 评论 0原文

有些东西我没有得到,我正在寻求帮助来理解这里发生的事情。

我有一个自定义列表适配器(仅扩展 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 技术交流群。

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

发布评论

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

评论(1

羅雙樹 2024-09-14 22:29:18

您将 ListViewTextView 设置为 android:layout_above="@id/bottom_control_bar",这意味着 TextView< /code> 将与 ListView 重叠。而且,您已经说过您的 ListView 高度为 0dip,这将使列表变得非常短。

我将 ListView 定义为位于 TextView 之上并锚定到屏幕顶部 (android:layout_alignParentTop="true")。

有什么方法可以连接
在我的 xml 中声明的 ListView 小部件
与我的 DeviceListAdapter?

通过调用 setListAdapter(),您已经做到了。

You have both the ListView and the TextView set to android:layout_above="@id/bottom_control_bar", which means the TextView will overlap the ListView. And, you have said that your ListView height is 0dip, which will make for an extremely short list.

I would define the ListView as being above the TextView and anchored to the top of the screen (android:layout_alignParentTop="true").

Is there some way to link up the
ListView widget declared in my xml
with my DeviceListAdapter?

You already are, by calling setListAdapter().

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