2015-04-16 36 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

相關問題