如何在Android中的应用程序的小部件中打印ListView的子项

时间:2015-04-16 06:38:42

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

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

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)函数,我们在listview中打印所有项目。我使用'+'运算符和'\ 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]);
    }

1 个答案:

答案 0 :(得分:0)

您应该充气另一个列表视图。所以你可以获取subListView位置来获取子元素。

您可以在https://stackoverflow.com/a/11653149/210973

找到一个好的解决方案