2015-04-16 19 views
0

我需要为我的android应用程序创建一个带有listview的小部件。所以我在谷歌搜索,我发现一个链接taht在下面。如何在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位置)功能。我使用'+'运算符和'\ 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]); 
    } 

回答

0

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

你可以在那里https://stackoverflow.com/a/11653149/210973

+0

一个很好的解决方案有没有像row.setTextzviewText任何其他方式? @Megawolt – user3546693

+0

可能是你可以迭代** Names [position] **'string names = Names [position] +“\ n”+ address [position]; (FooModel item:Names [position]){ names + = item.surname; } row.setTextViewText(android.R.id.text1,names);' – Megawolt

相关问题