【问题标题】:How to print sub items of ListView in widget of an app in Android如何在Android应用程序的小部件中打印ListView的子项
【发布时间】:2015-04-16 06:38:42
【问题描述】:

我需要为我的 android 应用程序创建一个带有 listview 的小部件。所以我在谷歌搜索,我发现下面有一个链接。

https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget/LoremWidget

我按照上面提到的链接进行操作。一切正常。但我无法打印列表视图的子项。

Widget_Layout

<?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:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:gravity="top"
   tools:context=".MainActivity"
   android:background="@drawable/widget_frame" >
<ListView
  android:id="@+id/listItem"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_marginTop="45dp"
  android:cacheColorHint="#00000000"
  android:background="@drawable/widget_frame"
/>
</RelativeLayout>

这是 ListView 项目的布局

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:textSize="20dp"
android:minHeight="?android:attr/listPreferredItemHeight"/>

这是我们打印列表视图中所有项目的 getViewAt(int position) 函数。我使用“+”运算符和“\n”打印了项目的子项目。但我无法区分项目及其子项目,这意味着我无法为子项目赋予特定属性。谁能帮我区分子项目和项目。

getViewAt

    public RemoteViews getViewAt(int position) {
    RemoteViews row=new RemoteViews(mCtxt.getPackageName(),R.layout.widget_layout);
    row.setTextViewText(android.R.id.text1, Names[position]+"\n"+address[position]);
    }

【问题讨论】:

    标签: android android-listview android-widget android-xml


    【解决方案1】:

    你应该膨胀另一个列表视图。所以你可以取 subListView 的位置,来获取子元素。

    你可以在那里找到一个好的解决方案https://stackoverflow.com/a/11653149/210973

    【讨论】:

    • 还有像row.setTextzviewText这样的方法吗? @Megawolt
    • 也许你可以迭代 Names[position] string names = Names[position]+"\n"+address[position]; for (FooModel item : Names[position]) { names += item.surname; } row.setTextViewText(android.R.id.text1, names);
    猜你喜欢
    • 1970-01-01
    • 2019-09-21
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 2015-05-10
    相关资源
    最近更新 更多