【问题标题】:Showing only specific attributes from xml in ListView Android在 ListView Android 中仅显示来自 xml 的特定属性
【发布时间】:2016-11-19 12:13:43
【问题描述】:

我试图在我的列表中显示一些项目的名称,点击时,我应该显示更多,甚至可以更新/编辑内容。我的活动 xml 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_list_items"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#035E7B"
    tools:context="com.example.melisaam.logintest.ListItemsActivity">

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:entries="@array/sections" >
    </ListView>

</RelativeLayout>

我的数组 xml 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="sections">
        <item>
            <name>Belgian Waffles</name>
            <price>$5.95</price>
            <description>
                Two of our famous Belgian Waffles with plenty of real maple syrup
            </description>
            <calories>650</calories>
        </item>
        <item>
            <name>Strawberry Belgian Waffles</name>
            <price>$7.95</price>
            <description>
                Light Belgian waffles covered with strawberries and whipped cream
            </description>
            <calories>900</calories>
        </item>
        <item>
            <name>Berry-Berry Belgian Waffles</name>
            <price>$8.95</price>
            <description>
                Belgian waffles covered with assorted fresh berries and whipped cream
            </description>
            <calories>900</calories>
        </item>
        <item>
            <name>French Toast</name>
            <price>$4.50</price>
            <description>
                Thick slices made from our homemade sourdough bread
            </description>
            <calories>600</calories>
        </item>
        <item>
            <name>Homestyle Breakfast</name>
            <price>$6.95</price>
            <description>
                Two eggs, bacon or sausage, toast, and our ever-popular hash browns
            </description>
            <calories>950</calories>
        </item>
    </string-array>
</resources>

目前我在我的活动中以这种方式加载我的项目:

        Log.d(TAG, "Trying to create the list activity");
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_list_items);


        //* *EDIT* *
        this.listView  = (ListView) findViewById(R.id.listView1);

但这不是很好。我所有的属性都放在我的列表中的一行中。我希望在我的列表视图中仅显示名称和价格,单击时应显示其他信息。有什么想法可以在没有 xml 解析器的情况下做到这一点,或者这是唯一的解决方案吗?

【问题讨论】:

    标签: android xml listview


    【解决方案1】:

    您需要定义自己的列表视图布局,并通过列表适配器对其进行映射。而且据我了解,您需要ExpandableListView,以便在按下时显示更多项目。

    【讨论】:

      猜你喜欢
      • 2021-10-13
      • 2013-05-16
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 2013-01-02
      • 1970-01-01
      • 2017-08-04
      • 1970-01-01
      相关资源
      最近更新 更多